Skip to content

Commit

Permalink
Fix issues with __module__ and __doc__ descriptors.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Feb 12, 2025
1 parent 93d0977 commit 16604a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,17 @@ def __init__(self, unbound, obj, cls):
self._dispatch = unbound.dispatcher.dispatch
self._obj = obj
self._cls = cls
# Set instance attributes which cannot be handled in __getattr__()
# because they conflict with type descriptors.
func = unbound.func
try:
self.__module__ = func.__module__
except AttributeError:
pass
try:
self.__doc__ = func.__doc__
except AttributeError:
pass

def __call__(self, /, *args, **kwargs):
if not args:
Expand All @@ -1057,19 +1068,13 @@ def __call__(self, /, *args, **kwargs):
return self._dispatch(args[0].__class__).__get__(self._obj, self._cls)(*args, **kwargs)

def __getattr__(self, name):
# Resolve these attributes lazily to speed up creation of
# the _singledispatchmethod_get instance.
if name not in {'__name__', '__qualname__', '__isabstractmethod__',
'__annotations__', '__type_params__'}:
raise AttributeError
return getattr(self._unbound.func, name)

@property
def __module__(self):
return self._unbound.func.__module__

@property
def __doc__(self):
return self._unbound.func.__doc__

@property
def register(self):
return self._unbound.register
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,7 @@ def static_func(arg: int) -> str:
):
with self.subTest(meth=meth):
self.assertEqual(meth.__module__, __name__)
self.assertEqual(type(meth).__module__, 'functools')
self.assertEqual(meth.__qualname__, prefix + meth.__name__)
self.assertEqual(meth.__doc__,
('My function docstring'
Expand Down

0 comments on commit 16604a1

Please sign in to comment.