[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Python type annotations
From: |
Collin Funk |
Subject: |
Re: Python type annotations |
Date: |
Thu, 29 Feb 2024 10:58:43 -0800 |
User-agent: |
Mozilla Thunderbird |
On 2/29/24 8:16 AM, Kévin Le Gouguec wrote:
> * the former provide some runtime sanity-checking,
> * the latter are ineffective at runtime, and exist mainly for the
> programmer's benefit (IDE hints for completion, documentation,
> linting, etc).
>
> (Though linters like mypy do _also_ pick up on the former)
I think that I was thinking of mypy when I originally mentioned it.
All of the different linters sort of mix together in my head though.
Thanks. :)
> '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? 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
[1] https://docs.python.org/3/library/typing.html#typing.Union
Thanks,
Collin