[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gnulib-tool.py: Update type hints and docstring.
From: |
Bruno Haible |
Subject: |
Re: gnulib-tool.py: Update type hints and docstring. |
Date: |
Sun, 21 Apr 2024 23:43:12 +0200 |
Hi Collin,
> > Also, what about the type of filetable? It is described as
> >
> > dict[str, list[str]]
> >
> > but I think it is more
> >
> > dict[str, list[str | tuple[str, str]]]
> >
> > right?
> ...
> Using union types like that is annoying with type checkers though. For
> example, using that type hint and this line of code in
> GLImport.execute():
>
> if [ file
> for file in filetable['all']
> if file.startswith('doc/') ]:
>
> I see this warning under 'startswith' from Pyright:
>
> /home/collin/.local/src/gnulib/pygnulib/GLImport.py:1020:22 - error: Cannot
> access attribute "startswith" for class "tuple[str, str]"
> Attribute "startswith" is unknown (reportAttributeAccessIssue)
>
> or with mypy:
>
> GLImport.py:1020: error: Item "tuple[str, ...]" of "str | tuple[str, str]"
> has no attribute "startswith" [union-attr]
Hmm. It's valid code that will work at runtime. I would understand a
"warning", if the tool cannot prove that the code is safe. But "error"?
That's too severe. It's again time for a bug report or two, I would say.
> It might just be more beneficial to use 'Any' for complex unions like
> that. You could ignore warnings like:
>
> if [ file
> for file in filetable['all']
> if file.startswith('doc/') ]: # type: ignore
>
> but that feels annoying for variables used frequently like that filetable.
Nah. I think the better cure of the problem is to abandon the 'dict' type
here. That is, create a proper Python class GLFileTable with five attributes.
In this situation, the five attributes are not constrained to have the same
type.
Bruno