fastparse and native parser assign different line numbers to nested subscripts in multi-line type annotations
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
For a multi-line type annotation containing an error in a nested subscript, fastparse and nativeparse assign different line numbers to the resulting UnboundType. fastparse uses the outermost annotation line (self.line fixed at TypeConverter construction); nativeparse uses the actual per-node source line. Downstream, type-arg / arity errors reported against that node show up on different lines depending on which parser was used.
I ran into this as # type: ignore comments in serial mode became unused-ignore when trying parallel mode due to the line number difference.
To Reproduce
producer.py:
def foo() -> dict[
str,
dict[int, int, int],
]:
return {}
mypy.ini:
[mypy]
python_version = 3.12
rm -rf .mypy_cache && mypy --num-workers=0 producer.py # fastparse
rm -rf .mypy_cache && mypy --num-workers=0 --native-parser producer.py # nativeparse, no workers
Expected Behavior
Both parsers should agree on the reported line — either both line 1 (outer annotation) or both line 3 (inner offending node).
Actual Behavior
| invocation | reported line |
|---|---|
--num-workers=0 (fastparse, no native-parser) |
1 (outer dict[) |
--num-workers=0 --native-parser |
3 (inner dict[int, int, int]) |
Same error in both: "dict" expects 2 type arguments, but 3 given [type-arg].
Bisection notes:
- The divergence requires the error-bearing node to be a nested sub-expression. Top-level multi-line wrong-arity (
x: (\n dict[str, str, str]\n ) = {}) reports line 1 in both parsers. - Single-line wrong-arity has nothing to diverge on; both parsers agree.
Your Environment
- Mypy version used: 2.1.0 (PyPI). Also reproduces on current master (
2.2.0+dev.e53693be05a2). - Mypy command-line flags:
--native-parser selects the inner-line behavior; default (--num-workers=0, no--native-parser`) selects the outer-line behavior. - Mypy configuration:
python_version = 3.12. - Python version used: 3.12.13
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 with the fastparse and nativeparse paths around TypeConverter, using the producer.py example and the provided mypy commands to reproduce the differing lines. Trace how nested subscript source lines become UnboundType locations, then verify that both parsers report the same line and that the type-arg error and type-ignore behavior remain consistent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100