-
Notifications
You must be signed in to change notification settings - Fork 259
No (public) way to dynamically introspect if an annotation is a TypedDict #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
While the given reason is valid for |
Closes issue41792. Also closes python/typing#751.
Uh oh!
There was an error while loading. Please reload this page.
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 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:
issubclass
onTypedDict
sTypedDict
itself and not subclasses of it, e.g. using a_root
classvar likeNamedTuple
does for its magic_TypedDictMeta
public API (rename toTypedDictMeta
)get_origin
work onTypedDict
s (not ideal because it isn't really a type)The text was updated successfully, but these errors were encountered: