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?

…ssage from using string concat to f-strings for more readability, and added a function in compat to grab default parameters and its values
@nicoddemus nicoddemus requested a review from The-Compiler March 1, 2025 15:09
msg = (
f"Test function '{function_name}' has a default argument '{param_name}={param_val}'.\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.

I still think this isn't a good idea, what's the rationale for it? With it, a

pytestwarnings =
    ...
    ignore::_pytest.warning_types.PytestDefaultArgumentWarning

in pytest's configuration would have no effect at all...

(Also, the type should probably be exposed as pytest.PytestDefaultArgumentWarning just like pytest.PytestUnknownMarkWarning is?

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