On 28 May 2019, at 11:52, Ivan Levkivskyi <levkivskyi@gmail.com> wrote:
On Tue, 28 May 2019 at 04:37, Till <till.varoquaux@gmail.com> wrote: I didn't implement any good helper function because I couldn't figure out the idiomatic way to traverse type hints at runtime. It would be very easy to add a "get_extras(t: typing.<Type>) -> Tuple[Any, ...]" function. Would that work?
This reminds me about something I wanted to do for Python 3.8: there is an idea to add couple public helpers for introspection of typing objects. For example `get_origin()` and `get_args()`. That would essentially just thin public wrappers around private `__origin__`/`__args__` API.
With such API one will be able to just write `if get_origin(typ) is Annotated: ...`
-- Ivan
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/
This actually gave me the idea to test x.__origin__ is Annotated instead of isinstance(x, _Annotated) but with the current implementation _Annotated.__origin__ points to the type being annotated, not the Annotated class. I made a dirty typing_extensions pull request[1] that attempts to rectify this but I’m not sure i’ll be able to finish it myself right now. On an unrelated note, something I’ve been wondering about since yesterday – is Annotated supposed to be, in principle, allowed to be used like this? def fun(x: List[Annotated[int, ‘some marker’]]) Best, Jakub [1] https://github.com/python/typing/pull/634