-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Seeking improved pytest rules, as flake8-pytest-style
doesn't align with best practices
#8796
Comments
As a user who just started adding ruff to his (somewhat big) codebase I also added config to change the weird defaults WRT parentheses because the defaults were quite weird! Just some thoughts on your points:
|
Agreed, except:
|
in that case there should also be a suggestion to drop |
perhaps it makes sense to trigger the pytest migration rules based on whether a pytest import is in a test file, a very neat but expensive way would be to have "migraton" rules/fixes only apply when the line is currently being changed anyway |
I would not like that in my own project; It leaves an inconsistent mess and makes PRs harder to review because now they suddenly contain no longer just the main change they are about but also 'infrastructure changes'. I prefer a separate PR for those (which can be more easily skimmed over during review)
but many test files don't contain any pytest imports if they don't use parametrization... |
I do not use flake8-pytest-style anyway, but. I did notice that it is recommended at https://docs.pytest.org/en/stable/explanation/goodpractices.html#checking-with-flake8-pytest-style Should it be removed? |
Good point It should |
FWIW, I have no problem with shipping better pytest rules (though I haven't used pytest extensively so don't feel qualified to have good opinions on the rules themselves). In practice, it may be easiest to ship them under an entirely new pytest prefix / rule set, and mark the |
I'll coordinate with pytest-core to get together a official set |
FYI until such time that there is an official set, for Hatch's new |
PT001, PT023 styles are configurable by https://docs.astral.sh/ruff/settings/#flake8-pytest-style-fixture-parentheses and https://docs.astral.sh/ruff/settings/#flake8-pytest-style-mark-parentheses |
flake8-pytest-style
doesn't align with best practices
See reson here: astral-sh/ruff#8796 (comment)
Would it be possible to disable the most offending rules by default for the time being? |
If you would prefer to have a large default selection of "good" rules, you can use Hatch https://hatch.pypa.io/latest/config/static-analysis/ The undesirable ones that you speak of here are excluded by default. |
I understand to a large extent what you mean. But the way I see it is that there is a difference between the possibilities of a programming language, a (testing) framework, or a library (API), and the agreements a team wants to make about a uniform usage of those assets. As an example, I can understand that a team agrees to always use a decorator with a set of parentheses, even when there are no additional parameters to provide in a particular case, or that a team always uses the list (or tuple) form in specifying the parameter names in OTOH, I didn't dive in all the particular rules, but advocating BTW, I think a know where these two rules come from: my line of reasoning is as follows:
I assume that this might be the line of reasoning behind Unfortunately the rule is implemented in a wrong way. Instead of checking the name of the decorated function, the rule should check the name of the fixture. This could be the name of the function, but can also be different if the |
@charliermarsh FYI the latest version of flake8-pytest-style (v2.0.0), which was shipped on april 1st, inverted the defaults for PT001 and PT023 so that they now conform with pytest recommendations. Maybe ruff should push out a similar change soon? |
If you do establish a new set of rules around pytest. A rule that warns users on the use of the |
## Summary This patch inverts the defaults for [pytest-fixture-incorrect-parentheses-style (PT001)](https://docs.astral.sh/ruff/rules/pytest-fixture-incorrect-parentheses-style/) and [pytest-incorrect-mark-parentheses-style (PT003)](https://docs.astral.sh/ruff/rules/pytest-incorrect-mark-parentheses-style/) to prefer dropping superfluous parentheses. Presently, Ruff defaults to adding superfluous parentheses on pytest mark and fixture decorators for documented purpose of consistency; for example, ```diff import pytest [email protected] [email protected]() def test_bar(): ... ``` This behaviour is counter to the official pytest recommendation and diverges from the flake8-pytest-style plugin as of version 2.0.0 (see m-burst/flake8-pytest-style#272). Seeing as either default satisfies the documented benefit of consistency across a codebase, it makes sense to change the behaviour to be consistent with pytest and the flake8 plugin as well. This change is breaking, so is gated behind preview (at least under my understanding of Ruff versioning). The implementation of this gating feature is a bit hacky, but seemed to be the least disruptive solution without performing invasive surgery on the `#[option()]` macro. Related to #8796. ### Caveat Whilst updating the documentation, I sought to reference the pytest recommendation to drop superfluous parentheses, but couldn't find any official instruction beyond it being a revealed preference within the pytest documentation code examples (as well as the linked issues from a core pytest developer). Thus, the wording of the preference is deliberately timid; it's to cohere with pytest rather than follow an explicit guidance. ## Test Plan `cargo nextest run` I also ran ```sh cargo run -p ruff -- check crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT001.py --no-cache --diff --select PT001 ``` and compared against it with `--preview` to verify that the default does change under preview (I also repeated this with `echo '[tool.ruff]\npreview = true' > pyproject.toml` to verify that it works with a configuration file). --------- Co-authored-by: Charlie Marsh <[email protected]>
If you are using pants (pantsbuild.org) then rule PT004 and PT005 will really hurt. We had a session scoped fixture which for changed from |
@charliermarsh based on this detail i think a release that declares them unsafe or removes them ougth to be expedited |
@RonnyPfannschmidt -- Neither of these rules have a fix though -- those changes must've been applied manually by the author. |
I'd be fine to deprecate PT004 and PT005 but it will have to wait until the next minor release (we don't deprecate rules in patch releases IIRC). |
Could also a rule be added. I have sometimes seen a problem with |
Current plan is to deprecate PT004 and PT005 in the upcoming v0.6. |
The upcoming 0.6 release deprecates PT004 and PT005 and changes the defaults for PT001 and PT023. |
hi everyone,
before i evaluated ruff i wasn't even aware of flake8-pytest-styles,
as one of the pytest maintainers i'm particularly annoyed as the rules don't reflect documented and communicated best practices and the upstream doesn't seem likely to change those details
PT001
should be reversed (drop brackets)PT004
is simply incorrect - public no return value fixtures should have public names for both usage in the usefixtures maker and as dependencies in other fixturesPT005
is equally incorrect - internal fixtures return values even if underscores are used - sabotage of that is not okPT006
undoes the intentionally established pattern of being able to comma separate multiple names in a single stringPT009
sabotages intentional usage of unittest (which is sometimes necessary)PT021
sabotages well established patterns for complex fixtures that setup in phases where on needs to ensure teardown but ought not to split them into multiple smaller fixturesPT023
should drop parens on marks by defaultPT024/25
should warn on any mark on a fixture as no marks are supported on fixturesPT027
also sabotages legitimate usage of unittest patternsThe text was updated successfully, but these errors were encountered: