diff options
author | hippo91 <guillaume.peillex@gmail.com> | 2021-10-05 13:39:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-05 13:39:29 +0200 |
commit | 1419ac51a89d55b441af55add6364d466db1a691 (patch) | |
tree | 1718ba8a446b492f5ea4995ad6c75efa633d62eb /astroid | |
parent | f9c1b310510dfb723a0d78e2b4f97b1310f13cd6 (diff) | |
download | astroid-git-1419ac51a89d55b441af55add6364d466db1a691.tar.gz |
Bug pylint 4326 (#1183)
* Adds unittest dealing with class subscript
* Adds support of type hints inside numpy's brains
* Adds unit test to check astroid does not crash if numpy is not available
Diffstat (limited to 'astroid')
-rw-r--r-- | astroid/brain/brain_numpy_core_numerictypes.py | 17 | ||||
-rw-r--r-- | astroid/brain/brain_numpy_ndarray.py | 7 | ||||
-rw-r--r-- | astroid/brain/brain_numpy_utils.py | 26 |
3 files changed, 45 insertions, 5 deletions
diff --git a/astroid/brain/brain_numpy_core_numerictypes.py b/astroid/brain/brain_numpy_core_numerictypes.py index 69c4686c..d52b1ed3 100644 --- a/astroid/brain/brain_numpy_core_numerictypes.py +++ b/astroid/brain/brain_numpy_core_numerictypes.py @@ -9,6 +9,7 @@ # TODO(hippo91) : correct the methods signature. """Astroid hooks for numpy.core.numerictypes module.""" +from astroid.brain.brain_numpy_utils import numpy_supports_type_hints from astroid.brain.helpers import register_module_extender from astroid.builder import parse from astroid.manager import AstroidManager @@ -19,9 +20,7 @@ def numpy_core_numerictypes_transform(): # According to numpy doc the generic object should expose # the same API than ndarray. This has been done here partially # through the astype method. - return parse( - """ - # different types defined in numerictypes.py + generic_src = """ class generic(object): def __init__(self, value): self.T = np.ndarray([0, 0]) @@ -106,8 +105,16 @@ def numpy_core_numerictypes_transform(): def transpose(self): return uninferable def var(self): return uninferable def view(self): return uninferable - - + """ + if numpy_supports_type_hints(): + generic_src += """ + @classmethod + def __class_getitem__(cls, value): + return cls + """ + return parse( + generic_src + + """ class dtype(object): def __init__(self, obj, align=False, copy=False): self.alignment = None diff --git a/astroid/brain/brain_numpy_ndarray.py b/astroid/brain/brain_numpy_ndarray.py index ea4fb809..bf588aa5 100644 --- a/astroid/brain/brain_numpy_ndarray.py +++ b/astroid/brain/brain_numpy_ndarray.py @@ -9,6 +9,7 @@ """Astroid hooks for numpy ndarray class.""" +from astroid.brain.brain_numpy_utils import numpy_supports_type_hints from astroid.builder import extract_node from astroid.inference_tip import inference_tip from astroid.manager import AstroidManager @@ -143,6 +144,12 @@ def infer_numpy_ndarray(node, context=None): def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False): return np.ndarray([0, 0]) def view(self, dtype=None, type=None): return np.ndarray([0, 0]) """ + if numpy_supports_type_hints(): + ndarray += """ + @classmethod + def __class_getitem__(cls, value): + return cls + """ node = extract_node(ndarray) return node.infer(context=context) diff --git a/astroid/brain/brain_numpy_utils.py b/astroid/brain/brain_numpy_utils.py index 64a720b3..97c88e3f 100644 --- a/astroid/brain/brain_numpy_utils.py +++ b/astroid/brain/brain_numpy_utils.py @@ -8,9 +8,35 @@ """Different utilities for the numpy brains""" +from typing import Tuple + from astroid.builder import extract_node from astroid.nodes.node_classes import Attribute, Import, Name, NodeNG +# Class subscript is available in numpy starting with version 1.20.0 +NUMPY_VERSION_TYPE_HINTS_SUPPORT = ("1", "20", "0") + + +def numpy_supports_type_hints() -> bool: + """ + Returns True if numpy supports type hints + """ + np_ver = _get_numpy_version() + return np_ver and np_ver > NUMPY_VERSION_TYPE_HINTS_SUPPORT + + +def _get_numpy_version() -> Tuple[str, str, str]: + """ + Return the numpy version number if numpy can be imported. Otherwise returns + ('0', '0', '0') + """ + try: + import numpy # pylint: disable=import-outside-toplevel + + return tuple(numpy.version.version.split(".")) + except ImportError: + return ("0", "0", "0") + def infer_numpy_member(src, node, context=None): node = extract_node(src) |