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

Add missing shape parameter to numpy methods #1450

Merged
merged 9 commits into from
Mar 9, 2022
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Release date: TBA

Closes #1410

* Fix false positive for ``shape`` parameter of ``numpy.zeros_like`` method.

Closes PyCQA/pylint#5871

* Set ``end_lineno`` and ``end_col_offset`` attributes to ``None`` for all nodes
with PyPy 3.8. PyPy 3.8 assigns these attributes inconsistently which could lead
to unexpected errors. Overwriting them with ``None`` will cause a fallback
Expand Down
6 changes: 3 additions & 3 deletions astroid/brain/brain_numpy_core_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
"""
)

Expand Down
26 changes: 25 additions & 1 deletion tests/unittest_brain_numpy_core_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
# Copyright (c) 2021 Pierre Sassoulas <[email protected]>
# Copyright (c) 2021 Daniël van Noord <[email protected]>
# Copyright (c) 2021 Marc Mueller <[email protected]>
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved

# 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
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved

try:
import numpy # pylint: disable=unused-import
Expand Down Expand Up @@ -62,5 +65,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 = list(instance.infer())[0].args.args
assert [arg.name for arg in actual_args] == expected_args


if __name__ == "__main__":
unittest.main()