rrudnicki
rrudnicki

Reputation: 11

missing 1 required positional argument on Return

I'm getting missing 1 required positional argument on the code below. I tried different stuffs but no luck. I believe it is a small detail that I'm missing but since I'm a beginner on Python and I'm not being able to fix it myself.

import requests
from flask import Flask, jsonify, request, render_template
import os

app = Flask(__name__)

    @app.route('/outages')
    def map_status(data):
    
      url = "https://my-api.local.com/data"
    
      response = requests.get(url)
    
      result_data = json.loads(response.text)
      if(data['install_status']=='1'):
        data['install_status']='Installed'
      elif(data['install_status']=='3'):
            data['install_status']='Maintenance'
      return data
    
      result_data['result']=map(map_status,result_data['result'])
    
      return(list(result_data['result']))

This is the error I'm getting: [ERR] TypeError: map_status() missing 1 required positional argument: 'data'

If I change the return for print, it works, but on this case I need to use return to get the data.

Upvotes: 0

Views: 2937

Answers (1)

Shidouuu
Shidouuu

Reputation: 409

When you call a function that has arguments, you must specify the value of them. The correct code would be:

result_data['result'] = map(map_status(insert_data), result_data['result'])

where (insert_data) is the place where you would insert the value of whatever needs to be put there.

If you don't want the argument to be required all the time you can specify an optional argument like so:

def map_status(data=None)

This makes the initial value of data a NoneType. When you want to call the function with the data argument, just do so like you would a normal argument, but with an equal sign, like so:

map_status(data="hello")

You can also just make the optional argument an empty literal:

def map_status(data="")

Upvotes: 1

Related Questions