Fix pytest-parametrize-names-wrong-type (PT006) to edit both argnames and argvalues if both of them are single-element tuples/lists (#14699)

## Summary

Close #11243. Fix `pytest-parametrize-names-wrong-type (PT006)` to edit
both `argnames` and `argvalues` if both of them are single-element
tuples/lists.

```python
# Before fix
@pytest.mark.parametrize(("x",), [(1,), (2,)])
def test_foo(x):
    ...

# After fix:
@pytest.mark.parametrize("x", [1, 2])
def test_foo(x):
    ...
```

## Test Plan

New test cases
This commit is contained in:
Harutaka Kawamura 2024-12-09 17:58:52 +09:00 committed by GitHub
parent 8df4983057
commit 3d9ac535e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 901 additions and 16 deletions

View file

@ -81,3 +81,78 @@ def test_not_decorator(param1, param2):
@pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
def test_keyword_arguments(param1, param2):
...
@pytest.mark.parametrize(("param",), [(1,), (2,)])
def test_single_element_tuple(param):
...
@pytest.mark.parametrize(("param",), [[1], [2]])
def test_single_element_list(param):
...
@pytest.mark.parametrize(("param",), [[1], [2]])
def test_single_element_list(param):
...
# Unsafe fix
@pytest.mark.parametrize(
(
# comment
"param",
),
[[1], [2]],
)
def test_comment_in_argnames(param):
...
# Unsafe fix
@pytest.mark.parametrize(
("param",),
[
(
# comment
1,
),
(2,),
],
)
def test_comment_in_argvalues(param):
...
# Safe fix
@pytest.mark.parametrize(
("param",),
[
(1,),
# comment
(2,),
],
)
def test_comment_between_argvalues_items(param):
...
# A fix should be suggested for `argnames`, but not for `argvalues`.
@pytest.mark.parametrize(
("param",),
[
(1,),
(2, 3),
],
)
def test_invalid_argvalues(param):
"""
pytest throws the following error for this test:
------------------------------------------------
a.py::test_comment_between_argvalues_items: in "parametrize" the number of names (1):
('param',)
must be equal to the number of values (2):
(2, 3)
------------------------------------------------
"""
...

View file

@ -0,0 +1,5 @@
import pytest
@pytest.mark.parametrize(("param",), [[1], [2]])
def test_PT006_and_PT007_do_not_conflict(param):
...