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 all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Semantic versioning in our case means:

## 1.1.0 WIP

### Rules

- Adds `WPS476`: do not allow `TypeVarTuple` after a `TypeVar` with a default #3265

### Command line utility

This version introduces `wps` CLI tool.
Expand Down
539 changes: 321 additions & 218 deletions poetry.lock

Large diffs are not rendered by default.

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."""
57 changes: 46 additions & 11 deletions tests/test_checker/test_noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import subprocess
import types
from collections import Counter
from typing import Final

import pytest

from wemake_python_styleguide.compat.constants import PY313

#: Used to find violations' codes in output.
ERROR_PATTERN = re.compile(r'(WPS\d{3})')

Expand All @@ -33,6 +36,9 @@
'WPS402', # we obviously use a lot of `noqa` comments
)

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

#: Number and count of violations that would be raised.
SHOULD_BE_RAISED = types.MappingProxyType(
{
Expand Down Expand Up @@ -234,6 +240,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 +299,11 @@
},
)

#: 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}
)


def _assert_errors_count_in_output(
output,
Expand Down Expand Up @@ -320,19 +332,22 @@ def test_codes(all_violations):
assert len(SHOULD_BE_RAISED) == len(all_violations)


ALWAYS: Final = True # just for beautiful condition def


@pytest.mark.parametrize(
('filename', 'violations'),
('filename', 'violations', 'run_condition'),
[
('noqa.py', SHOULD_BE_RAISED),
('noqa.py', SHOULD_BE_RAISED, ALWAYS),
('noqa313.py', SHOULD_BE_RAISED3_13, PY313),
],
)
def test_noqa_fixture_disabled(
absolute_path,
all_violations,
filename,
violations,
absolute_path, all_violations, filename, violations, run_condition
):
"""End-to-End test to check that all violations are present."""
if not run_condition: # pragma: no cover
return
process = subprocess.Popen(
[
'flake8',
Expand Down Expand Up @@ -360,8 +375,17 @@ def test_noqa_fixture_disabled(
)


def test_noqa_fixture(absolute_path):
@pytest.mark.parametrize(
('filename', 'run_condition'),
[
('noqa.py', ALWAYS),
('noqa313.py', PY313),
],
)
def test_noqa_fixture(absolute_path, filename, run_condition):
"""End-to-End test to check that `noqa` works."""
if not run_condition: # pragma: no cover
return
process = subprocess.Popen(
[
'flake8',
Expand All @@ -370,7 +394,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 +407,26 @@ 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', 'run_condition'),
[
('noqa.py', IGNORED_VIOLATIONS, ALWAYS),
('noqa313.py', IGNORED_VIOLATIONS3_13, PY313),
],
)
def test_noqa_fixture_without_ignore(
absolute_path, filename, ignored_violations, run_condition
):
"""End-to-End test to check that `noqa` works without ignores."""
if not run_condition: # pragma: no cover
return
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 +435,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