Description
Bug Report
Using a generic TypedDict
in another module may corrupt the cache. The cache doesn't recover and must be deleted.
To Reproduce
In module a
, define a generic TypedDict
,
# a.py
from typing import TypedDict, Generic, TypeVar
TValue = TypeVar("TValue")
class Dict(TypedDict, Generic[TValue]):
value: TValue
in another module (say b
) use this definition. The easiest way to trigger the bug is to have at least one typing error in the file (here g()
).
# b.py
from a import Dict, TValue
def f(d: Dict[TValue]) -> TValue:
return d["value"]
def g(d: Dict[TValue]) -> TValue:
return d["x"] # error
Now, type check b
$ mypy b.py
b.py:8: error: TypedDict "a.Dict[TValue]" has no key "x" [typeddict-item]
Found 1 error in 1 file (checked 1 source file)
as expected.
However, running mypy b.py
a second time reports an extra [type-arg]
and a few more related errors as well,
$ mypy b.py
b.py:3: error: "Dict" expects no type arguments, but 1 given [type-arg]
b.py:3: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
b.py:4: error: Incompatible return value type (got "TValue", expected "TValue") [return-value]
b.py:7: error: "Dict" expects no type arguments, but 1 given [type-arg]
b.py:7: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
b.py:8: error: TypedDict "a.Dict[TValue]" has no key "x" [typeddict-item]
Found 6 errors in 1 file (checked 1 source file)
from then on, the only way to get rid of the [type-arg]
error is to run with --no-increment
or delete the cache. Here, I removed g()
, leaving
# b.py [edited: deleted definition of g()]
from a import Dict, TValue
def f(d: Dict[TValue]) -> TValue:
return d["value"]
and
$ mypy b.py
b.py:3: error: "Dict" expects no type arguments, but 1 given [type-arg]
b.py:3: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
b.py:4: error: Incompatible return value type (got "TValue", expected "TValue") [return-value]
Found 3 errors in 1 file (checked 1 source file)
but
$ mypy --no-increment b.py
Success: no issues found in 1 source file
Expected Behavior
No difference between mypy
, mypy --no-increment
and the generic TypedDict
do not corrupt the cache.
Actual Behavior
mypy reports false positives
b.py:3: error: "Dict" expects no type arguments, but 1 given [type-arg]
b.py:3: error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
b.py:4: error: Incompatible return value type (got "TValue", expected "TValue") [return-value]
Your Environment
- Mypy version used: 0.991, 1.0.0, master
- Mypy command-line flags:
mypy
vs.mypy --no-increment
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used: 3.11 or importing
TypedDict
frommypy_extensions
on Python 3.10.
I could also reproduce that under Linux (Ubuntu 22.04) and MacOS (M1---Python installed via MacPorts).