Short version
It isn’t a mypy bug – it’s a deliberate restriction.
dict.update must leave the left‑hand dictionary (ab) a valid value of
type AB.
From the point of view of the type‑checker that is not guaranteed when
the argument is a variable whose static type is A.
Why mypy complains
––––––––––––––––––
-
AB is
{'a': int, 'b': str}
-
A is
{'a': int}
but in the typing world a value whose static type is A could in fact be
any subclass of A:
class APlus(A): # legal subclass
c: float # ← new key that AB does not have
So the variable
a: A
might be an instance that actually contains the extra key "c".
-
If you wrote
ab.update(a) # “c” would be copied into ab at run‑time
the object referenced by ab would no longer conform to the type
annotation AB, because AB does not allow a key "c".
Since mypy can’t rule that situation out, it rejects the call and asks for
something that is known never to introduce foreign keys.
The safest type it can ask for is essentially “AB, but with all keys
optional”:
TypedDict({'a': int, 'b'?: str})
– a mapping that may or may not override either of AB’s own keys but
cannot contain any others.
Work‑arounds
––––––––––––
• If you really know the argument won’t contain extra keys, use a cast:
from typing import cast
ab.update(cast('dict[str, object]', a)) # silence mypy
# or, more explicit:
from typing_extensions import TypedDict
SubsetOfAB = TypedDict('SubsetOfAB', {'a': int, 'b': str}, total=False)
ab.update(cast(SubsetOfAB, a))
• Or build a literal that mypy can analyse:
ab.update({'a': a['a']}) # OK: mypy sees the literal directly
Take‑away
–––––––––
TypedDict.update is only accepted by mypy when the type of the argument
guarantees it cannot introduce keys outside the target TypedDict.
A variable whose declared type is another TypedDict with fewer required
keys does not provide that guarantee, because of possible subclasses with
additional keys.