Skip to content

Commit fec544b

Browse files
authored
Merge pull request #202 from ap--/fix/dict-comprehension-cython-3.3
Fix/dict comprehension cython 3.3
2 parents 008c7ca + 2fa16b7 commit fec544b

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎cython_lint/cython_lint.py‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,14 @@
8282

8383
from cython_lint import __version__
8484

85-
CYTHON_VERSION = tuple(Cython.__version__.split(".")) # type: ignore[attr-defined]
85+
# non-numeric parts (e.g. the 'b1' of '3.1.0b1') are dropped
86+
CYTHON_VERSION = tuple(
87+
int(part)
88+
for part in Cython.__version__.split(".") # type: ignore[attr-defined]
89+
if part.isdigit()
90+
)
91+
CYTHON_3 = (3,)
92+
CYTHON_3_3 = (3, 3)
8693
if TYPE_CHECKING:
8794
from collections.abc import Hashable
8895
from collections.abc import Iterator
@@ -99,7 +106,7 @@
99106
r")/"
100107
)
101108

102-
if CYTHON_VERSION > ("3",): # pragma: no cover
109+
if CYTHON_VERSION >= CYTHON_3:
103110
from Cython.Compiler.ExprNodes import AnnotationNode # type: ignore[assignment]
104111
else: # pragma: no cover
105112

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

338345

346+
def _value_from_dict_comprehension_append_node(
347+
node: DictComprehensionAppendNode,
348+
) -> ExprNode:
349+
if CYTHON_VERSION >= CYTHON_3_3:
350+
# Cython 3.3 replaced key_expr/value_expr with a single DictItemNode
351+
return node.dict_item.value # type: ignore[attr-defined]
352+
return node.value_expr # type: ignore[attr-defined] # pragma: no cover
353+
354+
339355
def _rhs_from_single_assignment_node(node: SingleAssignmentNode) -> ExprNode:
340356
return node.rhs # type: ignore[attr-defined]
341357

@@ -629,7 +645,7 @@ def _traverse_file( # noqa: PLR0915,PLR0913
629645
)
630646

631647
if isinstance(node, (IfClauseNode, AssertStatNode)):
632-
if CYTHON_VERSION > ("3",) or isinstance(
648+
if CYTHON_VERSION >= CYTHON_3 or isinstance(
633649
node, IfClauseNode
634650
): # pragma: no cover
635651
test = isinstance(node.condition, TupleNode)
@@ -683,13 +699,13 @@ def _traverse_file( # noqa: PLR0915,PLR0913
683699
and isinstance(node.loop.body, ComprehensionAppendNode)
684700
):
685701
if isinstance(node.loop.body, DictComprehensionAppendNode):
686-
expr = node.loop.body.value_expr
702+
_expr = _value_from_dict_comprehension_append_node(node.loop.body)
687703
else:
688-
expr = node.loop.body.expr
689-
if isinstance(expr, LambdaNode) and not hasattr(expr, "loop"):
704+
_expr = node.loop.body.expr
705+
if isinstance(_expr, LambdaNode) and not hasattr(_expr, "loop"):
690706
# GeneratorExpressionNode is a LambdaNode, and has a loop
691707
# attribute, so need to exclude it.
692-
_children = [j.node for j in traverse(expr)]
708+
_children = [j.node for j in traverse(_expr)]
693709
_names = [
694710
_name_from_name_node(_child)
695711
for _child in _children

‎tests/main_test.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,22 @@ def test_late_binding_closure(
555555
assert ret == 1
556556

557557

558+
@pytest.mark.parametrize(
559+
"src",
560+
[
561+
"def f(seq):\n return [x for x in seq]\n",
562+
"def f(seq):\n return {x for x in seq}\n",
563+
"def f(seq):\n return {x: x for x in seq}\n",
564+
"def f(seq):\n return {k: v for k, v in seq}\n",
565+
],
566+
)
567+
def test_comprehension_without_late_binding_closure(capsys: Any, src: str) -> None:
568+
ret = _main(src, "t.py", ext=".pyx", no_pycodestyle=True)
569+
out, _ = capsys.readouterr()
570+
assert out == ""
571+
assert ret == 0
572+
573+
558574
@pytest.mark.parametrize(
559575
("ignore", "expected", "exp_ret"),
560576
[

0 commit comments

Comments
 (0)