| commit | c71fdb0bf97ca6a0aa3c0709d695c8ff9b778468 | [log] [tgz] |
|---|---|---|
| author | Aaron Wieczorek <aaronw@fastmail.com> | Mon Jan 05 21:15:13 2026 +0000 |
| committer | GitHub <noreply@github.com> | Mon Jan 05 13:15:13 2026 -0800 |
| tree | 9392af7b58a776ace7db717fff477b546ea219ff | |
| parent | c41dbd00718c1e873b7797ed7bf00126fd544df5 [diff] |
gh-20512: Fix specialization leak in generic TypedDict.update() (#20517) Fixes gh-20512 This PR fixes a bug where calling .update() on a specialized generic TypedDict like Group[int] would fail type checking because the plugin reverted to the unspecialized definition (Group[ValT]). After a lot of dead ends this one wasn't as straightforward as I thought initially. After spending a lot of time in `checkmember.py` and `checkexpr.py`I finally found the issue in `mypy/plugins/default.py`. The call got the anonymous version of the TypedDict from the TypeInfo, which pointed back to the original generic declaration. This caused specialized types to be replaced by their original TypeVar placeholders during the signature construction for update(). Whew! The fix basically uses `ctx.type` (the specialized type of TypedDict being updated) as the source for the items, then applies the anonymous fallback and sets `required_keys` to an empty set to keep the correct behavior of the update method. Added a test in `check-typeddict.test` which seemed like the right place. All tests pass, as well as the original repro script from the linked issue. --------- Co-authored-by: Aaron Wieczorek <woz@Aarons-MacBook-Pro.local> Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
We are always happy to answer questions! Here are some good places to ask them:
If you're just getting started, the documentation and type hints cheat sheet can also help answer questions.
If you think you've found a bug:
To report a bug or request an enhancement:
To discuss a new type system feature:
Mypy is a static type checker for Python.
Type checkers help ensure that you're using variables and functions in your code correctly. With mypy, add type hints (PEP 484) to your Python programs, and mypy will warn you when you use those types incorrectly.
Python is a dynamic language, so usually you'll only see errors in your code when you attempt to run it. Mypy is a static checker, so it finds bugs in your programs without even running them!
Here is a small example to whet your appetite:
number = input("What is your favourite number?") print("It is", number + 1) # error: Unsupported operand types for + ("str" and "int")
Adding type hints for mypy does not interfere with the way your program would otherwise run. Think of type hints as similar to comments! You can always use the Python interpreter to run your code, even if mypy reports errors.
Mypy is designed with gradual typing in mind. This means you can add type hints to your code base slowly and that you can always fall back to dynamic typing when static typing is not convenient.
Mypy has a powerful and easy-to-use type system, supporting features such as type inference, generics, callable types, tuple types, union types, structural subtyping and more. Using mypy will make your programs easier to understand, debug, and maintain.
See the documentation for more examples and information.
In particular, see:
Mypy can be installed using pip:
python3 -m pip install -U mypy
If you want to run the latest version of the code, you can install from the repo directly:
python3 -m pip install -U git+https://github.com/python/mypy.git
Now you can type-check the statically typed parts of a program like this:
mypy PROGRAM
You can always use the Python interpreter to run your statically typed programs, even if mypy reports type errors:
python3 PROGRAM
If you are working with large code bases, you can run mypy in daemon mode, that will give much faster (often sub-second) incremental updates:
dmypy run -- PROGRAM
You can also try mypy in an online playground (developed by Yusuke Miyazaki).
Mypy can be integrated into popular IDEs:
Additional information is available at the web site:
Jump straight to the documentation:
Follow along our changelog at:
https://mypy-lang.blogspot.com/
Help in testing, development, documentation and other tasks is highly appreciated and useful to the project. There are tasks for contributors of all experience levels.
To get started with developing mypy, see CONTRIBUTING.md.
Mypyc uses Python type hints to compile Python modules to faster C extensions. Mypy is itself compiled using mypyc: this makes mypy approximately 4 times faster than if interpreted!
To install an interpreted mypy instead, use:
python3 -m pip install --no-binary mypy -U mypy
To use a compiled version of a development version of mypy, directly install a binary from https://github.com/mypyc/mypy_mypyc-wheels/releases/latest.
To contribute to the mypyc project, check out the issue tracker at https://github.com/mypyc/mypyc