Skip to content

[ruff] Activate flake8-bugbear and flake8-pyi#11914

Merged
Pierre-Sassoulas merged 9 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:activate-flake8-bugbear-and-pyi
Feb 5, 2024
Merged

[ruff] Activate flake8-bugbear and flake8-pyi#11914
Pierre-Sassoulas merged 9 commits into
pytest-dev:mainfrom
Pierre-Sassoulas:activate-flake8-bugbear-and-pyi

Conversation

@Pierre-Sassoulas

@Pierre-Sassoulas Pierre-Sassoulas commented Feb 2, 2024

Copy link
Copy Markdown
Member

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 ?

@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the activate-flake8-bugbear-and-pyi branch from 6868db7 to 0140b16 Compare February 2, 2024 20:44
@Pierre-Sassoulas
Pierre-Sassoulas marked this pull request as draft February 2, 2024 20:52

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/_pytest/_py/error.py Outdated
Comment thread testing/_py/test_local.py Outdated
Comment thread testing/python/approx.py Outdated
# Negative tolerances are not allowed.
with pytest.raises(ValueError):
1.1 == approx(1, rel, abs)
1.1 == approx(1, rel, abs) # noqa: B015

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ignore the rule for the entire file (IIRC you add # ruff: noqa: B015 at the top of the file)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread pyproject.toml Outdated
Comment thread testing/test_warnings.py Outdated
Comment thread src/_pytest/unittest.py Outdated


def check_testcase_implements_trial_reporter(done: List[int] = []) -> None:
def check_testcase_implements_trial_reporter(done: List[int] = []) -> None: # noqa: B006

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh this is pretty terrible, I suggest we refactor the code to avoid this hidden global state.

@Pierre-Sassoulas Pierre-Sassoulas Feb 4, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I fix this in a subsequent merge request or in this one ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()
True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misunderstand how dir works but are sure "classImplements" not in dir() is not always false at the point it is called?

@Pierre-Sassoulas Pierre-Sassoulas Feb 4, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread testing/python/raises.py Outdated
Comment thread pyproject.toml
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the activate-flake8-bugbear-and-pyi branch from 0140b16 to 4651f85 Compare February 4, 2024 10:53
@Pierre-Sassoulas
Pierre-Sassoulas marked this pull request as ready for review February 4, 2024 13:02
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the activate-flake8-bugbear-and-pyi branch 2 times, most recently from 6f46cdf to da412fc Compare February 4, 2024 15:38
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the activate-flake8-bugbear-and-pyi branch from da412fc to e193a26 Compare February 4, 2024 18:27
@Pierre-Sassoulas
Pierre-Sassoulas force-pushed the activate-flake8-bugbear-and-pyi branch from 8c85144 to 3101c02 Compare February 4, 2024 22:08
@Pierre-Sassoulas
Pierre-Sassoulas merged commit 0d1f4c6 into pytest-dev:main Feb 5, 2024
@Pierre-Sassoulas
Pierre-Sassoulas deleted the activate-flake8-bugbear-and-pyi branch February 5, 2024 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants