Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upNo (public) way to dynamically introspect if an annotation is a TypedDict #751
Comments
|
This is reasonable. Can you show us a function that when added to typing.py would solve your problem? (E.g. is_typed_dict() -- how should it be implemented?) |
|
Simply: def is_typed_dict(cls: type) -> bool:
return isinstance(cls, _TypedDictMeta)would do. Is there a reason that we can't just allow |
|
PEP 589 explicitly rejects supporting Are you willing to make a contribution? There would actually have to be several -- this should be added to typing.py in the CPython 3.10 stdlib, to typing.py and typing_extensions in the typing repo. (Hm, maybe only to typing_extensions? typing.py there is only relevant for Python versions that are no longer supported, like 2.7 and 3.4.) |
https://github.com/python/typing/#workflow-for-cpython-changes says:
Is that note out of date then? |
|
Ouch, yes. That is totally out of date. The "master" copy of those files now lives in the CPython repo and the version here is only maintained (or at least not killed) for the benefit of Python 2.7 users of mypy and pytype. |
|
Done, see bpo-41792 |
Closes issue41792. Also closes python/typing#751.
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 preventissubclasson ithttps://github.com/python/cpython/blob/1b4552c/Lib/typing.py#L1901
Here is an example use-case:
This could be solved in one of the following ways:
issubclassonTypedDictsTypedDictitself and not subclasses of it, e.g. using a_rootclassvar likeNamedTupledoes for its magic_TypedDictMetapublic API (rename toTypedDictMeta)get_originwork onTypedDicts (not ideal because it isn't really a type)