New submission from Crusader Ky <[email protected]>:
An exceedingly common pattern in many Python libraries is for a function to
accept either a string or a list of strings, and change the function output
accordingly.
This however does not play nice with @typing.overload, as a str variable is
also an Iterable[str] that yields individual characters; a bytes variable is
also an Iterable[bytes].
The example below confuses tools like mypy:
@overload
def f(x: str) -> int
...
@overload
def f(x: Iterable[str]) -> List[int]
...
def f(x):
if isinstance(x, str):
return len(x)
return [len(i) for i in x]
mypy output:
error: Overloaded function signatures 1 and 2 overlap with incompatible return
types
The proposed solution is to modify PEP484 to specify that, in case of
ambiguity, whatever overloaded typing is defined first wins. This would be
coherent with the behaviour of @functools.singledispatch.
----------
components: Library (Lib)
messages: 339610
nosy: Crusader Ky
priority: normal
severity: normal
status: open
title: PEP484 @overload vs. str/bytes
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36555>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com