You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to test type compatibility in Python 3.7? #570
This is expected as mentioned in PEP560. So I'm not saying I should continue to write code like that.
But since it's still useful to inspect whether type is a list or not (e.g. when you inspect the object's type hints and generate a default value for it), I wonder whether these is already a known way to test such compatibility.
One possible solution is to use typing_inspect library on PyPI, it has a get_origin() function which you can use as issubclass(get_origin(type_info), list).
But I think maybe it is time consider adding some basic introspection to typing? From all recent similar requests and general experience with typing_inspect it looks like people are interested mostly in two things:
Get an "erased" version of a type, i.e. a runtime class equivalent of a given type
Get type arguments from a given type
@gvanrossum Does it make sense to add such two helper functions that would be essentially public wrappers around internal APIs like .__origin__ and .__args__?
Yes. I like that we make these things functions in typing rather than special attributes or methods -- it's much easier for a function to do the right thing when the type given to it is some built-in or user-defined type rather than one of the special one from typing. It should also deal with forward reference strings.
.__origin__ is already very helpful. Thanks! Didn't know that.
Besides the two features you suggest, I guess testing type (either actual type or typing's type variable, like _GenericAlias) compatibility would also be useful. If this would be automatically supported by those two features, it would still be worth mentioning in that particular release's notes. Thanks!
Activity
[-]How to test type compatibility in Python 3?[/-][+]How to test type compatibility in Python 3.7?[/+]ilevkivskyi commentedon Jul 9, 2018
One possible solution is to use
typing_inspectlibrary on PyPI, it has aget_origin()function which you can use asissubclass(get_origin(type_info), list).But I think maybe it is time consider adding some basic introspection to
typing? From all recent similar requests and general experience withtyping_inspectit looks like people are interested mostly in two things:@gvanrossum Does it make sense to add such two helper functions that would be essentially public wrappers around internal APIs like
.__origin__and.__args__?gvanrossum commentedon Jul 9, 2018
Yes. I like that we make these things functions in
typingrather than special attributes or methods -- it's much easier for a function to do the right thing when the type given to it is some built-in or user-defined type rather than one of the special one fromtyping. It should also deal with forward reference strings.Kimi-Arthur commentedon Jul 9, 2018
.__origin__is already very helpful. Thanks! Didn't know that.Besides the two features you suggest, I guess testing type (either actual type or typing's type variable, like _GenericAlias) compatibility would also be useful. If this would be automatically supported by those two features, it would still be worth mentioning in that particular release's notes. Thanks!
tfontConverter: use issubclass for __origin__
ilevkivskyi commentedon Jun 20, 2019
get_origin()(an official public API for.__origin__) got intotypingin Python 3.8, so I think this can be closed now.