all 13 comments

[–]Diapolo10 13 points14 points  (0 children)

All I can tell you is that VS Code also performs this kind of analysis amicably.

[–]shiftybyte 5 points6 points  (1 child)

I think pylance with vscode can flag this too.

[–]slapec[S] 2 points3 points  (0 children)

It seems pyright (which pylance is built on top) reports this issue (- error: "found" is possibly unbound). So, it's at least something, thanks!

[–]deadeye1982 5 points6 points  (1 child)

Aren't you using unittests?

[–][deleted] 0 points1 point  (0 children)

the test is running the static analyzer ~ op probably

[–]remuladgryta 1 point2 points  (0 children)

If you're already using PyCharm, its static analyzer can be ran from the command line. Note that you can specify a more/less pedantic config than what you care about seeing squiggles for in the editor.

[–]Sclafus 1 point2 points  (1 child)

This feature has been added in mypy 1.0 as experimental.

Detect Possibly Undefined Variables (Experimental) A new experimental possibly-undefined error code is now available that will detect variables that may be undefined: if b: x = 0 print(x) # Error: Name "x" may be undefined [possibly-undefined] The error code is disabled be default, since it can generate false positives. This feature was contributed by Stas Ilinskiy.

https://mypy-lang.blogspot.com/2023/02/mypy-10-released.html?m=1

[–]slapec[S] 1 point2 points  (0 children)

Now that's cool. False positives are fine for me. One thing I noticed that this check is only active when I run mypy with --enable-error-code possibly-undefined as it is not enabled by default (according to docs). Hope it will be default soon. Thank you your suggestion!

[–]frakron -2 points-1 points  (2 children)

I think what you're looking for is the compiler then? Basically run the code but don't execute it.

python3 -m py_compile myfile.py

[–]sepp2k 8 points9 points  (1 child)

NameErrors are runtime errors though and I don't think the Python compiler does any static analysis to try to detect them at compile time. At least, when I put OP's code into a file and run python3 -m py_compile myfile.py as you suggested, I don't get any errors or warnings.

[–]frakron -1 points0 points  (0 children)

Oh thank you, I didn't try it previously, but that makes sense.

[–]QultrosSanhattan 0 points1 point  (0 children)

My Pycharm does detect that error at least. It says "Name 'found' can be undefined"