brain_numpy_utils.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
  4. """Different utilities for the numpy brains."""
  5. from __future__ import annotations
  6. from astroid import nodes
  7. from astroid.builder import extract_node
  8. from astroid.context import InferenceContext
  9. # Class subscript is available in numpy starting with version 1.20.0
  10. NUMPY_VERSION_TYPE_HINTS_SUPPORT = ("1", "20", "0")
  11. def numpy_supports_type_hints() -> bool:
  12. """Returns True if numpy supports type hints."""
  13. np_ver = _get_numpy_version()
  14. return np_ver and np_ver > NUMPY_VERSION_TYPE_HINTS_SUPPORT
  15. def _get_numpy_version() -> tuple[str, str, str]:
  16. """
  17. Return the numpy version number if numpy can be imported.
  18. Otherwise returns ('0', '0', '0')
  19. """
  20. try:
  21. import numpy # pylint: disable=import-outside-toplevel
  22. return tuple(numpy.version.version.split("."))
  23. except (ImportError, AttributeError):
  24. return ("0", "0", "0")
  25. def infer_numpy_name(
  26. sources: dict[str, str], node: nodes.Name, context: InferenceContext | None = None
  27. ):
  28. extracted_node = extract_node(sources[node.name])
  29. return extracted_node.infer(context=context)
  30. def infer_numpy_attribute(
  31. sources: dict[str, str],
  32. node: nodes.Attribute,
  33. context: InferenceContext | None = None,
  34. ):
  35. extracted_node = extract_node(sources[node.attrname])
  36. return extracted_node.infer(context=context)
  37. def _is_a_numpy_module(node: nodes.Name) -> bool:
  38. """
  39. Returns True if the node is a representation of a numpy module.
  40. For example in :
  41. import numpy as np
  42. x = np.linspace(1, 2)
  43. The node <Name.np> is a representation of the numpy module.
  44. :param node: node to test
  45. :return: True if the node is a representation of the numpy module.
  46. """
  47. module_nickname = node.name
  48. potential_import_target = [
  49. x for x in node.lookup(module_nickname)[1] if isinstance(x, nodes.Import)
  50. ]
  51. return any(
  52. ("numpy", module_nickname) in target.names or ("numpy", None) in target.names
  53. for target in potential_import_target
  54. )
  55. def member_name_looks_like_numpy_member(
  56. member_names: frozenset[str], node: nodes.Name
  57. ) -> bool:
  58. """
  59. Returns True if the Name node's name matches a member name from numpy
  60. """
  61. return node.name in member_names and node.root().name.startswith("numpy")
  62. def attribute_name_looks_like_numpy_member(
  63. member_names: frozenset[str], node: nodes.Attribute
  64. ) -> bool:
  65. """
  66. Returns True if the Attribute node's name matches a member name from numpy
  67. """
  68. return (
  69. node.attrname in member_names
  70. and isinstance(node.expr, nodes.Name)
  71. and _is_a_numpy_module(node.expr)
  72. )