El jue, 21 oct 2021 a las 10:31, Carl Meyer (<c...@oddbird.net>) escribió:

> On Thu, Oct 21, 2021 at 10:44 AM Damian Shaw
> <damian.peter.s...@gmail.com> wrote:
> > Sorry for the naive question but why doesn't "TYPE_CHECKING" work under
> PEP 649?
> >
> > I think I've seen others mention this but as the code object isn't
> executed until inspected then if you are just using annotations for type
> hints it should work fine?
> >
> > Is the use case wanting to use annotations for type hints and real time
> inspection but you also don't want to import the objects at run time?
>
> Yes, you're right. And I don't think PEP 649 and PEP 563 are really
> all that different in this regard: if you have an annotation using a
> non-imported name, you'll be fine as long as you don't introspect it
> at runtime. If you do, you'll get a NameError. And with either PEP you
> can work around this if you need to by ensuring you do the imports
> first if you're going to need the runtime introspection of the
> annotations.
>
> The difference is that PEP 563 makes it easy to introspect the
> annotation _as a string_ without triggering NameError, and PEP 649
> takes that away, but I haven't seen anyone describe a really
> compelling use case for that.
>
> I would want this for my type checker, pyanalyze. I'd want to get the raw
annotation and turn it into a type. For example, if the annotation is
`List[SomeType]` and `SomeType` is imported in `if TYPE_CHECKING`, I'd at
least still be able to extract `List[Any]` instead of bailing out
completely. With some extra analysis work on the module I could even get
the real type out of the `if TYPE_CHECKING` block.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/Y2647QWRAOYLL22UWZL37DKAEUK22UIC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to