Collin Funk <collin.fu...@gmail.com> writes: >> 'from __future__ import annotations', available since Python 3.7 [1], >> allows older Pythons to ignore type-hint syntax sophistications brought >> by newer Pythons. >> >> So if Python 3.7 is acceptable as a minimum version (it seems available >> in the distro releases you mention), you would be able to start using >> this union syntax. > > I remember having to use the __future__ import. I guess I no longer > have to now that Debian Stable has Python 3.11. > > Do you happen to know if 'from __future__ import annotations' allows > for the use of 'TypeX | TypeY' union syntax with Python 3.7?
Yes, 'TypeX | TypeY' was what I was referring to above with "syntax sophistications brought by newer Pythons". Demonstration: Python 3.7.17+ (tags/3.7:0f56adb8d7, Mar 1 2024, 08:50:56) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def foo(s: str | list[str]) -> str | list[str]: ... return s ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'type' object is not subscriptable >>> from __future__ import annotations >>> def foo(s: str | list[str]) -> str | list[str]: ... return s ... >>> foo("hello") 'hello' So Pythons ≥3.7 can import 'annotations' from '__future__', and that lets them gloss over newer annotation constructs they do not understand… unless we actively tries to evaluate them: >>> from typing import get_type_hints >>> get_type_hints(foo) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/legouguec/src/python/3.7/Lib/typing.py", line 1013, in get_type_hints value = _eval_type(value, globalns, localns) File "/home/legouguec/src/python/3.7/Lib/typing.py", line 263, in _eval_type return t._evaluate(globalns, localns) File "/home/legouguec/src/python/3.7/Lib/typing.py", line 467, in _evaluate eval(self.__forward_code__, globalns, localns), File "<string>", line 1, in <module> TypeError: 'type' object is not subscriptable > I was > under the impression that it was added for 3.10 [1]. I think that for > compatibility with older versions you could write: > > def func(var) -> Union[int, str]: > > instead of: > > def func(var) -> int | str Right, that's another route you can go if (a) the linters you expect to use run Pythons <3.10, so they are subject to the above caveat, and/or (b) you expect to call typing.get_type_hints in your own scripts (e.g. GDB's DAP server uses that to sanity-check client requests) and/or (c) you (collective, as a project) find it more legible. > [1] https://docs.python.org/3/library/typing.html#typing.Union > > Thanks, > Collin