Skip to content

TYP: Use Final and LiteralString for the constants in numpy.version#26975

Merged
seberg merged 1 commit intonumpy:mainfrom
jorenham:typing/final-literal-version
Jul 18, 2024
Merged

TYP: Use Final and LiteralString for the constants in numpy.version#26975
seberg merged 1 commit intonumpy:mainfrom
jorenham:typing/final-literal-version

Conversation

@jorenham
Copy link
Copy Markdown
Member

@jorenham jorenham commented Jul 18, 2024

The numpy.version members are constants, and should be treated as such (overwriting them is possible, but a bad idea).

The use of typing.Final helps linters and type-checkers to recognize them as constants, even though their names aren't uppercase. These changes are backwards-compatible, unless the values are overwritten (but I can't imagine a usecase for this).

For python>=3.11 the str annotations have been replaced with typing.LiteralString, which is fully backwards-compatible with str. It will help typecheckers to identify the version strings as pre-defined constant values.

In #26871 similar changes are made in the numpy namespace.

@jorenham jorenham changed the title TYP: Final constants in numpy.version and use LiteralString TYP: Use Final and LiteralString for the constants in numpy.version Jul 18, 2024
Copy link
Copy Markdown
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right to me, thanks.

@seberg seberg merged commit 451fd48 into numpy:main Jul 18, 2024
'release',
'short_version',
'version',
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, having __all__ is important for types in some way?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it should make a functional difference.
I mostly added it because "explicit is better than implicit", and because makes it easier for typecheckers to determine the public module members, which can help with performance.

@jorenham jorenham deleted the typing/final-literal-version branch July 18, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants