forked from wemake-services/wemake-python-styleguide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve wemake-services#3265: add WPS476 TypeVarTupleFollowsTypeVarWi…
…thDefaultViolation and corresponding tests
- Loading branch information
Showing
5 changed files
with
248 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
tests/test_visitors/test_ast/test_classes/test_type_var_with_default.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from typing import Final | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.violations.best_practices import ( | ||
TypeVarTupleFollowsTypeVarWithDefaultViolation, | ||
) | ||
from wemake_python_styleguide.visitors.ast.classes import ( | ||
ConsecutiveDefaultTypeVarsVisitor, | ||
) | ||
|
||
|
||
class_header_formats: Final[list[str]] = [ | ||
"Class[{0}]", | ||
"Class(Generic[{0}])" | ||
] | ||
various_code: Final[str] = ( | ||
"pi = 3.14\n" | ||
"a = obj.method_call()\n" | ||
"w, h = get_size()\n" | ||
"obj.field = function_call()\n" | ||
"AlmostTypeVar = NotReallyATypeVar()\n" | ||
"NonDefault = TypeVar('NonDefault')\n" | ||
) | ||
classes_with_various_bases: Final[str] = ( | ||
"class SimpleBase(object): ...\n" | ||
"class NotANameSubscript(Some.Class[object]): ...\n" | ||
"class NotAGenericBase(NotAGeneric[T]): ...\n" | ||
"class GenericButNotMultiple(Generic[T]): ...\n" | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"class_header_format", | ||
class_header_formats, | ||
) | ||
def test_type_var_tuple_after_type_var_with_default( | ||
assert_errors, | ||
parse_ast_tree, | ||
default_options, | ||
class_header_format, | ||
): | ||
"""Test that WPS476 works correctly.""" | ||
class_header = class_header_format.format("T, *Ts") | ||
src = ( | ||
various_code + | ||
classes_with_various_bases + | ||
"T = TypeVar('T', default=int)\n" | ||
"Ts = TypeVarTuple('Ts')\n" | ||
"\n" | ||
"class {0}:\n" | ||
" ..." | ||
).format(class_header) | ||
|
||
tree = parse_ast_tree(src) | ||
|
||
visitor = ConsecutiveDefaultTypeVarsVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [TypeVarTupleFollowsTypeVarWithDefaultViolation]) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"class_header_format", | ||
class_header_formats, | ||
) | ||
def test_type_var_tuple_after_type_var_without_default( | ||
assert_errors, | ||
parse_ast_tree, | ||
default_options, | ||
class_header_format, | ||
): | ||
"""Test that WPS476 ignores non-defaulted TypeVars.""" | ||
class_header = class_header_format.format("T, *Ts") | ||
src = ( | ||
various_code + | ||
classes_with_various_bases + | ||
"T = TypeVar('T')\n" | ||
"Ts = TypeVarTuple('Ts')\n" | ||
"\n" | ||
"class {0}:\n" | ||
" ..." | ||
).format(class_header) | ||
|
||
tree = parse_ast_tree(src) | ||
|
||
visitor = ConsecutiveDefaultTypeVarsVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters