Fix pytest-parametrize-names-wrong-type (PT006) to edit both argnames and argvalues if both of them are single-element tuples/lists#14699
Conversation
|
|
I'm sure you're right, but can you expand on why that's a problem? (Should we not be raising a diagnostic at all?) |
|
@charliermarsh Thanks for the comment!
This is another option. |
|
I just found #11243. Should we unpack each item in |
|
In this case, would this be correct? With the comma inside the string? I don't know enough about pytest. |
|
@charliermarsh Let me check. import pytest
@pytest.mark.parametrize("x,", [(1,), (2,)])
def test_foo(x):
assert isinstance(x, int)
|
|
Source of the relevant pytest code According to the rule. How should a user rewrite your example? |
@pytest.mark.parametrize(("x",), [(1,), (2,)])
def test_foo(x):
...should be rewritten to: @pytest.mark.parametrize("x", [1, 2])
def test_foo(x):
...
# I'd prefer this style because I don't need to wonder what x is (tuple vs. int)Both (I implemented a fix to fix |
pytest-parametrize-names-wrong-type (PT006) not to suggest a fix if both argnames and argvalues are single-element sequencespytest-parametrize-names-wrong-type (PT006) to edit argnames and argvalues as a fix
pytest-parametrize-names-wrong-type (PT006) to edit argnames and argvalues as a fixpytest-parametrize-names-wrong-type (PT006) to edit both argnames and argvalues if both of them are single-element tuples/lists
That sounds great. I'll have a look when you push the change |
crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs
Outdated
Show resolved
Hide resolved
| // def test_foo(x): | ||
| // assert isinstance(x, int) # fails because `x` is a tuple, not an int | ||
| // ``` | ||
| unpack_single_element_items(checker, argvalues), |
There was a problem hiding this comment.
Should I add a test to ensure this doesn't conflict with PT007 which may also edit argvalues?
There was a problem hiding this comment.
This sounds like a great idea if we're concerned about whether the rules play nicely together.
There was a problem hiding this comment.
@MichaReiser Manually confirmed PT006 and PT007 don't conflicts:
> cargo run -p ruff -- check --no-cache --select PT007,PT006 a.py --unsafe-fixes --fix
--- a.py
+++ a.py
@@ -1,6 +1,6 @@
import pytest
-@pytest.mark.parametrize(("param",), [[1], [2], [3]])
+@pytest.mark.parametrize("param", [1, 2, 3])
def test_foo(param):
...
Would fix 1 error.There was a problem hiding this comment.
Added a test case for this.
| // def test_foo(x): | ||
| // assert isinstance(x, int) # fails because `x` is a tuple, not an int | ||
| // ``` | ||
| unpack_single_element_items(checker, argvalues), |
There was a problem hiding this comment.
This sounds like a great idea if we're concerned about whether the rules play nicely together.
crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs
Outdated
Show resolved
Hide resolved
MichaReiser
left a comment
There was a problem hiding this comment.
This is great. Thank you for following it thourgh
Summary
Close #11243. Fix
pytest-parametrize-names-wrong-type (PT006)to edit bothargnamesandargvaluesif both of them are single-element tuples/lists.Test Plan
New test cases