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

Fix for warning test arguments with default values #13044

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

huangbenny
Copy link

+ str(param.default)
+ "'.\n"
)
warnings.simplefilter("always", PytestDefaultArgumentWarning)
Copy link
Member

Choose a reason for hiding this comment

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

Is adding a preexisting filter again a noop? Else this is a problem

Copy link
Member

Choose a reason for hiding this comment

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

Even if it is, it seems odd for pytest to programmatically override the warning filters on its own. What's the reason for this?

Copy link
Author

Choose a reason for hiding this comment

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

Previously without this filter, the warnings would include a trackback to the line that made the warning. This is not the desired behavior in my opinion because I intended for this warning to help the user pinpoint the line number for the function that used the default parameters.

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 for getting the ball rolling! I added a couple of comments, and this will also need a test somewhere.

@@ -143,9 +144,29 @@ def async_fail(nodeid: str) -> None:
fail(msg, pytrace=False)


def async_default_arg_warn(nodeid: str, function_name, param) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Why async_? What's async about it?

Copy link
Author

Choose a reason for hiding this comment

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

I agree that I shouldn't have added async_ to the function name. I'll update that.

@@ -143,9 +144,29 @@ def async_fail(nodeid: str) -> None:
fail(msg, pytrace=False)


def async_default_arg_warn(nodeid: str, function_name, param) -> None:
msg = (
Copy link
Member

Choose a reason for hiding this comment

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

This would be much more readable with f-strings rather than string concatenation.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good, I'll change it to f-strings for more readability.

+ str(param.default)
+ "'.\n"
)
warnings.simplefilter("always", PytestDefaultArgumentWarning)
Copy link
Member

Choose a reason for hiding this comment

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

Even if it is, it seems odd for pytest to programmatically override the warning filters on its own. What's the reason for this?

warnings.simplefilter("always", PytestDefaultArgumentWarning)
warnings.warn(PytestDefaultArgumentWarning(msg))


@hookimpl(trylast=True)
def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
Copy link
Member

Choose a reason for hiding this comment

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

I'm not entirely convinced this is the right place for this. I wonder if something like FixtureManager.getfixtureinfo (where we already take care of reading the function's arguments) would make more sense maybe?

Copy link
Author

Choose a reason for hiding this comment

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

In my most recent commit, I updated the warning checks to be in the getfixtureinfo. After looking into the function more, I also thought that it would be better to include the checks in that function.

…ssage from using string concat to f-strings for more readability, and added a function in compat to grab default parameters and its values
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.

Warn on test arguments with default values?
4 participants