[issue40464] functools.singledispatch doesn't verify annotation is on FIRST parameter

Dutcho report at bugs.python.org
Mon May 4 16:00:54 EDT 2020


Dutcho <dutcho at ziggo.nl> added the comment:

I'm afraid my "even return" was interpreted in https://github.com/python/cpython/pull/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:
    ```
    >>> @functools.singledispatch
    ... def func(arg, x):...
    >>> @func.register
    ... def _int(arg, x:int):...
    >>> @func.register
    ... def _str(arg, x:str):...
```
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.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40464>
_______________________________________


More information about the Python-bugs-list mailing list