diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 79a6e200..2d45feb9 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -11,6 +11,8 @@ This library adheres to regression introduced in v4.4.1) - Fixed display of module name for forward references (`#492 `_; PR by @JelleZijlstra) +- Fixed ``TypeError`` when using an assignment expression + (`#510 `_; PR by @JohannesK71083) **4.4.1** (2024-11-03) diff --git a/src/typeguard/_transformer.py b/src/typeguard/_transformer.py index 999dec0c..a9f0aedb 100644 --- a/src/typeguard/_transformer.py +++ b/src/typeguard/_transformer.py @@ -1138,8 +1138,20 @@ def visit_NamedExpr(self, node: NamedExpr) -> Any: func_name, [ node.value, - Constant(node.target.id), - annotation, + List( + [ + List( + [ + Tuple( + [Constant(node.target.id), annotation], + ctx=Load(), + ) + ], + ctx=Load(), + ) + ], + ctx=Load(), + ), self._memo.get_memo_name(), ], [], diff --git a/tests/mypy/test_type_annotations.py b/tests/mypy/test_type_annotations.py index aacee7fb..ac978264 100644 --- a/tests/mypy/test_type_annotations.py +++ b/tests/mypy/test_type_annotations.py @@ -31,7 +31,7 @@ def get_negative_mypy_output() -> str: ) output = process.stdout.decode() assert output - return output + return re.sub(r"\n(?!\s|negative\.py)", " ", output.replace("\r", "")) def get_expected_errors() -> Dict[int, str]: diff --git a/tests/test_transformer.py b/tests/test_transformer.py index 2e18a5ad..3fb04627 100644 --- a/tests/test_transformer.py +++ b/tests/test_transformer.py @@ -1475,7 +1475,7 @@ def foo() -> None: def foo() -> None: memo = TypeCheckMemo(globals(), locals()) x: int - if (x := check_variable_assignment(otherfunc(), 'x', int, \ + if (x := check_variable_assignment(otherfunc(), [[('x', int)]], \ memo)): pass """ @@ -1504,7 +1504,7 @@ def foo(x: int) -> None: def foo(x: int) -> None: memo = TypeCheckMemo(globals(), locals()) check_argument_types('foo', {'x': (x, int)}, memo) - if (x := check_variable_assignment(otherfunc(), 'x', int, memo)): + if (x := check_variable_assignment(otherfunc(), [[('x', int)]], memo)): pass """ ).strip()