[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Python type annotations
From: |
dima . pasechnik |
Subject: |
Re: Python type annotations |
Date: |
Thu, 29 Feb 2024 19:31:09 +0000 |
On Thu, Feb 29, 2024 at 05:16:29PM +0100, Kévin Le Gouguec wrote:
> (Hi! Apologies for the drive-by comments, hopefully they won't be
> complete noise. Butting in hoping to clarify things at best, stand
> corrected at worst)
>
> Bruno Haible <bruno@clisp.org> writes:
>
> >> Python has added a lot of interesting type hinting stuff over the past
> >> few years [3].
> >
> > Interesting. Feel free to use this syntax where you find it appropriate.
> >
> > I feel it would make sense to use for function returns. For function
> > arguments
> > we already have type tests in the essential places.
>
> I would consider type tests (assuming you mean something along the lines
> of 'type(arg) is Sometype' or 'isinstance(arg, Sometype)') and type
> annotations somewhat orthogonal:
>
> * 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).
They are really different; "isinstance(arg, Sometype)" is true even if arg
is a subclass of Sometime, "whereas type(arg) is Sometype". E.g.
>>> from numbers import Number
>>> isinstance(1, Number)
True
>>> isinstance(1.0, Number)
True
>>> type(1)
<class 'int'>
>>> type(1.0)
<class 'float'>
Just in case,
Dima