Skip to content

ENH: Add a mypy plugin for exposing platform-specific extended-precision types - #18322

Merged
charris merged 6 commits into
numpy:masterfrom
BvB93:plugin-precision
Feb 8, 2021
Merged

ENH: Add a mypy plugin for exposing platform-specific extended-precision types#18322
charris merged 6 commits into
numpy:masterfrom
BvB93:plugin-precision

Conversation

@BvB93

@BvB93 BvB93 commented Feb 4, 2021

Copy link
Copy Markdown
Member

This PR modifies numpy's mypy plugin such that it manages all platform-specific extended-precision types (e.g. float128). More specifically, it removes all extended-precision types unavailable to the platform in question, rather than adding them.

The advantage of their removal (rather than addition) is that aforementioned types will still be available for use even without enabling the plugin. On the flip side, this does mean that, without the plugin, every single extended-precision type is "available" on every single platform.

The plugin can be enabled via one's mypy.ini file:

[mypy]
plugins = numpy.typing.mypy_plugin

Examples

With the plugin enabled.

from typing import TYPE_CHECKING
import numpy as np

if TYPE_CHECKING:
    # Depending on the platform:
    # note: Revealed type is 'numpy.floating[numpy.typing._128Bit*]'
    # or 
    # error: Module has no attribute "float128"; maybe "float64", "float32", or "float16"?
    reveal_type(np.float128())

@BvB93 BvB93 added 01 - Enhancement 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes 41 - Static typing labels Feb 4, 2021
Comment thread numpy/__init__.pyi Outdated

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.

Effectively the plugin removes all import statements that are invalid for the platform in question.

The full list of (potential) extended-precision types was taken from numpy/__init__.pxd.

@BvB93 BvB93 removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Feb 4, 2021
@BvB93

BvB93 commented Feb 4, 2021

Copy link
Copy Markdown
Member Author

Anyone knows what's going with the PyPy tests?
The failure seems to be unrelated, but it's failing nevertheless.

@mattip

mattip commented Feb 4, 2021

Copy link
Copy Markdown
Member

Anyone knows what's going with the PyPy tests?

It uses a nightly build since there is not an official release for win64 yet. Let's see if this repeats.

@charris
charris merged commit d7d9d7b into numpy:master Feb 8, 2021
@charris

charris commented Feb 8, 2021

Copy link
Copy Markdown
Member

Thanks Bas.

@BvB93
BvB93 deleted the plugin-precision branch February 8, 2021 17:06
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.

3 participants