False positive "Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader" when using Unions vs TypeVars
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When overloading a function to specify it can return bytes if given only bytes objects, str if given only str objects, or either if given a mix of both, mypy incorrectly complains that AnyStr is the same or broader than Union[str, bytes].
Despite the warning, mypy does correctly match the Union[str, bytes] signature when appropriate
To Reproduce
Very simple test case:
from typing import overload, TypeAlias, Union, AnyStr, reveal_type
from collections.abc import Sequence
from random import choice
AllStr: TypeAlias = Union[bytes, str]
# Below produces "error: Overloaded function signature 2 will never be
# matched: signature 1's parameter type(s) are the same or broader
# [misc]"
@overload
def pick_three(choices: Sequence[AnyStr]) -> AnyStr: ...
@overload
def pick_three(choices: Sequence[AllStr]) -> AllStr: ...
def pick_three(choices: Sequence[AllStr]) -> AllStr:
return choice(choices)
strings: tuple[str, ...] = ('abc', 'def')
byte_strings: tuple[bytes, ...] = (b'abc', b'def')
mixed_strings: tuple[AllStr, ...] = ('abc', b'def')
reveal_type(pick_three(strings)) # str
reveal_type(pick_three(byte_strings)) # bytes
reveal_type(pick_three(mixed_strings)) # Union[bytes, str], despite the warning
Some more example code on mypy Playground, with other related cases that work as expected.
Expected Behavior
Mypy does not warn that signature 2 cannot match, given it clearly is able to match based on the reveal_type(pick_three(mixed_strings)) output
Actual Behavior
main.py:39: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
main.py:43: note: Revealed type is "builtins.str"
main.py:44: note: Revealed type is "builtins.bytes"
main.py:45: note: Revealed type is "Union[builtins.bytes, builtins.str]"
Your Environment
Seen on Debian with Python 3.11 and mypy 1.0.1 using --strict, and in mypy Playground with Python 3.11 and mypy 1.4.1 with default options.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l'esempio in main.py con Python 3.11 e mypy 1.4.1 o versioni successive, usando gli overloads e le chiamate a reveal_type mostrati nel report. La correzione è completata quando la chiamata con stringhe miste continua a rivelare Union[bytes, str], mentre mypy non segnala più che overload signature 2 non verrà mai abbinata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100