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

PT006 issue not raised when keyword arguments are used instead of positional arguments. #15324

Closed
caarmen opened this issue Jan 7, 2025 · 1 comment · Fixed by #15327
Closed
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule

Comments

@caarmen
Copy link

caarmen commented Jan 7, 2025

Summary

For pytest checks:

When argnames and argvalues are passed as keyword arguments to pytest.mark.parametrize, ruff doesn't raise PT006 where it would otherwise (for passing a list instead of a single value).

Details

Steps to reproduce the issue:

  • Create a test filetest_something.py, containing two test functions.
    Note that the test functions only expect a single parametrized argument.

    In both cases, the parametrize() decorator is called with a list for argnames and a list of list for argvalues. The difference is that the first one uses positional arguments, and the second one uses keyword arguments.

    import pytest
     
     
    @pytest.mark.parametrize(
        ["contact_type"],
        [["email"], ["phone"]],
    )   
    def test_function_issue_caught_PT006(contact_type: str):...
     
    @pytest.mark.parametrize(
        argnames=["contact_type"],
        argvalues=[["email"], ["phone"]],
    )   
    def test_function_issue_uncaught_PT006(contact_type: str):...
  • Run ruff explicitly looking for the PT006 issue:

    $ ruff check test_something.py  --select PT006
  • Expected behavior (I think, you'll confirm 😅 ):

    • Ruff raises an issue PT006 with the same message for both test functions: "Use a string for the first argument".
  • Actual behavior:

    • Ruff raises an issue only for the first test function (the one with positional arguments):
      $ ruff check test_something.py  --select PT006
      test_something.py:5:5: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
        | 
      4 | @pytest.mark.parametrize(
      5 |     ["contact_type"],
        |     ^^^^^^^^^^^^^^^^ PT006
      6 |     [["email"], ["phone"]],
      7 | )
        | 
        = help: Use a string for the first argument
@caarmen caarmen changed the title PT006 doesn't raise issues when keyword arguments are used. PT006 issue not raised when keyword arguments are used instead of positional arguments. Jan 7, 2025
@InSyncWithFoo
Copy link
Contributor

InSyncWithFoo commented Jan 7, 2025

Currently keyword arguments are only detected in preview mode:

(from flake8_pytest_style/rules/parametrize.rs)

let names = if checker.settings.preview.is_enabled() {
    call.arguments.find_argument_value("argnames", 0)
} else {
    call.arguments.find_positional(0)
};
let values = if checker.settings.preview.is_enabled() {
    call.arguments.find_argument_value("argvalues", 1)
} else {
    call.arguments.find_positional(1)
};

These were added in #14699. The corresponding issue #11243 doesn't have any relevant information, so the reason was probably backward compatibility.

Perhaps it's safe to remove these preview checks; I think this is by no mean a breaking change, even when PT006 is stable.

@dhruvmanila dhruvmanila added rule Implementing or modifying a lint rule preview Related to preview mode features labels Jan 8, 2025
MichaReiser pushed a commit that referenced this issue Jan 8, 2025
…ze` calls" (`PT006`) (#15327)

Co-authored-by: Micha Reiser <[email protected]>
Resolves #15324. Stabilizes the behavior changes introduced in #14515.
MichaReiser pushed a commit that referenced this issue Jan 8, 2025
…ze` calls" (`PT006`) (#15327)

Co-authored-by: Micha Reiser <[email protected]>
Resolves #15324. Stabilizes the behavior changes introduced in #14515.
MichaReiser pushed a commit that referenced this issue Jan 8, 2025
…ze` calls" (`PT006`) (#15327)

Co-authored-by: Micha Reiser <[email protected]>
Resolves #15324. Stabilizes the behavior changes introduced in #14515.
MichaReiser pushed a commit that referenced this issue Jan 9, 2025
…ze` calls" (`PT006`) (#15327)

Co-authored-by: Micha Reiser <[email protected]>
Resolves #15324. Stabilizes the behavior changes introduced in #14515.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
4 participants