Message367824
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.
``` |
|
Date |
User |
Action |
Args |
2020-05-01 07:04:01 | Dutcho | set | recipients:
+ Dutcho |
2020-05-01 07:04:01 | Dutcho | set | messageid: <1588316641.36.0.617194746525.issue40464@roundup.psfhosted.org> |
2020-05-01 07:04:01 | Dutcho | link | issue40464 messages |
2020-05-01 07:04:00 | Dutcho | create | |
|