google / pytype

A static type analyzer for Python code

Home Page:https://google.github.io/pytype

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`typing_extensions.Required` (PEP 655) unsupported

srittau opened this issue · comments

This came up in python/typeshed#11078. It seems that Required is currently unsupported by pytype and causes the an error. I tried to add the following TypedDict to typeshed:

from typing_extensions import Required

...

class _DictConfigArgs(TypedDict, total=False):
    version: Required[Literal[1]]
    formatters: dict[str, _FormatterConfiguration]
    filters: dict[str, _FilterConfiguration]
    handlers: dict[str, _HandlerConfiguration]
    loggers: dict[str, _LoggerConfiguration]
    root: _RootLoggerConfiguration
    incremental: bool
    disable_existing_loggers: bool

This causes the typeshed pytype tests to fail:

stdlib/logging/config.pyi (3.11): pytype.pytd.visitors.ContainerError: Class typing.Required is not a container

Inverting the TypedDict works:

from typing_extensions import NotRequired

...

class _DictConfigArgs(TypedDict):
    version: Literal[1]
    formatters: NotRequired[dict[str, _FormatterConfiguration]]
    filters: NotRequired[dict[str, _FilterConfiguration]]
    handlers: NotRequired[dict[str, _HandlerConfiguration]]
    loggers: NotRequired[dict[str, _LoggerConfiguration]]
    root: NotRequired[_RootLoggerConfiguration]
    incremental: NotRequired[bool]
    disable_existing_loggers: NotRequired[bool]

I did not test with typing.Required, only with typing_extensions.Required, though.