python / python/mypy

Type annotation "List"[int] generates runtime error but is accepted by mypy

Open
#9,932 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low topic-quoted-annotations
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

This bug was just logged against pyright (and fixed): https://github.com/microsoft/pyright/issues/1393

I noticed that mypy also fails to report this error.

from typing import List
foo: "List"[int] = [1] # Accepted by mypy but generates runtime error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Python reproducer and compare mypy's result with the linked pyright issue. Trace how mypy handles a quoted "List" annotation followed by subscripting, then add a regression test demonstrating that the invalid annotation is reported rather than accepted. Done means the test passes and mypy no longer accepts this example silently.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.