bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Python type annotations


From: Bruno Haible
Subject: Re: Python type annotations
Date: Thu, 29 Feb 2024 16:10:42 +0100

Hi Collin,

> Was gnulib-tool.py originally written in Python 2?

Yes, it was written in Python 2, with the knowledge that Python 3 was
already nearby around the corner.

> I don't feel like I
> don't see 'type(var) == list' anymore (in the very little Python code
> I read). Usually I feel like you would check if something is iterable
> like so [1] [2]:
> 
>      #!/usr/bin/env python3
> 
>      import collections.abc
> 
> 
>      def is_iterable(var):
>          return isinstance(var, collections.abc.Iterable)

Argument tests like we have them:

       if type(actioncmd) is not str:

are perfectly OK to use. Simple and straightforward.

is_iterable feels a bit like fashion: You can use them if you want to be
fashonable and have extra money to spend :)

> 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.

> Reading the union types was a bit confusing at first
> since I didn't know they were added [4].

Probably they would apply here:

       def cleaner(sequence):

could become

       def cleaner(sequence) -> str | list(str):

But since they are only supported starting with Python 3.10, and Python 3.10
is not on board of e.g. CentOS 8 or Ubuntu 20.04 [1], I would find it too early
to use this syntax now (except in comments, of course).

Bruno

[1] https://repology.org/project/python/versions






reply via email to

[Prev in Thread] Current Thread [Next in Thread]