Skip to content

fastparse and native parser assign different line numbers to nested subscripts in multi-line type annotations #21480

@sunny-zuo

Description

@sunny-zuo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions