11import ast
22from typing import Final , final
33
4+ from wemake_python_styleguide .compat .constants import PY312
45from wemake_python_styleguide .logic .tree import attributes
6+ from wemake_python_styleguide .options .validation import ValidatedOptions
57from wemake_python_styleguide .types import AnyFunctionDef
68from wemake_python_styleguide .violations .best_practices import (
79 NewStyledDecoratorViolation ,
1517 ast .Name ,
1618)
1719
20+ _ALLOWED_DECORATOR_TYPES3_12 : Final = (
21+ * _ALLOWED_DECORATOR_TYPES ,
22+ ast .Subscript , # PEP 695 - Type Parameter Syntax
23+ )
24+
1825
1926@final
2027@alias (
2734class WrongDecoratorVisitor (BaseNodeVisitor ):
2835 """Checks decorators's correctness."""
2936
37+ def __init__ (
38+ self ,
39+ options : ValidatedOptions ,
40+ tree : ast .AST ,
41+ ** kwargs ,
42+ ) -> None :
43+ """Creates Decorator Visitor."""
44+ self .ALLOWED_DECORATOR_TYPES : Final = (
45+ _ALLOWED_DECORATOR_TYPES3_12 if PY312 else _ALLOWED_DECORATOR_TYPES
46+ )
47+ super ().__init__ (options , tree , ** kwargs )
48+
3049 def visit_any_function (self , node : AnyFunctionDef ) -> None :
3150 """Checks functions' decorators."""
3251 self ._check_new_decorator_syntax (node )
@@ -38,13 +57,13 @@ def _check_new_decorator_syntax(self, node: AnyFunctionDef) -> None:
3857 self .add_violation (NewStyledDecoratorViolation (decorator ))
3958
4059 def _is_allowed_decorator (self , node : ast .expr ) -> bool :
41- if not isinstance (node , _ALLOWED_DECORATOR_TYPES ):
60+ if not isinstance (node , self . ALLOWED_DECORATOR_TYPES ):
4261 return False
4362
4463 if isinstance (node , ast .Name ):
4564 return True # Simple names are fine!
4665
4766 return all (
48- isinstance (part , _ALLOWED_DECORATOR_TYPES )
67+ isinstance (part , self . ALLOWED_DECORATOR_TYPES )
4968 for part in attributes .parts (node )
5069 )
0 commit comments