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

Give warning when test function return other than None #9956

Merged
merged 16 commits into from
May 25, 2022

Conversation

Cheukting
Copy link
Contributor

@Cheukting Cheukting commented May 13, 2022

Adding a warning in pytest_pyfunc_call after the async_warn_and_skip will check if the return is something other than None.

Closes #7337; see that issue for details.

CC @Zac-HD

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.

Looks good to me - if you can confirm that this doesn't trigger when used with pytest-twisted, I'll be happy to merge 🥳

src/_pytest/warning_types.py Outdated Show resolved Hide resolved
src/_pytest/python.py Outdated Show resolved Hide resolved
changelog/7337.improvement.rst Outdated Show resolved Hide resolved
@Cheukting
Copy link
Contributor Author

Looks good to me - if you can confirm that this doesn't trigger when used with pytest-twisted, I'll be happy to merge 🥳

I saw there is a test file pytest_twisted_integration.py. Does it test pytest-twisted? Or do I have to create new tests?

@Cheukting Cheukting changed the title [WIP] Give warning when test function return other than None Give warning when test function return other than None May 13, 2022
@Zac-HD
Copy link
Member

Zac-HD commented May 13, 2022

Looks good to me - if you can confirm that this doesn't trigger when used with pytest-twisted, I'll be happy to merge 🥳

I saw there is a test file pytest_twisted_integration.py. Does it test pytest-twisted? Or do I have to create new tests?

Without looking, I'd have gone and installed pytest-twisted, ran the inlineCallbacks and ensureDeferred examples to check that they work with the main branch and also (without warnings) on this PR. It looks like that test file does pretty much exactly that though, so if it's passing we're all good 💪

@Cheukting
Copy link
Contributor Author

Cheukting commented May 13, 2022

Seems fnmatch_lines works differently in Python3.7 and Python 3.8, This one https://github.com/pytest-dev/pytest/runs/6429987533?check_suite_focus=true#step:5:85 fails at 3.8 but passes at 3.7

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.

Looks good to me! I'll give others a few days to review, but inclined to merge if I don't hear any objections.

Thanks for the patch, Cheuk 🤩

Copy link
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

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

Thanks @Cheukting, that seems quite useful! There's a bit of documentation and an addition to the public API missing - after that, this seems good to merge to me!

src/_pytest/python.py Outdated Show resolved Hide resolved
@@ -55,6 +55,13 @@ class PytestRemovedIn8Warning(PytestDeprecationWarning):
__module__ = "pytest"


@final
class PytestReturnNotNoneWarning(PytestDeprecationWarning):
Copy link
Member

Choose a reason for hiding this comment

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

This should be imported in src/pytest/__init__.py and added to __all__ there, since we want e.g. people to be able to ignore it via their config without having to import private API.

It should also be added to doc/en/reference/reference.rst with the other warnings, and probably get a new entry in doc/en/deprecations.rst.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review, just want to be clear about the deprecations. Since we are already in 7, shall it be deprecated in 8? Or should I say 7?

Copy link
Member

Choose a reason for hiding this comment

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

This should be a warning, not a deprecation warning

Copy link
Member

Choose a reason for hiding this comment

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

Should it, if we plan to turn it into an error in the future?

Copy link
Member

@nicoddemus nicoddemus May 25, 2022

Choose a reason for hiding this comment

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

Deprecation warning is correct if we want to turn it into an error in the future, which seems the plan for unittest in the stdlib too: #7337 (comment).

python/cpython#27748

src/_pytest/python.py Outdated Show resolved Hide resolved
src/_pytest/python.py Outdated Show resolved Hide resolved
changelog/7337.improvement.rst Outdated Show resolved Hide resolved
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 a lot @Cheukting for the contribution! 👍

Copy link
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

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

I'm always a bit confused about how deprecations work in pytest - I feel like we should document this better somewhere... @nicoddemus @bluetech @RonnyPfannschmidt am I correct with my comment? Should PytestReturnNotNoneWarning really be its own class, or should it be an instance of PytestRemovedIn8Warning (or PytestRemovedIn9Warning even?) like some others in src/_pytest/deprecated.py?

Apologies for the delay and somewhat bumpy road @Cheukting!

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

It should be a normal warning

The new class should be a subclass of user warning

@RonnyPfannschmidt
Copy link
Member

Also, good work, I'd love to get it in

@Zac-HD
Copy link
Member

Zac-HD commented May 20, 2022

It should be a normal warning

The new class should be a subclass of user warning

I disagree; IMO this should be a deprecation warning. I'd like to make it an error in future, and it seems like CPython is also going that way with a deprecation warning in python/cpython#27748 (for 3.11).

@RonnyPfannschmidt
Copy link
Member

in that case, lets go for Removed in pytest 8

Co-authored-by: Florian Bruhin <[email protected]>
Copy link
Member

@The-Compiler The-Compiler left a comment

Choose a reason for hiding this comment

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

This seems ready now - @RonnyPfannschmidt @nicoddemus?

@nicoddemus nicoddemus merged commit c988e49 into pytest-dev:main May 25, 2022
@graingert
Copy link
Member

in that case, lets go for Removed in pytest 8

NotNoneWarning should subclass RemovedIn8Warning https://github.com/pytest-dev/pytest/pull/10012/files#diff-a7738833b05551853edda7933c165b6d7a41d521afb130fdc68631bd9129531eR59

@graingert
Copy link
Member

in that case, lets go for Removed in pytest 8

It's not possible to subclass RemovedIn8Warning because it's decorated with @final

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.

Test functions that return non-None should raise a warning/error
6 participants