You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
functools.singledispatch doesn't verify annotation is on FIRST parameter #84644
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2020-05-01.07:04:01.217>labels= ['type-bug', 'library', '3.9', '3.10', '3.11']
title="functools.singledispatch doesn't verify annotation is on FIRST parameter"updated_at=<Date2021-11-09.22:47:09.886>user='https://github.com/Dutcho'
From Python 3.7, functools.singledispatch makes the register() attribute of the generic function infer the type of the first argument automatically for functions annotated with types. That's great for DRY.
However, in 3.7 and 3.8, no check is made that the first parameter of the registered function is actually annotated; any annotation suffices, even the return one.
>>> @func.register
... def _no_annotation(arg): ...
Traceback (most recent call last):
...
TypeError: Invalid first argument to `register()`: <function _no_annotation at 0x000001D769A43D30>. Use either `@register(some_class)` or plain `@register` on an annotated function.
I'm afraid my "even return" was interpreted in #19871 as "only return", while as stated "any annotation" suffices. To rephrase:
If the *first* parameter of the registered function isn't annotated, any non-first annotation suffices for registering, but will not dispatch correctly.
Example:
No errors happen, although parameter x is annotated, not the first parameter arg. So func() will dispatch on the type of arg according to the annotation of x.
So I'm afraid the PR solves the specific "return" example case, but not the flagged general issue.
Right, forgot about that. We can get the first argument name from inspect.signature and then fetch it from the get_type_hints dictionary, I don't know a better way to do it.
Activity
Dutcho commentedon May 1, 2020
From Python 3.7,
functools.singledispatchmakes theregister()attribute of the generic function infer the type of the first argument automatically for functions annotated with types. That's great for DRY.However, in 3.7 and 3.8, no check is made that the first parameter of the registered function is actually annotated; any annotation suffices, even the return one.
Example:
No errors happen, although the return type, not
arg, is annotated.This results in:
Obviously, that doesn't dispatch correctly.
Note that un-annotated functions *are* caught:
Dutcho commentedon May 4, 2020
I'm afraid my "even return" was interpreted in #19871 as "only return", while as stated "any annotation" suffices. To rephrase:
If the *first* parameter of the registered function isn't annotated, any non-first annotation suffices for registering, but will not dispatch correctly.
Example:
No errors happen, although parameter
xis annotated, not the first parameterarg. Sofunc()will dispatch on the type ofargaccording to the annotation ofx.So I'm afraid the PR solves the specific "return" example case, but not the flagged general issue.
FFY00 commentedon May 5, 2020
Right, forgot about that. We can get the first argument name from inspect.signature and then fetch it from the get_type_hints dictionary, I don't know a better way to do it.
AlexWaygood commentedon Nov 9, 2021
Reproduced on 3.11.
6 remaining items
Add precedent case for pythonGH-84644
Fix pythonGH-84644 test
Fix `TestSingleDispatch.test_method_signatures` to put annotations on…
gh-127750: Fix annotations in singledispatchmethod signature tests (G…
pythongh-127750: Fix annotations in singledispatchmethod signature te…
pythongh-127750: Fix annotations in singledispatchmethod signature te…
[3.13] gh-127750: Fix annotations in singledispatchmethod signature t…
[3.14] gh-127750: Fix annotations in singledispatchmethod signature t…
pythongh-127750: Fix annotations in singledispatchmethod signature te…
pythongh-127750: Fix annotations in singledispatchmethod signature te…