python / python/mypy

A generic function taking a TypedDict type erroneously reports missing __required_keys__

Open
#17,365 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When calling a generic function taking a type variable bound to a typed dictionary type, MyPy reports that the typed dictionary type does not have __required_keys__ and __optional_keys__.

Pyright does not report any errors for the same code.

To Reproduce

from typing import Any, TypedDict, TypeVar, TypeGuard, Mapping

class _A(TypedDict):
    i: int
    
class _B(TypedDict):
    s: str
    
TD = TypeVar("TD", _A,_B)

def is_ab(td: type[TD], doc: Mapping[str, Any]) -> bool:
    for k in doc.keys():
        if k not in td.__required_keys__ and k not in td.__optional_keys__:
            return False
            
    for k in td.__required_keys__:
        if k not in doc:
            return False
            
    return True

def is_a(doc: Mapping[str, Any]) -> TypeGuard[_A]:
    return is_ab(_A, doc)

def is_b(doc: Mapping[str, Any]) -> TypeGuard[_B]:
    return is_ab(_B, doc)

xa: Mapping[str, Any] = {"i": 1}
xb: Mapping[str, Any] = {"s": "B"}

if is_a(xa):
  a: _A = xa

if is_b(xb):
  b: _B = xb

Expected Behavior

No errors should be reported because TD is bound to either _A or _B, which have all special TypedDict attributes.

Actual Behavior

When running MyPy against the code above, reports these errors:

mypy --strict td.py
td.py:13: error: "type[_A]" has no attribute "__required_keys__"  [attr-defined]
td.py:13: error: "type[_B]" has no attribute "__required_keys__"  [attr-defined]
td.py:13: error: "type[_A]" has no attribute "__optional_keys__"  [attr-defined]
td.py:13: error: "type[_B]" has no attribute "__optional_keys__"  [attr-defined]
td.py:16: error: "type[_A]" has no attribute "__required_keys__"  [attr-defined]
td.py:16: error: "type[_B]" has no attribute "__required_keys__"  [attr-defined]
Found 6 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0 on Windows
  • Mypy command-line flags: --strict (same behavior without)
  • Mypy configuration options from mypy.ini (and other config files): not used
  • Python version used: 3.10

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the td.py reproducer and the handling of generic functions whose type variables range over TypedDict types. Confirm the current attr-defined errors under --strict, then add a regression test showing that required_keys and optional_keys are accepted for _A and _B and that the reproducer reports no errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.