Skip to content

Commit f29c9e6

Browse files
committed
fix: support Cython 3.3 DictComprehensionAppendNode layout
Cython 3.3 replaced the `key_expr`/`value_expr` children of `DictComprehensionAppendNode` with a single `dict_item` child (a `DictItemNode` with `key`/`value`), which made cython-lint crash with `AttributeError` on any dict comprehension with a single, non-tuple loop target. - add `_value_from_dict_comprehension_append_node` helper that selects the attribute layout based on a Cython version check - depend on `packaging` and compare `packaging.version.Version` objects instead of raw string tuples, which compared incorrectly for multi-digit version segments (e.g. `3.10` sorted before `3.3`) Fixes #201
1 parent 4e5b339 commit f29c9e6

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎cython_lint/cython_lint.py‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@
7777
from Cython.Compiler.Nodes import StatListNode
7878
from Cython.Compiler.Nodes import StatNode
7979
from Cython.Compiler.TreeFragment import parse_from_strings
80+
from packaging.version import Version
8081
from tokenize_rt import src_to_tokens
8182
from tokenize_rt import tokens_to_src
8283

8384
from cython_lint import __version__
8485

85-
CYTHON_VERSION = tuple(Cython.__version__.split(".")) # type: ignore[attr-defined]
86+
CYTHON_VERSION = Version(Cython.__version__) # type: ignore[attr-defined]
87+
CYTHON_3 = Version("3")
88+
CYTHON_3_3 = Version("3.3")
8689
if TYPE_CHECKING:
8790
from collections.abc import Hashable
8891
from collections.abc import Iterator
@@ -99,7 +102,7 @@
99102
r")/"
100103
)
101104

102-
if CYTHON_VERSION > ("3",): # pragma: no cover
105+
if CYTHON_VERSION >= CYTHON_3: # pragma: no cover
103106
from Cython.Compiler.ExprNodes import AnnotationNode # type: ignore[assignment]
104107
else: # pragma: no cover
105108

@@ -336,6 +339,15 @@ def _args_from_sequence_node(node: SequenceNode) -> list[ExprNode]:
336339
return node.args # type: ignore[attr-defined]
337340

338341

342+
def _value_from_dict_comprehension_append_node(
343+
node: DictComprehensionAppendNode,
344+
) -> ExprNode:
345+
if CYTHON_VERSION >= CYTHON_3_3: # pragma: no cover
346+
# Cython 3.3 replaced key_expr/value_expr with a single DictItemNode
347+
return node.dict_item.value # type: ignore[attr-defined]
348+
return node.value_expr # type: ignore[attr-defined] # pragma: no cover
349+
350+
339351
def _rhs_from_single_assignment_node(node: SingleAssignmentNode) -> ExprNode:
340352
return node.rhs # type: ignore[attr-defined]
341353

@@ -629,7 +641,7 @@ def _traverse_file( # noqa: PLR0915,PLR0913
629641
)
630642

631643
if isinstance(node, (IfClauseNode, AssertStatNode)):
632-
if CYTHON_VERSION > ("3",) or isinstance(
644+
if CYTHON_VERSION >= CYTHON_3 or isinstance(
633645
node, IfClauseNode
634646
): # pragma: no cover
635647
test = isinstance(node.condition, TupleNode)
@@ -683,13 +695,13 @@ def _traverse_file( # noqa: PLR0915,PLR0913
683695
and isinstance(node.loop.body, ComprehensionAppendNode)
684696
):
685697
if isinstance(node.loop.body, DictComprehensionAppendNode):
686-
expr = node.loop.body.value_expr
698+
_expr = _value_from_dict_comprehension_append_node(node.loop.body)
687699
else:
688-
expr = node.loop.body.expr
689-
if isinstance(expr, LambdaNode) and not hasattr(expr, "loop"):
700+
_expr = node.loop.body.expr
701+
if isinstance(_expr, LambdaNode) and not hasattr(_expr, "loop"):
690702
# GeneratorExpressionNode is a LambdaNode, and has a loop
691703
# attribute, so need to exclude it.
692-
_children = [j.node for j in traverse(expr)]
704+
_children = [j.node for j in traverse(_expr)]
693705
_names = [
694706
_name_from_name_node(_child)
695707
for _child in _children

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ classifiers = [
1818
]
1919
dependencies = [
2020
"cython>=0.29.32",
21+
"packaging",
2122
"pycodestyle",
2223
"tokenize-rt>=3.2.0",
2324
"tomli;python_version<'3.11'",

0 commit comments

Comments
 (0)