From 546a19e6eb50e94fa37d206db7ad4344bc984abc Mon Sep 17 00:00:00 2001 From: Eugene Morozov Date: Thu, 14 Aug 2025 16:12:18 +0300 Subject: [PATCH] Adds support for the "type" keyword in Python 3.12+ --- flake8_expression_complexity/utils/complexity.py | 7 +++++++ setup.py | 3 +++ tests/test_complexity.py | 5 +++++ tests/test_files/type_alias.py | 1 + 4 files changed, 16 insertions(+) create mode 100644 tests/test_files/type_alias.py diff --git a/flake8_expression_complexity/utils/complexity.py b/flake8_expression_complexity/utils/complexity.py index 9e04524..3551e05 100644 --- a/flake8_expression_complexity/utils/complexity.py +++ b/flake8_expression_complexity/utils/complexity.py @@ -61,6 +61,11 @@ ] ) +if sys.version_info >= (3, 12): + TYPES_MAP.append( + (ast.TypeAlias, 'type'), + ) + def get_expression_complexity(node: ast.AST) -> float: info = get_expression_part_info(node) @@ -99,6 +104,7 @@ def get_complexity_increase_for_node_type(node_type_sid: str) -> float: 'walrus': 2, 'match': 1, 'case': 1, + 'type': 1, } return nodes_scores_map[node_type_sid] @@ -148,5 +154,6 @@ def _get_sub_nodes(node: Any, node_type_sid: str) -> List[ast.AST]: 'walrus': lambda n: [n.target, n.value], 'match': lambda n: n.cases, 'case': lambda n: [], + 'type': lambda n: [n.value], } return subnodes_map[node_type_sid](node) diff --git a/setup.py b/setup.py index b1fc7e7..01eac2e 100644 --- a/setup.py +++ b/setup.py @@ -35,6 +35,9 @@ def get_long_description() -> str: 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', + 'Programming Language :: Python :: 3.12', + 'Programming Language :: Python :: 3.13', ], long_description=get_long_description(), long_description_content_type='text/markdown', diff --git a/tests/test_complexity.py b/tests/test_complexity.py index 26bcfd6..f210eba 100644 --- a/tests/test_complexity.py +++ b/tests/test_complexity.py @@ -19,3 +19,8 @@ def test_walrus(): def test_match(): errors = run_validator_for_test_file('match.py', max_expression_complexity=1) assert len(errors) == 1 + + +@pytest.mark.skipif(sys.version_info < (3, 12), reason='runs only for python 3.12+') +def test_type(): + run_validator_for_test_file('type_alias.py', max_expression_complexity=1) diff --git a/tests/test_files/type_alias.py b/tests/test_files/type_alias.py new file mode 100644 index 0000000..999ad3e --- /dev/null +++ b/tests/test_files/type_alias.py @@ -0,0 +1 @@ +type Integer = int