-1

I have a small flask app that has the code below. Please note that I have removed a great deal of code to simply my question. If anything needed re-adding or missing, let me know.

1   @app.route("/app/data/", methods=['POST'])
2   def methodpost():
3
4       if 'Content-Type' in request.headers and request.headers['Content-Type'] == 'application/json':
5
6       #some_code
7       #some_more_code
8
9       if not any(['unittest' or 'discover' in arg for arg in sys.argv]):
10
11          #code_calls_external_stuff
12          #more_code
13
14
15      return msg
16
17
18      app.run(host=0.0.0.0, debug=debug, port=5000)

When I do a cURL to it using the following:

curl -X POST "localhost:5000/app/data/" -H "Content-Type: application/json" -d @jsonBody

It does not go into LINE-11 and LINE-12.

However, when I modify LINE-9 to the following:

----FROM:----
if not any(['unittest' or 'discover' in arg for arg in sys.argv]):
----TO:----
if not any(['unittest' in arg for arg in sys.argv]):

then it works and calls LINE-11 and LINE-12. The above was only added so I can run UnitTest and Tox while the app is not running because i did not want to make external calls... Is there a way to get around it while keeping the code as it is.

Saffik
  • 911
  • 4
  • 19
  • 45

1 Answers1

2
if not any(['unittest' or 'discover' in arg for arg in sys.argv]):

isn't doing what you seem to want it to. Simplify this to

['x' or 'y' in a for a in ['y']]

run it in the REPL, and contemplate why it responds with ['x'].

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • I'm not sure if I follow sorry, a beginner here but I am not passing any sys.args to it at all? – Saffik Apr 15 '20 at 18:35
  • 1
    sys.args has no effect in that code as written. The precendence of `or` ensures that the answer will always be `['unittest']`, and `not any(['unittest'])` will always be False. – Dave W. Smith Apr 15 '20 at 18:42
  • Right gotcha. I have now split it in to 2 statements and it is a workaround for now... – Saffik Apr 15 '20 at 19:02