Spurious type error using result of re.match in lambda inside map
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Consider the following testcase:
import re
r = re.compile("([a-z]+)")
if m := re.match(r, "abc"):
matches = map(lambda i: m.group(i), [1])
print(next(matches))
I expect this to pass type checking successfully, but mypy instead complains about the map line as follows:
mypy ./t.py
t.py:4: error: Item "None" of "Match[str] | None" has no attribute "group" [union-attr]
Found 1 error in 1 file (checked 1 source file)
The bug only seems to happen if the lambda appears directly inside the call to map. E.g. if I pre-declare it as follows:
import re
r = re.compile("([a-z]+)")
if m := re.match(r, "abc"):
fn = lambda i: m.group(i)
matches = map(fn, [1])
print(next(matches))
then this type-checks OK. My environment is as follows:
$ mypy --version
mypy 1.15.0 (compiled: yes)
$ python3 --version
Python 3.13.2
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
Beginne mit dem bereitgestellten Testfall und führe mypy 1.15.0 unter Python 3.13.2 aus, wobei du die an map übergebene Inline-Lambda-Funktion mit der separat zugewiesenen Lambda-Funktion vergleichst. Die Aufgabe ist abgeschlossen, wenn die Inline-Form ohne den gemeldeten union-attr-Fehler typgeprüft wird und dabei das bestehende Verhalten erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100