Skip to content

Commit

Permalink
Merge cf38460 into bab0b3f
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jan 14, 2024
2 parents bab0b3f + cf38460 commit dfdf8ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def check_union(
formatted_errors = indent(
"\n".join(f"{key}: {error}" for key, error in errors.items()), " "
)
del errors # avoid creating ref cycle
raise TypeCheckError(f"did not match any element in the union:\n{formatted_errors}")


Expand Down
20 changes: 20 additions & 0 deletions tests/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,26 @@ def test_union_fail(self, annotation, value):
f" int: is not an instance of int"
)

@pytest.mark.skipif(
sys.implementation.name != "cpython",
reason="Test relies on CPython's reference counting behavior",
)
def test_union_reference_leak(self):
leaked = True

class Leak:
def __del__(self):
nonlocal leaked
leaked = False

def inner():
leak = Leak() # noqa: F841
with pytest.raises(TypeCheckError, match="any element in the union:"):
check_type(1, Union[str, bytes])

inner()
assert not leaked


class TestTypevar:
def test_bound(self):
Expand Down

0 comments on commit dfdf8ce

Please sign in to comment.