Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic typeddict may corrupt the cache #14638

Closed
Synss opened this issue Feb 7, 2023 · 0 comments · Fixed by #14675
Closed

Generic typeddict may corrupt the cache #14638

Synss opened this issue Feb 7, 2023 · 0 comments · Fixed by #14675
Labels

Comments

@Synss
Copy link

Synss commented Feb 7, 2023

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 from mypy_extensions on Python 3.10.

I could also reproduce that under Linux (Ubuntu 22.04) and MacOS (M1---Python installed via MacPorts).

@Synss Synss added the bug mypy got something wrong label Feb 7, 2023
@hauntsaninja hauntsaninja mentioned this issue Feb 7, 2023
17 tasks
JukkaL pushed a commit that referenced this issue Feb 14, 2023
Fixes #14638 

TBH I don't remember why do we need to create the "incomplete" type
alias (with empty type variables), and set up type variables later. But
I didn't want to risk a larger refactoring and just fixed the missing
calls surfaced by the issue instead.
ilinum pushed a commit to ilinum/mypy that referenced this issue Feb 14, 2023
Fixes python#14638

TBH I don't remember why do we need to create the "incomplete" type
alias (with empty type variables), and set up type variables later. But
I didn't want to risk a larger refactoring and just fixed the missing
calls surfaced by the issue instead.
CheckmkCI pushed a commit to Checkmk/checkmk that referenced this issue Feb 23, 2023
mypy 1.0.1 fixed python/mypy#14638

Change-Id: I751778a7499f80b01a6dc8d72b3c5fea124c318e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants