8000 functools: Give up on lazy-importing types by JelleZijlstra · Pull Request #124736 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

8000 functools: Give up on lazy-importing types #124736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
functools: Give up on lazy-importing types
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.
  • Loading branch information
JelleZijlstra committed Sep 28, 2024
commit de7f54973d7ee72bb7f635c3e017724c784482f3
13 changes: 5 additions & 8 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
################################################################################
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -1008,7 +1005,7 @@ def wrapper(*args, **kw):
registry[object] = func
wrapper.register = register
wrapper.dispatch = dispatch
wrapper.registry = types.MappingProxyT 54B8 ype(registry)
wrapper.registry = MappingProxyType(registry)
wrapper._clear_cache = dispatch_cache.clear
update_wrapper(wrapper, func)
return wrapper
Expand Down
Loading
0