This is an excellent observation, and it highlights a subtle but important distinction in how TypedDict types are treated by mypy. Let’s carefully analyze why ab.update(ab) does not raise an error, while ab.update(a) does, and determine whether this behavior is consistent or indicative of a bug.
Known Information Recap
-
AB and A Definitions:
AB is a TypedDict with required keys a: int and b: str.
A is a TypedDict with only the required key a: int.
-
Behavior Observed:
ab.update(a) raises a mypy error because a has the type A, and mypy cannot guarantee that a does not contain extra keys (due to potential subclasses of A).
ab.update(ab) does not raise an error, even though ab has the type AB, and it could theoretically have extra keys if AB were subclassed.
-
Key Question:
- Why does mypy treat
ab.update(ab) differently from ab.update(a)? Is this behavior consistent, or is it a bug?
Analysis of ab.update(ab)
When you call ab.update(ab), mypy does not raise an error because it assumes that ab is a valid instance of AB. This assumption is based on the following reasoning:
-
Static Type of ab:
- The variable
ab is explicitly annotated as having the type AB. This means mypy treats ab as conforming strictly to the AB type unless there is evidence to the contrary.
-
No Subclassing Risk for ab:
- While it is true that
AB could theoretically be subclassed to add extra keys, mypy does not consider this possibility when analyzing ab.update(ab). This is because ab is already declared as being of type AB, and mypy assumes that the runtime value of ab matches its static type exactly.
- In other words, mypy trusts that
ab is a valid AB and does not contain any extra keys beyond those defined in AB.
-
Self-Update Guarantee:
- When you call
ab.update(ab), the operation is effectively a no-op because ab is updating itself with its own keys. Since ab is already known to conform to AB, mypy concludes that the update operation cannot introduce invalid keys.
Comparison with ab.update(a)
In contrast, when you call ab.update(a), mypy raises an error because:
-
Static Type of a:
- The variable
a is annotated as having the type A. While A is a subset of AB, mypy cannot guarantee that a does not contain extra keys. This is because A could be subclassed to include additional keys that are not part of AB.
-
Subclassing Risk for a:
- Unlike
ab, which is explicitly declared as AB, a is declared as A. Mypy must account for the possibility that a is an instance of a subclass of A that includes extra keys. If such extra keys were present, calling ab.update(a) could corrupt the AB type by introducing invalid keys.
Consistency Check
The behavior of ab.update(ab) and ab.update(a) is consistent with mypy's design principles for TypedDict. Here’s why:
-
Trust in Static Types:
- Mypy trusts that a variable declared with a specific
TypedDict type adheres to that type at runtime. For ab, which is explicitly declared as AB, mypy assumes it contains only the keys defined in AB.
-
Cautious Treatment of Subtypes:
- For
a, which is declared as A, mypy is cautious because A could be subclassed to include extra keys. This caution ensures that ab.update(a) does not inadvertently introduce invalid keys into ab.
-
Self-Update Special Case:
- The case of
ab.update(ab) is special because mypy knows that ab is already a valid AB. Since ab is updating itself with its own keys, mypy can safely assume that no invalid keys will be introduced.
Conclusion
This behavior is not a bug but rather a deliberate design choice in mypy to balance type safety and practicality. While it may seem inconsistent at first glance, the distinction arises from mypy's treatment of variables with explicit TypedDict types versus variables with potentially subclassed TypedDict types.
If you find this behavior unintuitive, you can use workarounds like casting or constructing a literal dictionary to make your intentions explicit to mypy. However, the current behavior aligns with mypy's goal of ensuring type safety for TypedDict operations.