static-analysis

How to get a warning about a list being a mutable default argument?

How to get a warning about a list being a mutable default argument? Question: I accidentally used a mutable default argument without knowing it. Is there a linter or tool that can spot this and warn me? Asked By: Pascal T. || Source Answers: flake8-bugbear, Pylint, PyCharm, and Pyright can detect this: Bugbear has B006 …

Total answers: 1

Pylint R1732 ("Consider using 'with'") for one-liner: is it really good advice?

Pylint R1732 ("Consider using 'with'") for one-liner: is it really good advice? Question: On a line such as r = open(path, encoding="utf-8").read() (actual line here), Pylint 2.14.5 provides the following advise: submodules-dedup.py:71:32: R1732: Consider using ‘with’ for resource-allocating operations (consider-using-with) If I understand correctly, the suggestion is to change it to with open(path, encoding="utf-8") as …

Total answers: 2

Mypy complaining about Name "Optional" is not defined without the use of Optional

Mypy complaining about Name "Optional" is not defined without the use of Optional Question: I’ve recently started using mypy, and have run into some weird problems that i cannot for the life of me seem to figure out. I’m using mypy 0.950, django-stubs 1.11.0, django 4.0.5 and python 3.10.2. Running mypy through the command line …

Total answers: 1

Why is imperative mood important for docstrings?

Why is imperative mood important for docstrings? Question: The error code D401 for pydocstyle reads: First line should be in imperative mood. I often run into cases where I write a docstring, have this error thrown by my linter, and rewrite it — but the two docstrings are semantically identical. Why is it important to …

Total answers: 7

How to find out if (the source code of) a function contains a loop?

How to find out if (the source code of) a function contains a loop? Question: Let’s say, I have a bunch of functions a, b, c, d and e and I want to find out if they directly use a loop: def a(): for i in range(3): print(i**2) def b(): i = 0 while i …

Total answers: 3

Excluding directory

Excluding directory Question: I am working on a django project and am trying to run pyflakes on an app in it. I need to exclude the “migrations” directory from pyflakes. For pep8 I can do pep8 –exclude=migrations app_name Is there any similar way for pyflakes? I couldn’t find any proper documentation for pyflakes. Asked By: …

Total answers: 2

Import order coding standard

Import order coding standard Question: PEP8 suggests that: Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports You should put a blank line between each group of imports. Is there a way to check if the standard is violated anywhere in the package using static …

Total answers: 6

Is there a need for a "use strict" Python compiler?

Is there a need for a "use strict" Python compiler? Question: There exist static analysis tools for Python, but compile time checks tend to be diametrically opposed to the run-time binding philosophy that Python embraces. It’s possible to wrap the standard Python interpreter with a static analysis tool to enforce some “use strict“-like constraints, but …

Total answers: 10

How can I analyze Python code to identify problematic areas?

How can I analyze Python code to identify problematic areas? Question: I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed. Specifically, I’d like to call out routines with a high cyclomatic complexity, …

Total answers: 8