MyPy fails on Union[str, int] < Union[str, int] when underlying type is known
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Given the following script I'm receiving several unexpected errors:
test.py:22: error: Unsupported operand types for < ("str" and "int")
test.py:22: error: Unsupported operand types for < ("int" and "str")
test.py:22: note: Both left and right operands are unions
test.py:24: error: Unsupported operand types for < ("str" and "int")
test.py:24: error: Unsupported operand types for < ("int" and "str")
test.py:24: note: Both left and right operands are unions
Found 4 errors in 1 file (checked 1 source file)
Even though the if-block and assert statement just before the comparison operator usage on a and b clearly filters out the case of mismatching types. I've only added the assert statement in the hope of telling mypy that these variables have the same type, but no luck there.
from typing import Union
def semver_prerelease_compare(lhs: str, rhs: str) -> bool:
a: Union[str, int]
b: Union[str, int]
for a, b in zip(lhs.split("."), rhs.split(".")):
try:
a = int(a)
except ValueError:
pass
try:
b = int(b)
except ValueError:
pass
if isinstance(a, int) and not isinstance(b, int):
# Numeric identifiers sort before non-numeric ones
return True
if type(a) != type(b):
return False
if a < b:
return True
elif b < a:
return False
return len(lhs) < len(rhs)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere die gemeldeten Diagnosen, indem du mypy auf test.py ausführst, und verfolge anschließend, wie sich die Guard-Bedingung type(a) != type(b) auf die Eingrenzung für die Vergleiche a < b und b < a auswirkt. Erledigt, wenn die Prüfung auf äquivalente Typen nicht mehr die gemeldeten str/int-Fehler erzeugt und das Verhalten durch einen Regressionstest abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100