[New-bugs-announce] [issue32227] singledispatch support for type annotations

Łukasz Langa report at bugs.python.org
Tue Dec 5 16:24:12 EST 2017


New submission from Łukasz Langa <lukasz at langa.pl>:

With the patch attached to this issue, @singledispatch gains support for passing the type in @register via annotations.

This looks more natural and enables more thorough type checking without repeating yourself:


@singledispatch
def generic(arg): ...

@generic.register
def _(arg: str): ...

@generic.register
def _(arg: int): ...


The previous API is still available for backwards compatibility, as well as stacking, and use with classes (sic, I was surprised to learn it's used that way, too).

The patch should be uncontroversial, maybe except for the fact that it's importing the `typing` module if annotations are used. This is necessary because of forward references, usage of None as a type, and so on. More importantly, with PEP 563 it's mandatory.

----------
assignee: lukasz.langa
components: Library (Lib)
messages: 307686
nosy: lukasz.langa, rhettinger, yselivanov
priority: normal
severity: normal
status: open
title: singledispatch support for type annotations
type: enhancement
versions: Python 3.7

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


More information about the New-bugs-announce mailing list