Skip to content

Commit 447ee40

Browse files
Fixed checking of assignment expressions (#511)
The test fails without this commit because the mypy output creates line breaks and the expected output doesn't account for that. Fixed by remove unnecessary line breaks via regex. Fixes #510.
1 parent 95ef60d commit 447ee40

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

docs/versionhistory.rst

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This library adheres to
1111
regression introduced in v4.4.1)
1212
- Fixed display of module name for forward references
1313
(`#492 <https://github.com/agronholm/typeguard/pull/492>`_; PR by @JelleZijlstra)
14+
- Fixed ``TypeError`` when using an assignment expression
15+
(`#510 <https://github.com/agronholm/typeguard/issues/510>`_; PR by @JohannesK71083)
1416

1517
**4.4.1** (2024-11-03)
1618

src/typeguard/_transformer.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,20 @@ def visit_NamedExpr(self, node: NamedExpr) -> Any:
11381138
func_name,
11391139
[
11401140
node.value,
1141-
Constant(node.target.id),
1142-
annotation,
1141+
List(
1142+
[
1143+
List(
1144+
[
1145+
Tuple(
1146+
[Constant(node.target.id), annotation],
1147+
ctx=Load(),
1148+
)
1149+
],
1150+
ctx=Load(),
1151+
)
1152+
],
1153+
ctx=Load(),
1154+
),
11431155
self._memo.get_memo_name(),
11441156
],
11451157
[],

tests/mypy/test_type_annotations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_negative_mypy_output() -> str:
3131
)
3232
output = process.stdout.decode()
3333
assert output
34-
return output
34+
return re.sub(r"\n(?!\s|negative\.py)", " ", output.replace("\r", ""))
3535

3636

3737
def get_expected_errors() -> Dict[int, str]:

tests/test_transformer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ def foo() -> None:
14751475
def foo() -> None:
14761476
memo = TypeCheckMemo(globals(), locals())
14771477
x: int
1478-
if (x := check_variable_assignment(otherfunc(), 'x', int, \
1478+
if (x := check_variable_assignment(otherfunc(), [[('x', int)]], \
14791479
memo)):
14801480
pass
14811481
"""
@@ -1504,7 +1504,7 @@ def foo(x: int) -> None:
15041504
def foo(x: int) -> None:
15051505
memo = TypeCheckMemo(globals(), locals())
15061506
check_argument_types('foo', {'x': (x, int)}, memo)
1507-
if (x := check_variable_assignment(otherfunc(), 'x', int, memo)):
1507+
if (x := check_variable_assignment(otherfunc(), [[('x', int)]], memo)):
15081508
pass
15091509
"""
15101510
).strip()

0 commit comments

Comments
 (0)