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

Crash on nodes.FunctionDef._proxied in numpy tests #5244

Closed
DanielNoord opened this issue Nov 2, 2021 · 1 comment · Fixed by pylint-dev/astroid#1234 or #5269
Closed

Crash on nodes.FunctionDef._proxied in numpy tests #5244

DanielNoord opened this issue Nov 2, 2021 · 1 comment · Fixed by pylint-dev/astroid#1234 or #5269
Labels
Astroid Related to astroid Crash 💥 A bug that makes pylint crash Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Needs astroid update Needs an astroid update (probably a release too) before being mergable
Milestone

Comments

@DanielNoord
Copy link
Collaborator

DanielNoord commented Nov 2, 2021

Bug description

pylint crashes on the following code:

import pytest
import warnings
import numpy as np


@pytest.mark.filterwarnings("error")
def test_getattr_warning():
    # issue gh-14735: make sure we clear only getattr errors, and let warnings
    # through
    class Wrapper:
        def __init__(self, array):
            self.array = array

        def __len__(self):
            return len(self.array)

        def __getitem__(self, item):
            return type(self)(self.array[item])

        def __getattr__(self, name):
            if name.startswith("__array_"):
                warnings.warn("object got converted", UserWarning, stacklevel=1)

            return getattr(self.array, name)

        def __repr__(self):
            return "<Wrapper({self.array})>".format(self=self)

    array = Wrapper(np.arange(10))
    with pytest.raises(UserWarning, match="object got converted"):
        np.asarray(array)


def test_array_called():
    class Wrapper:
        val = '0' * 100
        def __array__(self, result=None):
            return np.array([self.val], dtype=object)


    wrapped = Wrapper()
    arr = np.array(wrapped, dtype=str)
    assert arr.dtype == 'U100'
    assert arr[0] == Wrapper.val

Taken from:
https://github.com/numpy/numpy/blob/main/numpy/core/tests/test_protocols.py

Configuration

Configuration of `main`.

Command used

git clone https://github.com/numpy/numpy.git
pylint pylint numpy/core/tests/test_protocols.py

Or see GitHub Actions output here:
https://github.com/DanielNoord/pylint/runs/4082260843?check_suite_focus=true

Pylint output

************* Module numpy.core.tests.test_protocols
numpy/core/tests/test_protocols.py:1:0: C0114: Missing module docstring (missing-module-docstring)
numpy/core/tests/test_protocols.py:7:0: C0116: Missing function or method docstring (missing-function-docstring)
numpy/core/tests/test_protocols.py:10:4: C0115: Missing class docstring (missing-class-docstring)
Exception on node <FunctionDef.__len__ l.14 at 0x109d8a160> in file '/Users/daniel/DocumentenLaptop/Programming/Github/pylint/.pylint_primer_tests/numpy/numpy/numpy/core/tests/test_protocols.py'
Traceback (most recent call last):
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/inference_tip.py", line 28, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function register_builtin_transform.<locals>._transform_wrapper at 0x109819e50>, <Call l.15 at 0x109d871c0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/checkers/classes.py", line 2174, in visit_functiondef
    inferred = _safe_infer_call_result(node, node)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/checkers/classes.py", line 438, in _safe_infer_call_result
    value = next(inferit)
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/nodes/scoped_nodes.py", line 1804, in infer_call_result
    yield from returnnode.value.infer(context)
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 113, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/inference_tip.py", line 30, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/brain/brain_builtin_inference.py", line 187, in _transform_wrapper
    result = transform(node, context=context)
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/brain/brain_builtin_inference.py", line 783, in infer_len
    return nodes.Const(helpers.object_len(argument_node, context=context))
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/helpers.py", line 262, in object_len
    and inferred_node._proxied == node_frame.parent
AttributeError: 'FunctionDef' object has no attribute '_proxied'
numpy/core/tests/test_protocols.py:1:0: F0001: Fatal error while checking 'numpy/core/tests/test_protocols.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/Users/daniel/Library/Caches/pylint/pylint-crash-2021-11-02-18.txt'. (fatal)

-------------------------------------------------------------------
Your code has been rated at 6.25/10 (previous run: 10.00/10, -3.75)

Expected behavior

No error? I think?

This might also be due to this being code for tests which shouldn't be linted at all. I found this while working on the primer tests.

Pylint version

pylint 2.11.2-dev0
astroid 2.8.4
Python 3.9.6 (default, Aug  2 2021, 08:23:35) 
[Clang 12.0.5 (clang-1205.0.22.11)]

OS / Environment

No response

Additional dependencies

No response

@DanielNoord DanielNoord added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 2, 2021
@DanielNoord
Copy link
Collaborator Author

A minimal reproducible example is:

A directory that starts with numpy. Can be numpy2, numpy_2 or (seemingly) anything else with numpy at the start.

An empty numpy/__init__.py.

numpy/test.py with:

class MyClass:
    def __init__(self, array):
        self.array = array

    def __len__(self):
        return len(self.array)

Command:
pylint numpy/test.py

Crash:
pylint crashed with a AttributeError and with the following stacktrace:

Traceback (most recent call last):
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/inference_tip.py", line 28, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function register_builtin_transform.<locals>._transform_wrapper at 0x10d3b2e50>, <Call l.6 at 0x10d9434f0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/lint/pylinter.py", line 1032, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/lint/pylinter.py", line 1067, in _check_file
    check_astroid_module(ast_node)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/lint/pylinter.py", line 1203, in check_astroid_module
    retval = self._check_astroid_module(
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/lint/pylinter.py", line 1250, in _check_astroid_module
    walker.walk(node)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/checkers/classes.py", line 2174, in visit_functiondef
    inferred = _safe_infer_call_result(node, node)
  File "/Users/daniel/DocumentenLaptop/Programming/Github/pylint/pylint/checkers/classes.py", line 438, in _safe_infer_call_result
    value = next(inferit)
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/nodes/scoped_nodes.py", line 1804, in infer_call_result
    yield from returnnode.value.infer(context)
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 113, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/inference_tip.py", line 30, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/brain/brain_builtin_inference.py", line 187, in _transform_wrapper
    result = transform(node, context=context)
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/brain/brain_builtin_inference.py", line 783, in infer_len
    return nodes.Const(helpers.object_len(argument_node, context=context))
  File "/Users/daniel/.pyenv/versions/3.9.6/envs/pylint-3.9.6/lib/python3.9/site-packages/astroid/helpers.py", line 262, in object_len
    and inferred_node._proxied == node_frame.parent
AttributeError: 'FunctionDef' object has no attribute '_proxied'

I guess this has something to do with astroid's brain for numpy but I have no experience with brains so I don't know where to look to start fixing this.

@DanielNoord DanielNoord added Astroid Related to astroid Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Crash 💥 A bug that makes pylint crash Needs astroid update Needs an astroid update (probably a release too) before being mergable and removed Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 2, 2021
@DanielNoord DanielNoord added this to the 2.12.0 milestone Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astroid Related to astroid Crash 💥 A bug that makes pylint crash Needs astroid Brain 🧠 Needs a brain tip in astroid (then an astroid upgrade) Needs astroid update Needs an astroid update (probably a release too) before being mergable
Projects
None yet
1 participant