Skip to content
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

Warn when a mark is applied to a fixture #8428

Merged

Conversation

graingert
Copy link
Member

Fixes #3664

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

Seems like a good idea to warn (and eventually error) on this. The code LGTM, with some comments.

Also, for deprecations doc/en/deprecations.rst also needs to be updated.

@@ -95,6 +95,8 @@
"see https://docs.pytest.org/en/latest/deprecations.html#node-fspath-in-favor-of-pathlib-and-node-path",
)

MARKED_FIXTURE = PytestDeprecationWarning("Marks cannot be applied to fixtures")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe make this message more informative, e.g. "Marks applied to fixture functions have no effect. Use XYZ instead."

Copy link
Member Author

Choose a reason for hiding this comment

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

there's not always a clean option, eg sometimes you want item.add_marker( sometimes you want to use @pytest.fixture(params=[pytest.param(..., marks=...)]) and sometimes you want to add an explicit fixture dependency

Copy link
Member

Choose a reason for hiding this comment

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

Right, this is probably too much for the warning itself, but maybe in the deprecation doc, basically what you wrote above would be helpful I think. But OK if not.

src/_pytest/mark/structures.py Outdated Show resolved Hide resolved
changelog/3664.deprecation.rst Outdated Show resolved Hide resolved
@graingert graingert force-pushed the warn-when-a-mark-is-applied-to-a-fixture branch from 3a439db to a7e0ae2 Compare March 19, 2021 09:44
@graingert graingert requested a review from bluetech March 19, 2021 09:47
@1Mark
Copy link

1Mark commented Jul 8, 2022

any progress here?

doc/en/deprecations.rst Outdated Show resolved Hide resolved
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks @graingert ?

testing/python/fixtures.py Outdated Show resolved Hide resolved
changelog/3664.deprecation.rst Outdated Show resolved Hide resolved
doc/en/deprecations.rst Outdated Show resolved Hide resolved
src/_pytest/deprecated.py Outdated Show resolved Hide resolved
src/_pytest/mark/structures.py Show resolved Hide resolved
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

This looks great, and I'm keen to get it in soon - rebase and apply the minor comments below?

doc/en/how-to/fixtures.rst Outdated Show resolved Hide resolved
src/_pytest/deprecated.py Outdated Show resolved Hide resolved
testing/deprecated_test.py Outdated Show resolved Hide resolved
testing/deprecated_test.py Outdated Show resolved Hide resolved
testing/deprecated_test.py Outdated Show resolved Hide resolved
doc/en/deprecations.rst Outdated Show resolved Hide resolved
doc/en/deprecations.rst Outdated Show resolved Hide resolved
graingert and others added 2 commits June 25, 2023 16:08
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
@graingert graingert requested a review from Zac-HD June 25, 2023 15:09
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Thanks, @graingert! Looking forward to seeing how many people hit this new warning...

@Zac-HD Zac-HD dismissed stale reviews from nicoddemus and bluetech June 25, 2023 17:21

Requested changes implemented

@Zac-HD Zac-HD merged commit 5eaf820 into pytest-dev:main Jun 25, 2023
@graingert graingert deleted the warn-when-a-mark-is-applied-to-a-fixture branch June 25, 2023 17:26
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.

Generate an error when a mark is applied to a fixture
5 participants