diff --git a/Lib/typing.py b/Lib/typing.py index 95bd61c7f8c61f4..be191555205aa1b 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -344,6 +344,7 @@ def _flatten_literal_params(parameters): _cleanups = [] +_caches = { } def _tp_cache(func=None, /, *, typed=False): @@ -351,13 +352,15 @@ def _tp_cache(func=None, /, *, typed=False): original function for non-hashable arguments. """ def decorator(func): - cached = functools.lru_cache(typed=typed)(func) - _cleanups.append(cached.cache_clear) + cache = functools.lru_cache(typed=typed)(func) + _caches[func] = cache + _cleanups.append(cache.cache_clear) + del cache @functools.wraps(func) def inner(*args, **kwds): try: - return cached(*args, **kwds) + return _caches[func](*args, **kwds) except TypeError: pass # All real errors (not unhashable args) are raised below. return func(*args, **kwds) diff --git a/Misc/NEWS.d/next/Library/2022-10-24-11-01-05.gh-issue-98253.HVd5v4.rst b/Misc/NEWS.d/next/Library/2022-10-24-11-01-05.gh-issue-98253.HVd5v4.rst new file mode 100644 index 000000000000000..dd37b9e81eb9a56 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-10-24-11-01-05.gh-issue-98253.HVd5v4.rst @@ -0,0 +1,2 @@ +Fixed a source of potential type-related refleaks caused by LRU caches in +``typing``.