Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from Feb 8, 2021

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Feb 4, 2021

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 Static typing labels Feb 4, 2021
complex160 as complex160,
complex192 as complex192,
complex256 as complex256,
complex512 as complex512,
Copy link
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
Copy link
Member Author

BvB93 commented Feb 4, 2021

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

@mattip
Copy link
Member

mattip commented Feb 4, 2021

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
Copy link
Member

charris commented Feb 8, 2021

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.

None yet

3 participants