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.
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 <alkathiri.abdulla@gmail.com> 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 -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474... Code of Conduct: http://python.org/psf/codeofconduct/
-- Inada Naoki <songofacandy@gmail.com>
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 <alkathiri.abdulla@gmail.com> 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 -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474... Code of Conduct: http://python.org/psf/codeofconduct/
-- Inada Naoki <songofacandy@gmail.com> _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/VENGRL... Code of Conduct: http://python.org/psf/codeofconduct/
Oh, note that Abdulla said: "we can annotate our **functions** with “Any" right away without the extra step." Python 3.9.0 (default, Nov 21 2020, 14:01:55)
from __future__ import annotations def foo(a: Any, b: Dict[Any, Any]) -> Any: pass
-- Inada Naoki <songofacandy@gmail.com>
Any only works as an annotation: In [3]: def fn(*argv: Any) -> Any: ...: return argv[0] ...: From: Paul Bryan <pbryan@anode.ca> Sent: 30 November 2020 05:55 To: Inada Naoki <songofacandy@gmail.com>; Abdulla Al Kathiri <alkathiri.abdulla@gmail.com> Cc: python-ideas <python-ideas@python.org> Subject: [Python-ideas] Re: Making "Any" a builtin pbryan@dynamo<mailto: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/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.python.org%2Fdev%2Fpeps%2Fpep-0563%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705057013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=QKqmO0f%2BLE040QGySslUfYKGGjyVPIbqVndWLwLHEe4%3D&reserved=0> Regards, On Mon, Nov 30, 2020 at 12:29 AM Abdulla Al Kathiri <alkathiri.abdulla@gmail.com<mailto:alkathiri.abdulla@gmail.com>> 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 -- python-ideas@python.org<mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org<mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman3%2Flists%2Fpython-ideas.python.org%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705057013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=H%2BrsVsj%2BgLzD0JEROxT8tZb%2Bgn6zua0Hc%2BBCsm%2Fo%2FE4%3D&reserved=0> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474TKP2OKHP4NW5HOVUPKDPLYE2JP/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Farchives%2Flist%2Fpython-ideas%40python.org%2Fmessage%2FELI474TKP2OKHP4NW5HOVUPKDPLYE2JP%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705057013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=fTeCdpijNUBREFSHnA%2FYqD0dkEUq0g48xVHjiYCUMHc%3D&reserved=0> Code of Conduct: http://python.org/psf/codeofconduct/<https://eur05.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705067008%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=FHpZxOJuTA6gUkhUqwkbtZP05jBTrRliCDAG%2BfOzwyo%3D&reserved=0> -- Inada Naoki <songofacandy@gmail.com<mailto:songofacandy@gmail.com>> _______________________________________________ Python-ideas mailing list -- python-ideas@python.org<mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org<mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman3%2Flists%2Fpython-ideas.python.org%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705067008%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=gBFYebL5JU%2BF0R1xo4c1Ynn6WbX0CiJtgFsomRFkLcg%3D&reserved=0> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/VENGRL6T54XQUYDXONZRZE7LUCO6MKWI/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Farchives%2Flist%2Fpython-ideas%40python.org%2Fmessage%2FVENGRL6T54XQUYDXONZRZE7LUCO6MKWI%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705077002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=iCFcrLAsqsph9RfW%2BcybewCKo76feAs6Y0haKRmi6%2FY%3D&reserved=0> Code of Conduct: http://python.org/psf/codeofconduct/<https://eur05.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705077002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=%2F7zbYkgPIZg1rpyFnSbRjyk5%2Fxyv0ByZPD6MUstQ6EM%3D&reserved=0>
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 <pbryan@anode.ca> Sent: 30 November 2020 05:55 To: Inada Naoki <songofacandy@gmail.com>; Abdulla Al Kathiri <alkathiri.abdulla@gmail.com> Cc: python-ideas <python-ideas@python.org> 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 <alkathiri.abdulla@gmail.com> 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 -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474...
Code of Conduct: http://python.org/psf/codeofconduct/ -- Inada Naoki <songofacandy@gmail.com>
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/VENGRL...
Code of Conduct: http://python.org/psf/codeofconduct/
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 <pbryan@anode.ca> Sent: 30 November 2020 06:30 To: Steve Barnes <GadgetSteve@live.co.uk>; Inada Naoki <songofacandy@gmail.com>; Abdulla Al Kathiri <alkathiri.abdulla@gmail.com> Cc: python-ideas <python-ideas@python.org> Subject: Re: [Python-ideas] Re: Making "Any" a builtin pbryan@dynamo<mailto: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 <pbryan@anode.ca<mailto:pbryan@anode.ca>> Sent: 30 November 2020 05:55 To: Inada Naoki <songofacandy@gmail.com<mailto:songofacandy@gmail.com>>; Abdulla Al Kathiri <alkathiri.abdulla@gmail.com<mailto:alkathiri.abdulla@gmail.com>> Cc: python-ideas <python-ideas@python.org<mailto:python-ideas@python.org>> Subject: [Python-ideas] Re: Making "Any" a builtin pbryan@dynamo<mailto: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/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.python.org%2Fdev%2Fpeps%2Fpep-0563%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705057013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=QKqmO0f%2BLE040QGySslUfYKGGjyVPIbqVndWLwLHEe4%3D&reserved=0> Regards, On Mon, Nov 30, 2020 at 12:29 AM Abdulla Al Kathiri <alkathiri.abdulla@gmail.com<mailto:alkathiri.abdulla@gmail.com>> 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 -- python-ideas@python.org<mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org<mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman3%2Flists%2Fpython-ideas.python.org%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705057013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=H%2BrsVsj%2BgLzD0JEROxT8tZb%2Bgn6zua0Hc%2BBCsm%2Fo%2FE4%3D&reserved=0> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474TKP2OKHP4NW5HOVUPKDPLYE2JP/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Farchives%2Flist%2Fpython-ideas%40python.org%2Fmessage%2FELI474TKP2OKHP4NW5HOVUPKDPLYE2JP%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705057013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=fTeCdpijNUBREFSHnA%2FYqD0dkEUq0g48xVHjiYCUMHc%3D&reserved=0> Code of Conduct: http://python.org/psf/codeofconduct/<https://eur05.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705067008%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=FHpZxOJuTA6gUkhUqwkbtZP05jBTrRliCDAG%2BfOzwyo%3D&reserved=0> -- Inada Naoki <songofacandy@gmail.com<mailto:songofacandy@gmail.com>> _______________________________________________ Python-ideas mailing list -- python-ideas@python.org<mailto:python-ideas@python.org> To unsubscribe send an email to python-ideas-leave@python.org<mailto:python-ideas-leave@python.org> https://mail.python.org/mailman3/lists/python-ideas.python.org/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman3%2Flists%2Fpython-ideas.python.org%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705067008%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=gBFYebL5JU%2BF0R1xo4c1Ynn6WbX0CiJtgFsomRFkLcg%3D&reserved=0> Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/VENGRL6T54XQUYDXONZRZE7LUCO6MKWI/<https://eur05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Farchives%2Flist%2Fpython-ideas%40python.org%2Fmessage%2FVENGRL6T54XQUYDXONZRZE7LUCO6MKWI%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705077002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=iCFcrLAsqsph9RfW%2BcybewCKo76feAs6Y0haKRmi6%2FY%3D&reserved=0> Code of Conduct: http://python.org/psf/codeofconduct/<https://eur05.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=04%7C01%7C%7Cf27001db4fda41a8422008d894f63854%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637423132705077002%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=%2F7zbYkgPIZg1rpyFnSbRjyk5%2Fxyv0ByZPD6MUstQ6EM%3D&reserved=0>
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 <pbryan@anode.ca> Sent: 30 November 2020 06:30 To: Steve Barnes <GadgetSteve@live.co.uk>; Inada Naoki <songofacandy@gmail.com>; Abdulla Al Kathiri <alkathiri.abdulla@gmail.com> Cc: python-ideas <python-ideas@python.org> 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 <pbryan@anode.ca> Sent: 30 November 2020 05:55 To: Inada Naoki <songofacandy@gmail.com>; Abdulla Al Kathiri <alkathiri.abdulla@gmail.com> Cc: python-ideas <python-ideas@python.org> 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 <alkathiri.abdulla@gmail.com> 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 -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474...
Code of Conduct: http://python.org/psf/codeofconduct/ -- Inada Naoki <songofacandy@gmail.com>
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/VENGRL...
Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/C3MADO... Code of Conduct: http://python.org/psf/codeofconduct/
On Mon, Nov 30, 2020 at 4:27 PM Paul Bryan <pbryan@anode.ca> wrote:
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.
I don't say it works 100% use cases. But when you want to use type checker (mypy, pyre), or code completions (e.g. pylance), it "works". When you need to support typing.get_type_hints, you can still `from typing import Any`. And we can consider resolving typing.Any (and any other globals) in typing.get_type_hints(), instead of adding Any to builtins. As Abdulla said, having both `any` and `Any` in builtins makes Python more confusing. If typing.get_type_hints() is the problem, why don't we changing typing.get_type_hints() behavior? Regards, -- Inada Naoki <songofacandy@gmail.com>
In python 3.9, I get the following error: In python 3.10, I get no runtime errors: However, Pylance (I am not sure about mypy) didn’t recognize Any. Instead it made it “Unknown”. In fact, I can put anything in 3.10 annotation and it runs just fine. I am not sure if this is the intention of Python3.10
On Nov 30, 2020, at 11:46 AM, Inada Naoki <songofacandy@gmail.com> wrote:
I don't say it works 100% use cases. But when you want to use type checker (mypy, pyre), or code completions (e.g. pylance), it "works". When you need to support typing.get_type_hints, you can still `from typing import Any`.
And we can consider resolving typing.Any (and any other globals) in typing.get_type_hints(), instead of adding Any to builtins.
As Abdulla said, having both `any` and `Any` in builtins makes Python more confusing. If typing.get_type_hints() is the problem, why don't we changing typing.get_type_hints() behavior?
On Mon, Nov 30, 2020 at 05:19:13PM +0400, Abdulla Al Kathiri wrote:
In python 3.9, I get the following error:
In python 3.10, I get no runtime errors:
Did you forget to paste the text? Or did gmail eat it?
However, Pylance (I am not sure about mypy) didn’t recognize Any. Instead it made it “Unknown”.
That's probably a question for the Pylance devs, but my guess is that anything flagged as "Any" type is the same as flagging it as "I don't know what that type is".
In fact, I can put anything in 3.10 annotation and it runs just fine. I am not sure if this is the intention of Python3.10
Yes, Python 3.10 makes the `from __future__ import annotations` behaviour occur automatically. See PEP 563: https://www.python.org/dev/peps/pep-0563/ (At least, I'm guessing that's what is happening here, it has to be a guess because I can't see the code you run and the error you get.) -- Steve
They pasted the code / results as screenshots. But you got the gist. In the future, it's really better to use plain text for email lists. -CHB On Tue, Dec 1, 2020 at 9:33 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Nov 30, 2020 at 05:19:13PM +0400, Abdulla Al Kathiri wrote:
In python 3.9, I get the following error:
In python 3.10, I get no runtime errors:
Did you forget to paste the text? Or did gmail eat it?
However, Pylance (I am not sure about mypy) didn’t recognize Any. Instead it made it “Unknown”.
That's probably a question for the Pylance devs, but my guess is that anything flagged as "Any" type is the same as flagging it as "I don't know what that type is".
In fact, I can put anything in 3.10 annotation and it runs just fine. I am not sure if this is the intention of Python3.10
Yes, Python 3.10 makes the `from __future__ import annotations` behaviour occur automatically. See PEP 563:
https://www.python.org/dev/peps/pep-0563/
(At least, I'm guessing that's what is happening here, it has to be a guess because I can't see the code you run and the error you get.)
-- Steve _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YMJ5NM... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
I still like the approach of doing `import typing as t` - (or other short name of ones preference). Bcause in the end, any argument used to bring "Any" as a built-in could be used in favor of any other element in typing anway. Typing "t.Any, t.Union, t.Optional" is not a hassle and immediately readable, even by someone who just opened the file and skipped the "import" session. No need for tens of names from "typing.*" to be on the module namespace (or builtins). On Wed, 2 Dec 2020 at 02:41, Christopher Barker <pythonchb@gmail.com> wrote:
They pasted the code / results as screenshots.
But you got the gist.
In the future, it's really better to use plain text for email lists.
-CHB
On Tue, Dec 1, 2020 at 9:33 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Nov 30, 2020 at 05:19:13PM +0400, Abdulla Al Kathiri wrote:
In python 3.9, I get the following error:
In python 3.10, I get no runtime errors:
Did you forget to paste the text? Or did gmail eat it?
However, Pylance (I am not sure about mypy) didn’t recognize Any. Instead it made it “Unknown”.
That's probably a question for the Pylance devs, but my guess is that anything flagged as "Any" type is the same as flagging it as "I don't know what that type is".
In fact, I can put anything in 3.10 annotation and it runs just fine. I am not sure if this is the intention of Python3.10
Yes, Python 3.10 makes the `from __future__ import annotations` behaviour occur automatically. See PEP 563:
https://www.python.org/dev/peps/pep-0563/
(At least, I'm guessing that's what is happening here, it has to be a guess because I can't see the code you run and the error you get.)
-- Steve _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YMJ5NM... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD
Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UGRPHA... Code of Conduct: http://python.org/psf/codeofconduct/
Yeah the results were screenshots. Next time, i will just paste them as texts. Still ‘unknown’ for any unidentified type (including ‘Any’ if not imported) means it’s not identified as if you didn’t annotate the variable even though you meant to say ‘Any’ type is ok there. That was my intention. But thanks for pointing at the PEP. Sent from my iPhone
On 2 Dec 2020, at 4:31 PM, Joao S. O. Bueno <jsbueno@python.org.br> wrote:
I still like the approach of doing `import typing as t` - (or other short name of ones preference).
Bcause in the end, any argument used to bring "Any" as a built-in could be used in favor of any other element in typing anway.
Typing "t.Any, t.Union, t.Optional" is not a hassle and immediately readable, even by someone who just opened the file and skipped the "import" session. No need for tens of names from "typing.*" to be on the module namespace (or builtins).
On Wed, 2 Dec 2020 at 02:41, Christopher Barker <pythonchb@gmail.com> wrote: They pasted the code / results as screenshots.
But you got the gist.
In the future, it's really better to use plain text for email lists.
-CHB
On Tue, Dec 1, 2020 at 9:33 PM Steven D'Aprano <steve@pearwood.info> wrote: On Mon, Nov 30, 2020 at 05:19:13PM +0400, Abdulla Al Kathiri wrote:
In python 3.9, I get the following error:
In python 3.10, I get no runtime errors:
Did you forget to paste the text? Or did gmail eat it?
However, Pylance (I am not sure about mypy) didn’t recognize Any. Instead it made it “Unknown”.
That's probably a question for the Pylance devs, but my guess is that anything flagged as "Any" type is the same as flagging it as "I don't know what that type is".
In fact, I can put anything in 3.10 annotation and it runs just fine. I am not sure if this is the intention of Python3.10
Yes, Python 3.10 makes the `from __future__ import annotations` behaviour occur automatically. See PEP 563:
https://www.python.org/dev/peps/pep-0563/
(At least, I'm guessing that's what is happening here, it has to be a guess because I can't see the code you run and the error you get.)
-- Steve _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YMJ5NM... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD
Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UGRPHA... Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/CITQTZ... Code of Conduct: http://python.org/psf/codeofconduct/
Well, there are some exceptions. E.g. type aliases are still evaluated: # Python 3.10 TT = tuple[int, Any] Similarly, casts: f(cast(Any, arg)) And subclassing from generic classes, e.g. T = TypeVar("T") class B(Generic[T]): ... class C(B[Any]): ... Probably some that I forgot. On Sun, Nov 29, 2020 at 9:43 PM Inada Naoki <songofacandy@gmail.com> 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 <alkathiri.abdulla@gmail.com> 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 -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ELI474... Code of Conduct: http://python.org/psf/codeofconduct/
-- Inada Naoki <songofacandy@gmail.com> _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/VENGRL... Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
participants (8)
-
Abdulla Al Kathiri
-
Christopher Barker
-
Guido van Rossum
-
Inada Naoki
-
Joao S. O. Bueno
-
Paul Bryan
-
Steve Barnes
-
Steven D'Aprano