Bug report
from __future__ import annotations appears to break TypedDict required/optional keys ending up in __required_keys__ and __optional_keys__. mypy works as expected though.
Using the example from https://peps.python.org/pep-0655/#usage-in-python-3-11 as the base
$ cat t.py
from __future__ import annotations
from typing_extensions import NotRequired, TypedDict
class Dog(TypedDict):
name: str
owner: NotRequired[str]
print("required", Dog.__required_keys__)
print("optional", Dog.__optional_keys__)
$ python3 t.py
required frozenset({'name', 'owner'})
optional frozenset()
With the __future__ import removed, works as expected:
$ cat t.py
from typing_extensions import NotRequired, TypedDict
class Dog(TypedDict):
name: str
owner: NotRequired[str]
print("required", Dog.__required_keys__)
print("optional", Dog.__optional_keys__)
$ python3 t.py
required frozenset({'name'})
optional frozenset({'owner'})
Note: breaks across different variations of total and Required/NotRequired and typing_extensions vs typing imports, above is just one example.
https://peps.python.org/pep-0655/#how-to-teach-this contains an example with the __future__ annotations import in place with no mention that it would not cause __required_keys__ and __optional_keys__ becoming populated as expected, so I'm assuming this is a bug.
Your environment
Bug report
from __future__ import annotationsappears to breakTypedDictrequired/optional keys ending up in__required_keys__and__optional_keys__. mypy works as expected though.Using the example from https://peps.python.org/pep-0655/#usage-in-python-3-11 as the base
With the
__future__import removed, works as expected:Note: breaks across different variations of
totalandRequired/NotRequiredandtyping_extensionsvstypingimports, above is just one example.https://peps.python.org/pep-0655/#how-to-teach-this contains an example with the
__future__annotations import in place with no mention that it would not cause__required_keys__and__optional_keys__becoming populated as expected, so I'm assuming this is a bug.Your environment
CPython versions tested on:
NotRequiredandTypedDictimports fromtyping_extensionstyping_extensionstyping_extensionstypingOperating system and architecture: Linux x86_64