This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Dutcho
Recipients Dutcho
Date 2020-05-01.07:04:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1588316641.36.0.617194746525.issue40464@roundup.psfhosted.org>
In-reply-to
Content
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.

Example:
    ```
    >>> @functools.singledispatch
    ... def func(arg):...
    >>> @func.register
    ... def _int(arg) -> int:...
    >>> @func.register
    ... def _str(arg) -> str:...
```
No errors happen, although the return type, *not* `arg`, is annotated.
This results in:
    ```
    >>> func.registry
    mappingproxy({<class 'object'>: <function func>, <class 'int'>: <function _int>, <class 'str'>: <function _str>})

    ```
Obviously, that doesn't dispatch correctly.

Note that un-annotated functions *are* caught:
    ```
    >>> @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.
    ```
History
Date User Action Args
2020-05-01 07:04:01Dutchosetrecipients: + Dutcho
2020-05-01 07:04:01Dutchosetmessageid: <1588316641.36.0.617194746525.issue40464@roundup.psfhosted.org>
2020-05-01 07:04:01Dutcholinkissue40464 messages
2020-05-01 07:04:00Dutchocreate