8000 functools: Give up on lazy-importing types (#124736) · python/cpython@95581b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95581b3

Browse files
functools: Give up on lazy-importing types (#124736)
PR #121089 added an eager import for types.MethodType, but still left the existing hacks for lazily importing from types. We could also create MethodType internally in functools.py (e.g., by using `type(Placeholder.__repr__)`, but it feels not worth it at this point, so instead I unlazified all the usages of types in the module.
1 parent 76fbee6 commit 95581b3

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Lib/functools.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
from abc import get_cache_token
1818
from collections import namedtuple
19-
# import types, weakref # Deferred to single_dispatch()
19+
# import weakref # Deferred to single_dispatch()
2020
from operator import itemgetter
2121
from reprlib import recursive_repr
22-
from types import MethodType
22+
from types import GenericAlias, MethodType, MappingProxyType, UnionType
2323
from _thread import RLock
2424

25-
# Avoid importing types, so we can speedup import time
26-
GenericAlias = type(list[int])
27-
2825
################################################################################
2926
### update_wrapper() and wraps() decorator
3027
################################################################################
@@ -900,7 +897,7 @@ def singledispatch(func):
900897
# There are many programs that use functools without singledispatch, so we
901898
# trade-off making singledispatch marginally slower for the benefit of
902899
# making start-up of such applications slightly faster.
903-
import types, weakref
900+
import weakref
904901

905902
registry = {}
906903
dispatch_cache = weakref.WeakKeyDictionary()
@@ -931,7 +928,7 @@ def dispatch(cls):
931928

932929
def _is_union_type(cls):
933930
from typing import get_origin, Union
934-
return get_origin(cls) in {Union, types.UnionType}
931+
return get_origin(cls) in {Union, UnionType}
935932

936933
def _is_valid_dispatch_type(cls):
937934
if isinstance(cls, type):
@@ -1008,7 +1005,7 @@ def wrapper(*args, **kw):
10081005
registry[object] = func
10091006
wrapper.register = register
10101007
wrapper.dispatch = dispatch
1011-
wrapper.registry = types.MappingProxyType(registry)
1008+
wrapper.registry = MappingProxyType(registry)
10121009
wrapper._clear_cache = dispatch_cache.clear
10131010
update_wrapper(wrapper, func)
10141011
return wrapper

0 commit comments

Comments
 (0)
0