mypy reports `used-before-def` with `TypeAlias`es despite `from __future__ import annotations`
@ilinum arbeitet bereits daran.
Seit 27.1.2023.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
from __future__ import annotations helps avoid NameErrors with later-defined entities in python, but seems to ineffective with use(d)-before-def in mypy.
To Reproduce (edited example code, see edit history for previous version if I mis-edited anything):
"""Bug."""
from __future__ import annotations
from typing import TYPE_CHECKING
# This works (only) with `from __future__ import annotations`:
list_direct: list[Class] = []
# By contrast, mypy reports `used-before-def` either way:
if TYPE_CHECKING:
ListOfClass = list[Class]
list_via_alias: ListOfClass = []
class Class:
pass
Expected Behavior
No error
Actual Behavior
bug.py:8: error: Name "MyClass" is used before definition [used-before-def]
Your Environment
- Mypy version used: mypy 1.0.0+dev.425fb0b4cfaa78cd0bf23fd165e4d6a1170670fe (compiled: no)
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.11.1
Related: https://github.com/python/mypy/pull/14163, https://github.com/python/mypy/pull/14166
Interestingly, no error on playground on master:
https://mypy-play.net/?mypy=master&python=3.11&gist=f323516535c0d04cf4e0a2f2c985f4ff
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.
Bewertung
Dieses Issue wurde noch nicht bewertet.