Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

No (public) way to dynamically introspect if an annotation is a TypedDict #751

Closed
@pxeger

Description

@pxeger

I want to know the Origin of an annotation. This works fine for basically everything, but there is no way to check if an annotation is a TypedDict subclass without resorting to (semantically) private API (by checking if it is an instance of_TypedDictMeta), becauseissubclass(x, TypedDict) fails. I believe this is because you don't want people thinkingisinstance(x, TypedDict) will work to actually validate the keys on a dict. However, I don't think there's any reason to preventissubclass on it

https://github.com/python/cpython/blob/1b4552c/Lib/typing.py#L1901

Here is an example use-case:

fromtypingimportDict,get_origindefvalidate(data,annotation)->bool:"""check if `data` conforms to the type given in `annotation`"""# this worksifget_origin(annotation)isDict:returnisinstance(data,dict)# but this doesn't...elifis_a_typed_dict(annotation):returnisinstance(data,dict)andvalidate_typeddict_keys(data,annotation)    ...

This could be solved in one of the following ways:

  • allowissubclass onTypedDicts
    • or allow it only onTypedDict itself and not subclasses of it, e.g. using a_root classvar likeNamedTuple does for its magic
      • although I see no reason not to allow it in that case either
  • make_TypedDictMeta public API (rename toTypedDictMeta)
  • makeget_origin work onTypedDicts (not ideal because it isn't really a type)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp