Sentinel and enum type merge issues with generic
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 49/100
Rechercherichtung
Start by running the supplied examples through mypy and compare direct assignment with assigning the result to a temporary. Check both the sentinel and enum cases against the working int example. Done means the direct assignments are accepted and the revealed types match the expected results.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
from typing_extensions import sentinel
Undefined = sentinel("Undefined")
def none_if_undefined[_T](value: _T | Undefined) -> _T | None:
return None if value is Undefined else value
def func() -> None:
device_id: str | Undefined | None = Undefined
device_id = none_if_undefined(device_id) # <-- error
reveal_type(device_id)
test.py:10: error: Argument 1 to "none_if_undefined" has incompatible type "str | Undefined | None"; expected "str | Undefined" [arg-type]
test.py:11: note: Revealed type is "str | Undefined | None"
However, assigning the result from none_if_undefined to a temporary variable first works as expected.
def func() -> None:
device_id: str | Undefined | None = Undefined
x = none_if_undefined(device_id)
reveal_type(x)
device_id = x
reveal_type(device_id)
test.py:11: note: Revealed type is "str | None"
test.py:13: note: Revealed type is "str | None"
--
A similar issues exists for enums
from enum import Enum
from typing import Literal
class E(Enum):
SPECIAL = 1
def none_if_special[_T](value: _T | Literal[E.SPECIAL]) -> _T | None:
return None if value is E.SPECIAL else value
def func() -> None:
device_id: str | Literal[E.SPECIAL] | None = E.SPECIAL
device_id = none_if_special(device_id) # <-- error
reveal_type(device_id)
test.py:12: error: Argument 1 to "none_if_special" has incompatible type "str | Literal[E.SPECIAL] | None"; expected "str | Literal[E.SPECIAL]" [arg-type]
test.py:13: note: Revealed type is "str | Literal[test7.E.SPECIAL] | None"
--
For completeness the related example with just int - that works fine -- no errors.
def none_if_int[_T](value: _T | int) -> _T | None:
return None if isinstance(value, int) else value
def func() -> None:
device_id: str | int | None = 42
device_id = none_if_int(device_id)
reveal_type(device_id)
- Vorherrschende Sprache
- Python
- Sterne
- 20.7k
- Forks
- 3.4k
- Ø Merge
- 1 T. 15 Std.
- Gemergte PRs (30 T.)
- 114
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/mypy
-
Regression in 2.4.0: empty constraint tuple in PEP 695 type parameter (`class C[T: ()]`) is no longer reportedEvtl. wieder frei @CheerathAniketh hat das vor 22 Tagen übernommen, und es ist kein Pull Request offen. Offentopic-parser topic-pep-695
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python/mypy#22176 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
matches_gitignore: directory-only patterns in nested .gitignore not excluded under --exclude-gitignoreEvtl. wieder frei @pavithraramesh5500-ship-cse hat das vor 14 Tagen übernommen, und es ist kein Pull Request offen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
`stubgen --inspect-mode` crashes with `AttributeError on PEP 604 union syntax (X | Y)` *(mypy 2.1.0)*Evtl. vergeben @ayushdoesdev hat das vor 90 Tagen übernommen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
Inconsistent description of `strict_optional`Evtl. vergeben @Joosboy hat das vor 106 Tagen übernommen. Offendocumentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python/mypy#21658 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Cannot set `output` in `mypy.ini`Evtl. wieder frei Ein Pull Request dazu wurde ohne Merge geschlossen. Offenbug topic-configuration topic-error-reporting
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
python/mypy#21376 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
question
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
conda-forge/vc-feedstock#132 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
limitlessprojects/Go-Hybrid#3342 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
UKGovernmentBEIS/inspect_ai#6268 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
LSSTDESC/rail_sompz#42 ·
-
All tests fail with 'EXITED with status 3' on pytest 9.2.0 (TestReport._id is not marshallable)Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
pytest-dev/pytest-forked#113 ·