From 6d81868433923ec11a4d62c8ad26cbe05e8f2880 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Wed, 9 Mar 2022 22:40:50 +0100 Subject: [PATCH] Add missing shape parameter to numpy methods (#1450) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes PyCQA/pylint#5871 Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> --- ChangeLog | 5 +++++ astroid/brain/brain_numpy_core_numeric.py | 6 +++--- requirements_test_brain.txt | 2 +- tests/unittest_brain_numpy_core_numeric.py | 25 ++++++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3d06d05215..04cb0f2308 100644 --- a/ChangeLog +++ b/ChangeLog @@ -27,6 +27,11 @@ Release date: TBA to unexpected errors. Overwriting them with ``None`` will cause a fallback to the already supported way of PyPy 3.7. +* Add missing ``shape`` parameter to numpy ``zeros_like``, ``ones_like``, + and ``full_like`` methods. + + Closes PyCQA/pylint#5871 + What's New in astroid 2.10.1? ============================= diff --git a/astroid/brain/brain_numpy_core_numeric.py b/astroid/brain/brain_numpy_core_numeric.py index 56c7ede925..24277206ae 100644 --- a/astroid/brain/brain_numpy_core_numeric.py +++ b/astroid/brain/brain_numpy_core_numeric.py @@ -24,9 +24,9 @@ def numpy_core_numeric_transform(): """ # different functions defined in numeric.py import numpy - def zeros_like(a, dtype=None, order='K', subok=True): return numpy.ndarray((0, 0)) - def ones_like(a, dtype=None, order='K', subok=True): return numpy.ndarray((0, 0)) - def full_like(a, fill_value, dtype=None, order='K', subok=True): return numpy.ndarray((0, 0)) + def zeros_like(a, dtype=None, order='K', subok=True, shape=None): return numpy.ndarray((0, 0)) + def ones_like(a, dtype=None, order='K', subok=True, shape=None): return numpy.ndarray((0, 0)) + def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None): return numpy.ndarray((0, 0)) """ ) diff --git a/requirements_test_brain.txt b/requirements_test_brain.txt index 1367d4eb13..a45e3bf2d0 100644 --- a/requirements_test_brain.txt +++ b/requirements_test_brain.txt @@ -1,7 +1,7 @@ attrs types-attrs nose -numpy +numpy>=1.17.0 python-dateutil types-python-dateutil six diff --git a/tests/unittest_brain_numpy_core_numeric.py b/tests/unittest_brain_numpy_core_numeric.py index 343de671f6..15fe901dc3 100644 --- a/tests/unittest_brain_numpy_core_numeric.py +++ b/tests/unittest_brain_numpy_core_numeric.py @@ -7,7 +7,11 @@ # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE + import unittest +from typing import List + +import pytest try: import numpy # pylint: disable=unused-import @@ -62,5 +66,26 @@ def test_numpy_function_calls_inferred_as_ndarray(self): ) +@pytest.mark.skipif(not HAS_NUMPY, reason="This test requires the numpy library.") +@pytest.mark.parametrize( + "method, expected_args", + [ + ("zeros_like", ["a", "dtype", "order", "subok", "shape"]), + ("full_like", ["a", "fill_value", "dtype", "order", "subok", "shape"]), + ("ones_like", ["a", "dtype", "order", "subok", "shape"]), + ("ones", ["shape", "dtype", "order"]), + ], +) +def test_function_parameters(method: str, expected_args: List[str]) -> None: + instance = builder.extract_node( + f""" + import numpy + numpy.{method} #@ + """ + ) + actual_args = instance.inferred()[0].args.args + assert [arg.name for arg in actual_args] == expected_args + + if __name__ == "__main__": unittest.main()