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

endless recursion checking the bool_value of a condition with value bool() #878

Open
ltcmelo opened this issue Jan 4, 2021 · 3 comments

Comments

@ltcmelo
Copy link
Contributor

ltcmelo commented Jan 4, 2021

I'm facing an endless recursion (apparently, from the trace, not the same as this currently open issue). Consider this snippet:

v = bool() or 1

When I infer the value of v, the execution gets to a point where it bounces back and forth between def _infer_method_result_truth and def bool_value. Eventually, the maximum recursion limit is reached.

I believe that the root cause of the problem is here: due to bool(), we have the inferred object as being the "same" as the instance object (an instance of class bool). I suppose that a check* must be added there, for this equality.

*I could create a PR, but it seems that Instance has no __eq__ method; I could still check with if (str(inferred) == str(instance): return util.Uninferable but I'm not sure it'd be corred/desired.

NOTE: The same issue happens in slight variations, e.g., v = w if hasattr(o, 'x') else 1

@ltcmelo ltcmelo changed the title endless recursion checking the bool_value of a condition which value bool() endless recursion checking the bool_value of a condition with value bool() Jan 4, 2021
@hippo91
Copy link
Contributor

hippo91 commented Jan 16, 2021

@ltcmelo thanks for your report.
I'm am currently unable to reproduce it.
Can you give a snippet of the problematic code and the version of python and astroid you are using?

@ltcmelo
Copy link
Contributor Author

ltcmelo commented Jan 18, 2021

Hi @hippo91, it seems that:

  • not always the recursion is infinite, there appears to exists a co-relation with the files/project being processed; I couldn't spot what is breaking the recursion, though — I suspect that, in certain cases, the context object passed to infer will aggregate enough info, but those cases don't seem to be guaranteed.
  • even with a limited recursion, it's still possible to observe (what I believe is) the bogus behavior I mentioned.

A list of steps that you could try in order to validate my suspicion. Perhaps, you could try reducing your sys.setrecursionlimit too…

  1. Right after the statement I pointed to earlier, add the following:
print(f'\n[[inferred:{inferred}]] [[value:{value}]] [[context:{context}]]')
  1. Create a regular AST visitor, load a module with that snippet I originally suggested, and, upon visit_name, call node.infer on object corresponding to v.

I expect that you see tens of calls jumping back and forth between those 2 methods, repeatedly trying to infer the same bool instance. This is how my prints look like.

Screen Shot 2021-01-18 at 10 46 12

Python version: Python 3.8.6 (CPython on darwin)
astroid: 2.4.2

@hippo91
Copy link
Contributor

hippo91 commented Jan 23, 2021

@ltcmelo thanks for your update. Sadly without a clean snippet that reproduce the problem, i'm afraid there is not so much we can do here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants