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 crusaderky
Recipients crusaderky
Date 2019-04-08.10:50:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1554720612.88.0.913385974113.issue36555@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-04-08 10:50:12crusaderkysetrecipients: + crusaderky
2019-04-08 10:50:12crusaderkysetmessageid: <1554720612.88.0.913385974113.issue36555@roundup.psfhosted.org>
2019-04-08 10:50:12crusaderkylinkissue36555 messages
2019-04-08 10:50:12crusaderkycreate