Skip to content

Commit f282802

Browse files
committed
Fixed TypeCheckError in unpacking assignment involving properties
Fixes #506.
1 parent 91b0cbd commit f282802

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

docs/versionhistory.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Version history
44
This library adheres to
55
`Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200>`_.
66

7+
**UNRELEASED**
8+
9+
- Fixed ``TypeCheckError`` in unpacking assignment involving properties of a parameter
10+
of the function (`#506 <https://github.com/agronholm/typeguard/issues/506>`_;
11+
regression introduced in v4.4.1)
12+
713
**4.4.1** (2024-11-03)
814

915
- Dropped Python 3.8 support

src/typeguard/_transformer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,9 @@ def visit_Assign(self, node: Assign) -> Any:
10731073

10741074
path.insert(0, exp.id)
10751075
name = prefix + ".".join(path)
1076-
annotation = self._memo.variable_annotations.get(exp.id)
1077-
if annotation:
1076+
if len(path) == 1 and (
1077+
annotation := self._memo.variable_annotations.get(exp.id)
1078+
):
10781079
annotations_.append((Constant(name), annotation))
10791080
check_required = True
10801081
else:

tests/dummymodule.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class Metaclass(type):
7474

7575
@typechecked
7676
class DummyClass(metaclass=Metaclass):
77+
bar: str
78+
baz: int
79+
7780
def type_checked_method(self, x: int, y: int) -> int:
7881
return x * y
7982

@@ -270,6 +273,11 @@ def unpacking_assign_star_no_annotation(value: Any) -> Tuple[int, List[bytes], s
270273
return x, y, z
271274

272275

276+
@typechecked
277+
def attribute_assign_unpacking(obj: DummyClass) -> None:
278+
obj.bar, obj.baz = "foo", 123123
279+
280+
273281
@typechecked(forward_ref_policy=ForwardRefPolicy.ERROR)
274282
def override_forward_ref_policy(value: "NonexistentType") -> None: # noqa: F821
275283
pass

tests/test_instrumentation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ def test_unpacking_assign_star_no_annotation_success(dummymodule):
250250
)
251251

252252

253+
def test_attribute_assign_unpacking(dummymodule):
254+
foo = dummymodule.DummyClass()
255+
dummymodule.attribute_assign_unpacking(foo)
256+
257+
253258
def test_unpacking_assign_star_no_annotation_fail(dummymodule):
254259
with pytest.raises(
255260
TypeCheckError, match=r"value assigned to z \(bytes\) is not an instance of str"

0 commit comments

Comments
 (0)