-
-
Notifications
You must be signed in to change notification settings - Fork 290
Fix crash on inference of __len__
#1234
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
Conversation
I'm marking this as a draft because I'm not 100% sure this is the right way forward, but I would like some input for any of the experienced I also have not thought of a good way to test this in |
@DanielNoord @Pierre-Sassoulas i had a look to this PR. Honestly i do not understand why this line was added. |
We're also putting primer tests in place in pylint-dev/pylint#5173 so maybe we'll get to be more confident in small changes like this in the near future :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some time to review this PR. A few notes.
The _proxied
comparison is necessary. Otherwise this would also match.
class A:
def __len__(self) -> int:
return 42
class Crash:
def __len__(self) -> int:
a = A()
return len(a)
Furthermore, I was able to reproduce the issue without numpy
.
class MyClass:
def some_func(self):
return lambda: 42
def __len__(self):
return len(self.some_func())
Those two tests could probably be added to unittest_brain.py
, similar to the existing one.
https://github.com/PyCQA/astroid/blob/b62f243b16b7f435c8be869577959e95a7927a91/tests/unittest_brain.py#L3112-L3126
--
As for numpy
, the whole issue was only triggered because self.array
was inferred as numpy FunctionDef
. Not sure this is correct in this instance or if the numpy
brain needs to be updated. Maybe @hippo91 know more about that? Anyway, if it needs to be updated, this should happen in another PR.
https://github.com/PyCQA/astroid/blob/b62f243b16b7f435c8be869577959e95a7927a91/astroid/brain/brain_numpy_core_multiarray.py#L41-L42
https://github.com/PyCQA/astroid/blob/b62f243b16b7f435c8be869577959e95a7927a91/astroid/brain/brain_numpy_utils.py#L90
Fixed the code with @cdce8p's suggestions and added test cases. The last test will fail without the changes in this PR, the first test is to make sure the inference on these kind of functions actually works. As for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some last comments. I know that the test names are already descriptive, but adding one more line in the docstring might be useful.
Added docstrings, also for the existing test |
As for the numpy issue, I don't think it's worth opening an issue for it as long as we don't encounter it in practice. -- class MyClass:
def __init__(self, array):
self.array = array
self.array #@
|
Steps
Description
This is a weird one.
I found this while working on the primer tests for
pylint
where running on the codebase ofnumpy
created a crash. I issued pylint-dev/pylint#5244 for this. Initially I thought it had something to do with the brain fornumpy
as the crash only occurred when the directory name starts withnumpy
.However, removing this single line seems to fix the issue. Furthermore, (locally) all tests for
astroid
andpylint
pass with this change. I have looked at the commit that added this line (25384d4) but did not immediately understand why this was added. In any case, no tests seems to have been added or this line is redundant and the removing it is the way forward.Type of Changes
Related Issue
Would close pylint-dev/pylint#5244