Skip to content

Commit

Permalink
Fixed checking of assignment expressions (#511)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohannesK71083 authored Feb 16, 2025
1 parent 95ef60d commit 447ee40
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This library adheres to
regression introduced in v4.4.1)
- Fixed display of module name for forward references
(`#492 <https://github.com/agronholm/typeguard/pull/492>`_; PR by @JelleZijlstra)
- Fixed ``TypeError`` when using an assignment expression
(`#510 <https://github.com/agronholm/typeguard/issues/510>`_; PR by @JohannesK71083)

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

Expand Down
16 changes: 14 additions & 2 deletions src/typeguard/_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
[],
Expand Down
2 changes: 1 addition & 1 deletion tests/mypy/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 447ee40

Please sign in to comment.