Mypy gives confusing/misleading error message
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy reports error: Name "..." is not defined [name-defined] when using an instance attribute that was annotated.
To Reproduce
from inspect import Parameter
class ListElementDescriptor[T]:
name: str
item_type: type[T]
@property
def signature_parameter(self) -> Parameter:
return Parameter(name=self.name, kind=Parameter.KEYWORD_ONLY, annotation=list[self.item_type])
Expected Behavior
Definitely not an error that self.item_type is not defined. I suspect mypy tries to to convey a different error, maybe related to using variables for type parameters in generics, though I disagree with that as well since list[] is not used here in a typing context to specify a type, but is used as a value.
Actual Behavior
typing-7.py:11: error: Name "self.item_type" is not defined [name-defined]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 (compiled: no) - Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files):
[tool.mypy]
enable_incomplete_feature = "NewGenericSyntax"
disable_bytearray_promotion = true
disable_memoryview_promotion = true
check_untyped_defs = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
- Python version used: 3.12
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.
Research direction
Start by running the provided ListElementDescriptor example with mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 and the shown Python 3.12 configuration. Trace the diagnostic for list[self.item_type] and update it so the reported problem is accurate rather than claiming self.item_type is undefined; rerun the reproduction to confirm the misleading name-defined error is gone.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100