Skip to content
Open
Show file tree
Hide file tree
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
53 changes: 42 additions & 11 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,7 @@ def _find_impl(cls, registry):
match = t
return registry.get(match)

def singledispatch(func):
"""Single-dispatch generic function decorator.

Transforms a function into a generic function, which can have different
behaviours depending upon the type of its first argument. The decorated
function acts as the default implementation, and additional
implementations can be registered using the register() attribute of the
generic function.
"""
def _singledispatchimpl(func, *, is_method):
# 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.
Expand Down Expand Up @@ -916,6 +908,32 @@ def _is_valid_dispatch_type(cls):
return (isinstance(cls, UnionType) and
all(isinstance(arg, type) for arg in cls.__args__))

def _skip_self_type(argname, cls, hints_iter):
# GH-130827: Methods are sometimes annotated with
# typing.Self. We should skip that when it's a valid type.
from typing import Self
if cls is not Self:
return argname, cls
if not is_method:
# typing.Self is not valid in a normal function
raise TypeError(
f"Invalid annotation for {argname!r}. "
"typing.Self can only be used with singledispatchmethod()"
)
try:
argname, cls = next(hints_iter)
return argname, cls
except StopIteration:
# The method is one of some invalid edge cases:
# 1. method(self: Self) -> ...
# 2. method(self, weird: Self) -> ...
# 3. method(self: Self, unannotated) -> ...
raise TypeError(
f"Invalid annotation for {argname!r}. "
"typing.Self must be the first annotation and must "
"have a second parameter with an annotation"
) from None

def register(cls, func=None):
"""generic_func.register(cls, func) -> func

Expand Down Expand Up @@ -944,7 +962,10 @@ def register(cls, func=None):
# only import typing if annotation parsing is necessary
from typing import get_type_hints
from annotationlib import Format, ForwardRef
argname, cls = next(iter(get_type_hints(func, format=Format.FORWARDREF).items()))
hints_iter = iter(get_type_hints(func, format=Format.FORWARDREF).items())
argname, cls = next(hints_iter)
argname, cls = _skip_self_type(argname, cls, hints_iter)

if not _is_valid_dispatch_type(cls):
if isinstance(cls, UnionType):
raise TypeError(
Expand Down Expand Up @@ -987,6 +1008,16 @@ def wrapper(*args, **kw):
update_wrapper(wrapper, func)
return wrapper

def singledispatch(func):
"""Single-dispatch generic function decorator.

Transforms a function into a generic function, which can have different
behaviours depending upon the type of its first argument. The decorated
function acts as the default implementation, and additional
implementations can be registered using the register() attribute of the
generic function.
"""
return _singledispatchimpl(func, is_method=False)

# Descriptor version
class singledispatchmethod:
Expand All @@ -1000,7 +1031,7 @@ def __init__(self, func):
if not callable(func) and not hasattr(func, "__get__"):
raise TypeError(f"{func!r} is not callable or a descriptor")

self.dispatcher = singledispatch(func)
self.dispatcher = _singledispatchimpl(func, is_method=True)
self.func = func

def register(self, cls, method=None):
Expand Down
56 changes: 56 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3454,6 +3454,62 @@ def _(item, arg: bytes) -> str:
self.assertEqual(str(Signature.from_callable(A.static_func)),
'(item, arg: int) -> str')

def test_typing_self(self):
# gh-130827: typing.Self with singledispatchmethod() didn't work
class Foo:
@functools.singledispatchmethod
def bar(self: typing.Self, arg: int | str) -> int | str: ...

@bar.register
def _(self: typing.Self, arg: int) -> int:
return arg


foo = Foo()
self.assertEqual(foo.bar(42), 42)

# But, it shouldn't work on singledispatch()
@functools.singledispatch
def test(self: typing.Self, arg: int | str) -> int | str:
pass
with self.assertRaises(TypeError):
@test.register
def silly(self: typing.Self, arg: int | str) -> int | str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will 'typing.Self' annotation work the same way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! It should, yeah. get_type_hints handles forward references.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test case for that, maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, but as long as get_type_hints is working it's probably fine.

pass

# typing.Self cannot be the only annotation
with self.assertRaises(TypeError):
class Foo:
@functools.singledispatchmethod
def bar(self: typing.Self, arg: int | str):
pass

@bar.register
def _(self: typing.Self, arg):
return arg

# typing.Self can only be used in the first parameter
with self.assertRaises(TypeError):
class Foo:
@functools.singledispatchmethod
def bar(self, arg: int | str):
pass

@bar.register
def _(self, arg: typing.Self):
return arg

# 'self' cannot be the only parameter
with self.assertRaises(TypeError):
class Foo:
@functools.singledispatchmethod
def bar(self: typing.Self, arg: int | str):
pass

@bar.register
def _(self: typing.Self):
pass


class CachedCostItem:
_cost = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :exc:`TypeError` when using :func:`functools.singledispatchmethod` with
a :class:`typing.Self` annotation.
Loading