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

Issue 3265: Add WPS476 SneakyTypeVarWithDefaultViolation #3314

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e4c5c98
Resolve #3265: add WPS476 TypeVarTupleFollowsTypeVarWithDefaultViolat…
Tapeline Feb 10, 2025
632171d
Refactoring: split classes.py into submodules. Change naming
Tapeline Feb 20, 2025
8b2c65a
Merge branch 'master' into issue-3265
Tapeline Feb 20, 2025
4f3bd91
Transfer changes from classes.py to classes/methods.py
Tapeline Feb 20, 2025
c48d615
Update CHANGELOG.md
Tapeline Feb 20, 2025
57530d2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 20, 2025
48ce42e
Fix WPS linting and CI build issues
Tapeline Feb 20, 2025
f016624
Merge remote-tracking branch 'origin/issue-3265' into issue-3265
Tapeline Feb 20, 2025
5a03e99
Fix WPS linting and CI build issues
Tapeline Feb 20, 2025
990c9de
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 20, 2025
e12ae40
Fix CI build issues and compatibility issues
Tapeline Feb 20, 2025
3061b70
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 20, 2025
3f6e072
Fix CI build issues and compatibility issues
Tapeline Feb 20, 2025
41c7e05
Merge remote-tracking branch 'origin/issue-3265' into issue-3265
Tapeline Feb 20, 2025
b31d45a
Change rule behaviour: support only 3.13+ typevars. Change tests
Tapeline Feb 27, 2025
06b55b4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2025
cc68e6b
Add python versioning conditions on tests
Tapeline Feb 27, 2025
f5b0eea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2025
ef34b32
Adjust test skipping
Tapeline Feb 27, 2025
082b819
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
539 changes: 321 additions & 218 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions tests/fixtures/noqa/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,17 +747,3 @@ def my_function():

def pos_only_problem(first_argpm=0, second_argpm=1, /): # noqa: WPS475
my_print(first_argpm, second_argpm)


TypeVarDefault = TypeVar("T", default=int)
FollowingTuple = TypeVarTuple("Ts")


class NewStyleGenerics[TypeVarDefault, *FollowingTuple]: # noqa: WPS476
...


class OldStyleGenerics(
Generic[TypeVarDefault, *FollowingTuple] # noqa: WPS476
):
...
11 changes: 11 additions & 0 deletions tests/fixtures/noqa/noqa313.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
This file contains all possible violations for python 3.13+.

It is used for e2e tests.
"""

class NewStyleGenerics[
TypeVarDefault=int,
*FollowingTuple=*tuple[int, ...] # noqa: WPS476
]:
"""TypeVarTuple follows a defaulted TypeVar."""
40 changes: 35 additions & 5 deletions tests/test_checker/test_noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
'WPS402', # we obviously use a lot of `noqa` comments
)

#: List of ignored violations on python 3.13+.
IGNORED_VIOLATIONS3_13 = (

)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
IGNORED_VIOLATIONS3_13 = (
)
IGNORED_VIOLATIONS3_13 = ()


#: Number and count of violations that would be raised.
SHOULD_BE_RAISED = types.MappingProxyType(
{
Expand Down Expand Up @@ -234,6 +239,7 @@
'WPS473': 0,
'WPS474': 1,
'WPS475': 1,
'WPS476': 0, # enabled only in python 3.13+
'WPS500': 1,
'WPS501': 1,
'WPS502': 0, # disabled since 1.0.0
Expand Down Expand Up @@ -292,6 +298,13 @@
},
)

#: Number and count of violations that would be raised in 3.13+ section.
SHOULD_BE_RAISED3_13 = types.MappingProxyType(
dict.fromkeys(SHOULD_BE_RAISED, 0) | {
'WPS476': 1
}
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
SHOULD_BE_RAISED3_13 = types.MappingProxyType(
dict.fromkeys(SHOULD_BE_RAISED, 0) | {
'WPS476': 1
}
)
SHOULD_BE_RAISED3_13 = types.MappingProxyType(
{
'WPS476': 1
}
)

Let's simplify this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Testcases check that every violation code is defined in such dictionary, so I was forced to add that line. Formatter ate my comment on this, btw

Copy link
Member

Choose a reason for hiding this comment

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



def _assert_errors_count_in_output(
output,
Expand Down Expand Up @@ -324,6 +337,7 @@ def test_codes(all_violations):
('filename', 'violations'),
[
('noqa.py', SHOULD_BE_RAISED),
('noqa313.py', SHOULD_BE_RAISED3_13),
],
)
def test_noqa_fixture_disabled(
Expand Down Expand Up @@ -360,7 +374,14 @@ def test_noqa_fixture_disabled(
)


def test_noqa_fixture(absolute_path):
@pytest.mark.parametrize(
'filename',
[
'noqa.py',
'noqa313.py',
]
)
def test_noqa_fixture(absolute_path, filename):
"""End-to-End test to check that `noqa` works."""
process = subprocess.Popen(
[
Expand All @@ -370,7 +391,7 @@ def test_noqa_fixture(absolute_path):
'--isolated',
'--select',
'WPS, E999',
absolute_path('fixtures', 'noqa', 'noqa.py'),
absolute_path('fixtures', 'noqa', filename),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand All @@ -383,15 +404,24 @@ def test_noqa_fixture(absolute_path):
assert not stderr.count('WPS')


def test_noqa_fixture_without_ignore(absolute_path):
@pytest.mark.parametrize(
('filename', 'ignored_violations'),
[
('noqa.py', IGNORED_VIOLATIONS),
('noqa313.py', IGNORED_VIOLATIONS3_13),
]
)
def test_noqa_fixture_without_ignore(
absolute_path, filename, ignored_violations
):
"""End-to-End test to check that `noqa` works without ignores."""
process = subprocess.Popen(
[
'flake8',
'--isolated',
'--select',
'WPS, E999',
absolute_path('fixtures', 'noqa', 'noqa.py'),
absolute_path('fixtures', 'noqa', filename),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand All @@ -400,5 +430,5 @@ def test_noqa_fixture_without_ignore(absolute_path):
)
stdout, _ = process.communicate()

for violation in IGNORED_VIOLATIONS:
for violation in ignored_violations:
assert stdout.count(violation) > 0
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import BuiltinSubclassViolation
from wemake_python_styleguide.visitors.ast.classes import WrongClassDefVisitor
from wemake_python_styleguide.visitors.ast.classes.classdef import (
WrongClassDefVisitor,
)

class_with_base = 'class TestClass({0}): ...'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from wemake_python_styleguide.violations.best_practices import (
BaseExceptionSubclassViolation,
)
from wemake_python_styleguide.visitors.ast.classes import WrongClassDefVisitor
from wemake_python_styleguide.visitors.ast.classes.classdef import (
WrongClassDefVisitor,
)

class_with_base = """
class Meta({0}):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from wemake_python_styleguide.violations.best_practices import (
KwargsUnpackingInClassDefinitionViolation,
)
from wemake_python_styleguide.visitors.ast.classes import WrongClassDefVisitor
from wemake_python_styleguide.visitors.ast.classes.classdef import (
WrongClassDefVisitor,
)

# Wrong:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import WrongBaseClassViolation
from wemake_python_styleguide.visitors.ast.classes import WrongClassDefVisitor
from wemake_python_styleguide.visitors.ast.classes.classdef import (
WrongClassDefVisitor,
)

class_with_base = """
class Meta({0}):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import BuggySuperContextViolation
from wemake_python_styleguide.visitors.ast.classes import BuggySuperCallVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
BuggySuperCallVisitor,
)

error_dict_comprehension = """
{super().make_key(it): make_value(it) for it in items}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from wemake_python_styleguide.violations.oop import (
ShadowedClassAttributeViolation,
)
from wemake_python_styleguide.visitors.ast.classes import ClassAttributeVisitor
from wemake_python_styleguide.visitors.ast.classes.attributes import (
ClassAttributeVisitor,
)

# Can raise:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import WrongSlotsViolation
from wemake_python_styleguide.visitors.ast.classes import WrongSlotsVisitor
from wemake_python_styleguide.visitors.ast.classes.attributes import (
WrongSlotsVisitor,
)

class_body_template = """
class ClassWithSlots:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from wemake_python_styleguide.violations.oop import (
UnpythonicGetterSetterViolation,
)
from wemake_python_styleguide.visitors.ast.classes import WrongClassBodyVisitor
from wemake_python_styleguide.visitors.ast.classes.classdef import (
WrongClassBodyVisitor,
)

module_getter_and_setter = """
attribute = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from wemake_python_styleguide.violations.oop import (
LambdaAttributeAssignedViolation,
)
from wemake_python_styleguide.visitors.ast.classes import ClassAttributeVisitor
from wemake_python_styleguide.visitors.ast.classes.attributes import (
ClassAttributeVisitor,
)

# Correct:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from wemake_python_styleguide.violations.consistency import (
WrongMethodOrderViolation,
)
from wemake_python_styleguide.visitors.ast.classes import (
from wemake_python_styleguide.visitors.ast.classes.methods import (
ClassMethodOrderVisitor,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import AsyncMagicMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

sync_method = """
class Example:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import AsyncMagicMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

sync_method = """
class Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from wemake_python_styleguide.constants import MAGIC_METHODS_BLACKLIST
from wemake_python_styleguide.violations.oop import BadMagicMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

magic_method = """
class Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
MethodWithoutArgumentsViolation,
StaticMethodViolation,
)
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

method_inside_class = """
class Example:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import StaticMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

decorated_method = """
class Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import pytest

from wemake_python_styleguide.violations import oop
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

regular_method_detailed = """
class Useless:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from wemake_python_styleguide.violations.oop import YieldMagicMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
from wemake_python_styleguide.visitors.ast.classes.methods import (
WrongMethodVisitor,
)

method_template = """
class Test:
Expand Down
Loading
Loading