I believe the __future__ import makes any annotation a string, it
doesn't make Any magically resolvable later. If you don't
import Any into the scope of the annotation, it won't be resolved when
getting type hints.
Based on this, I believe:
1. It is incorrect to say "Any" will "work" by using from __futures__
import annotations. It will just be annotated as a string.
2. You should import anything that is expected to resolved within the
scope of the annotation (or you must supply globals/locals
to get_type_hints). So, this will work should:
from __future__ import annotations
from typing import Any
def fn(x: Any) -> Any:
...
Later, when evaluating the annotation with get_type_hints (possibly in
another module), it will resolve Any correctly.
On Mon, 2020-11-30 at 07:15 +0000, Steve Barnes wrote:
> The __future__ import only makes the Any type available for use
> during **annotations** i.e. if you follow the below with:
> In [6]: from typing import Any
>
> In [7]: get_type_hints(fn)
> Out[7]: {'argv': typing.Any, 'return': typing.Any}
>
> So the Any is out of scope for typing unless it is imported from
> there as well.
>
> From: Paul Bryan <[email protected]>
> Sent: 30 November 2020 06:30
> To: Steve Barnes <[email protected]>; Inada Naoki
> <[email protected]>; Abdulla Al Kathiri
> <[email protected]>
> Cc: python-ideas <[email protected]>
> Subject: Re: [Python-ideas] Re: Making "Any" a builtin
>
> pbryan@dynamo:~$ python3
> Python 3.8.6 (default, Sep 30 2020, 04:00:38)
> [GCC 10.2.0] on linux
> Type "help", "copyright", "credits" or "license" for more
> information.
> >>> from __future__ import annotations
> >>> def fn(*argv: Any) -> Any:
> ... ...
> ...
> >>> from typing import get_type_hints
> >>> get_type_hints(fn)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/lib/python3.8/typing.py", line 1264, in get_type_hints
> value = _eval_type(value, globalns, localns)
> File "/usr/lib/python3.8/typing.py", line 270, in _eval_type
> return t._evaluate(globalns, localns)
> File "/usr/lib/python3.8/typing.py", line 518, in _evaluate
> eval(self.__forward_code__, globalns, localns),
> File "<string>", line 1, in <module>
> NameError: name 'Any' is not defined
> >>>
>
>
> On Mon, 2020-11-30 at 06:10 +0000, Steve Barnes wrote:
> > Any only works as an annotation:
> >
> > In [3]: def fn(*argv: Any) -> Any:
> > ...: return argv[0]
> > ...:
> >
> >
> > From: Paul Bryan <[email protected]>
> > Sent: 30 November 2020 05:55
> > To: Inada Naoki <[email protected]>; Abdulla Al Kathiri
> > <[email protected]>
> > Cc: python-ideas <[email protected]>
> > Subject: [Python-ideas] Re: Making "Any" a builtin
> >
> > pbryan@dynamo:~$ python3
> > Python 3.8.6 (default, Sep 30 2020, 04:00:38)
> > [GCC 10.2.0] on linux
> > Type "help", "copyright", "credits" or "license" for more
> information.
> > >>> from __future__ import annotations
> > >>> Any
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > NameError: name 'Any' is not defined
> > >>>
> >
> > On Mon, 2020-11-30 at 14:43 +0900, Inada Naoki wrote:
> > > Since Python 3.10, you can use "Any" without "from typing import
> > > Any".
> > > You can do it in Python 3.7 by "from __future__ import
> > > annotations" too.
> > >
> > > See https://www.python.org/dev/peps/pep-0563/
> > >
> > > Regards,
> > >
> > > On Mon, Nov 30, 2020 at 12:29 AM Abdulla Al Kathiri
> > > <[email protected]> wrote:
> > > >
> > > > Instead of importing “Any" from the typing module, we can
> > > > annotate our functions with “Any" right away without the extra
> > > > step. What do you think? We have the builtin function “any”
> > > > which some Python users could mistakingly use, but static type
> > > > checkers should catch that.
> > > > _______________________________________________
> > > > Python-ideas mailing list -- [email protected]
> > > > To unsubscribe send an email to [email protected]
> > > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > > Message archived at
> > > >
> https://mail.python.org/archives/list/[email protected]/message/ELI474TKP2OKHP4NW5HOVUPKDPLYE2JP/
> > > > Code of Conduct: http://python.org/psf/codeofconduct/
> > >
> > >
> > >
> > > --
> > > Inada Naoki <[email protected]>
> > > _______________________________________________
> > > Python-ideas mailing list -- [email protected]
> > > To unsubscribe send an email to [email protected]
> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > Message archived at
> > >
> https://mail.python.org/archives/list/[email protected]/message/VENGRL6T54XQUYDXONZRZE7LUCO6MKWI/
> > > Code of Conduct: http://python.org/psf/codeofconduct/
> >
>
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/C3MADOUNVJEPUVRGQTUCAYUCLQPEBCKY/
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/DL7SZEHUL5L72ICUTFFLRYB2YNW7UBHE/
Code of Conduct: http://python.org/psf/codeofconduct/