https://github.com/python/cpython/commit/95581b35517f547b44e42631bd97ab4ba1a5c343
commit: 95581b35517f547b44e42631bd97ab4ba1a5c343
branch: main
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2024-09-29T06:31:06-07:00
summary:

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.

files:
M Lib/functools.py

diff --git a/Lib/functools.py b/Lib/functools.py
index 83b8895794e7c0..9d53d3601559b2 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -16,15 +16,12 @@
 
 from abc import get_cache_token
 from collections import namedtuple
-# import types, weakref  # Deferred to single_dispatch()
+# import weakref  # Deferred to single_dispatch()
 from operator import itemgetter
 from reprlib import recursive_repr
-from types import MethodType
+from types import GenericAlias, MethodType, MappingProxyType, UnionType
 from _thread import RLock
 
-# Avoid importing types, so we can speedup import time
-GenericAlias = type(list[int])
-
 
################################################################################
 ### update_wrapper() and wraps() decorator
 
################################################################################
@@ -900,7 +897,7 @@ def singledispatch(func):
     # There are many programs that use functools without singledispatch, so we
     # trade-off making singledispatch marginally slower for the benefit of
     # making start-up of such applications slightly faster.
-    import types, weakref
+    import weakref
 
     registry = {}
     dispatch_cache = weakref.WeakKeyDictionary()
@@ -931,7 +928,7 @@ def dispatch(cls):
 
     def _is_union_type(cls):
         from typing import get_origin, Union
-        return get_origin(cls) in {Union, types.UnionType}
+        return get_origin(cls) in {Union, UnionType}
 
     def _is_valid_dispatch_type(cls):
         if isinstance(cls, type):
@@ -1008,7 +1005,7 @@ def wrapper(*args, **kw):
     registry[object] = func
     wrapper.register = register
     wrapper.dispatch = dispatch
-    wrapper.registry = types.MappingProxyType(registry)
+    wrapper.registry = MappingProxyType(registry)
     wrapper._clear_cache = dispatch_cache.clear
     update_wrapper(wrapper, func)
     return wrapper

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to