Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen-max-2025-01-25· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

For this code [code] Mypy reports an error [code] Is this a bug in Mypy or is there…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    For this code

    from typing import TypedDict
    
    class AB(TypedDict):
      a: int
      b: str
    
    class A(TypedDict):
      a: int
    
    ab: AB = {"a": 1, "b": "2"}
    a: A = {"a": 3}
    
    ab.update(a)
    

    Mypy reports an error

    error: Argument 1 to "update" of "TypedDict" has incompatible type "A"; expected "TypedDict({'a': int, 'b'?: str})"  [typeddict-item]
    

    Is this a bug in Mypy or is there something to it?

  2. Qwen Assistant

    This is not a bug in Mypy; rather, it reflects the type-checking rules for TypedDict in Python. Let's break this down step by step to understand why Mypy raises this error and whether the code adheres to the expected behavior.


    Known Information

    1. TypedDict Definition:

      • AB is a TypedDict with two required keys: a (of type int) and b (of type str).
      • A is a TypedDict with one required key: a (of type int).
    2. Variable Assignments:

      • ab is an instance of AB, initialized with {"a": 1, "b": "2"}.
      • a is an instance of A, initialized with {"a": 3}.
    3. The update Method:

      • The update method is used to merge dictionaries. For TypedDict, it expects the argument to be compatible with the structure of the TypedDict being updated.
    4. Mypy Error:

      • Mypy reports that the argument to ab.update(a) is incompatible. Specifically, it expects a TypedDict with at least the key a (of type int) and optionally the key b (of type str), but it finds a TypedDict with only the key a.

    Why Does Mypy Raise This Error?

    The key issue lies in how TypedDict enforces type safety. A TypedDict is designed to ensure that the dictionary adheres strictly to its defined schema, including both the presence and types of keys.

    • When you call ab.update(a), you are attempting to update the ab dictionary (of type AB) with the contents of a (of type A).
    • However, A does not include the key b, which is required in AB. Even though a contains a valid value for the key a, it does not satisfy the full schema of AB.

    From Mypy's perspective, this operation could potentially leave ab in an invalid state. After the update, ab might lack the required key b, violating the AB schema. Therefore, Mypy flags this as an error.


    Is This Behavior Correct?

    Yes, this behavior is correct and aligns with the design principles of TypedDict. The purpose of TypedDict is to enforce strict type checking for dictionary structures, ensuring that all required keys are present and have the correct types. Allowing an incomplete TypedDict (like A) to update a more complete one (like AB) would undermine this guarantee.


    How to Resolve the Issue?

    If you want to update ab with the contents of a, you need to ensure that the resulting dictionary still satisfies the AB schema. One way to achieve this is to explicitly provide a value for the missing key b when performing the update. For example:

    ab.update({**a, "b": ab["b"]})
    

    Here:

    • {**a, "b": ab["b"]} creates a new dictionary that includes all keys from a and explicitly retains the existing value of b from ab.
    • This ensures that the resulting dictionary still conforms to the AB schema.

    Alternatively, if you are certain that the update will not violate the schema, you can use a type ignore comment to suppress the Mypy error:

    ab.update(a)  # type: ignore
    

    However, this approach should be used sparingly, as it bypasses type checking and could lead to runtime errors if the schema is violated.


    Conclusion

    The error reported by Mypy is valid and reflects the strict type-checking rules for TypedDict. To resolve the issue, you should ensure that the update operation preserves the required structure of the AB schema. If you choose to suppress the error, do so with caution and only when you are confident that the operation is safe.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.