[ruff] Activate flake8-bugbear and flake8-pyi#11914
Conversation
6868db7 to
0140b16
Compare
bluetech
left a comment
There was a problem hiding this comment.
Left some comments, the commits made it really easy to review, thanks.
From what I've seen the lints are nice so I vote to 👍 on bugbear.
| # Negative tolerances are not allowed. | ||
| with pytest.raises(ValueError): | ||
| 1.1 == approx(1, rel, abs) | ||
| 1.1 == approx(1, rel, abs) # noqa: B015 |
There was a problem hiding this comment.
Maybe ignore the rule for the entire file (IIRC you add # ruff: noqa: B015 at the top of the file)
|
|
||
|
|
||
| def check_testcase_implements_trial_reporter(done: List[int] = []) -> None: | ||
| def check_testcase_implements_trial_reporter(done: List[int] = []) -> None: # noqa: B006 |
There was a problem hiding this comment.
Ooh this is pretty terrible, I suggest we refactor the code to avoid this hidden global state.
There was a problem hiding this comment.
This one I didn't change yet, I suppose the intent was to do this once instead of multiple time:
from twisted.trial.itrial import IReporter
from zope.interface import classImplements
classImplements(TestCaseFunction, IReporter)When launching
@hookimpl(wrapper=True)
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules:
ut: Any = sys.modules["twisted.python.failure"]
Failure__init__ = ut.Failure.__init__
check_testcase_implements_trial_reporter()I suppose it's a costly operation. Any ideas how to fix or should I just use a more "politically correct" global state ?
There was a problem hiding this comment.
Yes, let's at least not "hide" the global var in the parameter list, I think it's a bad pattern. Perhaps we will deal with the actual global sometime...
There was a problem hiding this comment.
So should I fix this in a subsequent merge request or in this one ?
There was a problem hiding this comment.
I prefer not to add noqa for real problems so either fix now (move done to a global var rather than a mutable default arg) or ignore B006 for the project and fix later.
There was a problem hiding this comment.
I used an existing global state from python (8c85144 / https://docs.python.org/3/library/functions.html#dir) let me know what you think.
>>> import sys
>>> "sys" in sys.modules
True
>>> "version_info" in dir()
False
>>> from sys import version_info
>>> "version_info" in dir()
TrueThere was a problem hiding this comment.
I might be misunderstand how dir works but are sure "classImplements" not in dir() is not always false at the point it is called?
There was a problem hiding this comment.
Right..
import sys
def in_function_scope():
if not "version_info" in dir():
from sys import version_info
print(f"Imported version_info")
else:
print("version_info already imported")
in_function_scope()
in_function_scope()=>
Imported version_info
Imported version_info
I'm going to just use a global
There was a problem hiding this comment.
import sys
classImplements_has_run = False
def in_function_scope():
global classImplements_has_run
if not classImplements_has_run:
from sys import version_info
print(f"Imported version_info")
classImplements_has_run = True
else:
print("version_info already imported")
in_function_scope()
in_function_scope()Imported version_info
version_info already imported
0140b16 to
4651f85
Compare
6f46cdf to
da412fc
Compare
da412fc to
e193a26
Compare
8c85144 to
3101c02
Compare
Follow-up to #11912 . The message raised by flake8bugbear were very often false positives. Maybe not such a good linter for the pytest team after all ? Or maybe it can catch issues that took time to fix in the past, as the codebase is of very high quality because it's old and battle tested ?