mypy reports `used-before-def` with `TypeAlias`es despite `from __future__ import annotations`
@ilinum is already working on this.
Since Jan 27, 2023.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.