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 'AsStringVisitor' object has no attribute 'visit_unknown' #5048

Closed
WolfgangFellger opened this issue Sep 20, 2021 · 5 comments · Fixed by #5057
Closed

Crash 'AsStringVisitor' object has no attribute 'visit_unknown' #5048

WolfgangFellger opened this issue Sep 20, 2021 · 5 comments · Fixed by #5057
Assignees
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash Needs astroid update Needs an astroid update (probably a release too) before being mergable
Milestone

Comments

@WolfgangFellger
Copy link

WolfgangFellger commented Sep 20, 2021

Bug description

pylint crashed with a AttributeError when parsing a file, see stacktrace below.

Edit: Forgot to mention this is a regression. File worked in pylint 2.10.2/astroid 2.7.3.

The example below is as condensed as possible (to the point of not making sense, I know):

def _find_prefix(a, b):
    common_prefix = []
    for i, j in zip(a, b):
        if i != j:
            break
        common_prefix.append(i)
    return tuple(common_prefix)


def _check_role_references(path, nearest_provider, provider_paths):
    checked_parent_providers = set()
    prefix = _find_prefix(path, nearest_provider)
    parent = prefix + (nearest_provider[len(prefix)],)
    if parent in checked_parent_providers:
        raise Exception()

Configuration

No response

Command used

pylint faulty.py

Pylint output

************* Module faulty
faulty.py:1:0: C0114: Missing module docstring (missing-module-docstring)
faulty.py:1:0: C0103: Argument name "a" doesn't conform to snake_case naming style (invalid-name)
faulty.py:1:0: C0103: Argument name "b" doesn't conform to snake_case naming style (invalid-name)
Exception on node <If l.14 at 0x7f419c74e9d0> in file '/opt/code/quidditch/faulty.py'
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    callback(astroid)
  File "/usr/local/lib/python3.9/site-packages/pylint/checkers/base.py", line 1104, in visit_if
    self._check_using_constant_test(node, node.test)
  File "/usr/local/lib/python3.9/site-packages/pylint/checkers/base.py", line 1142, in _check_using_constant_test
    inferred = utils.safe_infer(test)
  File "/usr/local/lib/python3.9/site-packages/pylint/checkers/utils.py", line 1181, in safe_infer
    value = next(infer_gen)
  File "/usr/local/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 105, in infer
    yield from self._infer(context, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/astroid/inference.py", line 872, in _infer_compare
    retval = _do_compare(lhs, op, rhs)
  File "/usr/local/lib/python3.9/site-packages/astroid/inference.py", line 842, in _do_compare
    left, right = _to_literal(left), _to_literal(right)
  File "/usr/local/lib/python3.9/site-packages/astroid/inference.py", line 814, in _to_literal
    return ast.literal_eval(node.as_string())
  File "/usr/local/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 547, in as_string
    return AsStringVisitor()(self)
  File "/usr/local/lib/python3.9/site-packages/astroid/nodes/as_string.py", line 55, in __call__
    return node.accept(self).replace(DOC_NEWLINE, "\n")
  File "/usr/local/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 186, in accept
    return func(self)
  File "/usr/local/lib/python3.9/site-packages/astroid/nodes/as_string.py", line 516, in visit_tuple
    return "(%s, )" % node.elts[0].accept(self)
  File "/usr/local/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 185, in accept
    func = getattr(visitor, "visit_" + self.__class__.__name__.lower())
AttributeError: 'AsStringVisitor' object has no attribute 'visit_unknown'
faulty.py:1:0: F0001: Fatal error while checking 'faulty.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/home/uid1000/.cache/pylint/pylint-crash-2021-09-20-08.txt'. (fatal)

Expected behavior

Not quite sure, but not crashing

Pylint version

pylint 2.11.1
astroid 2.8.0
Python 3.9.7 (default, Sep  3 2021, 02:02:37) 
[GCC 10.2.1 20210110]

OS / Environment

No response

Additional dependencies

No response

@WolfgangFellger WolfgangFellger added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 20, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 20, 2021
@DanielNoord
Copy link
Collaborator

DanielNoord commented Sep 20, 2021

I narrowed down the issue to:

def func(parameter):
    if tuple() + (parameter[1],) in set():
        raise Exception()

Going to see if I can find a solution, but someone might know what's up with this!

@cdce8p
Copy link
Member

cdce8p commented Sep 20, 2021

@DanielNoord The issue is caused by pylint-dev/astroid#979. Unknown doesn't have a AsStringVisitor, thus astroid -> _to_literal will fail. It doesn't really make sense to add one either, since the node is Unknown. What should the return string be in that case?

Instead, I would recommend to add AttributeError to the list of exceptions here and add a comment here to explain that it can also raise AttributeError, since not all nodes do have a visitor.

@cdce8p cdce8p added this to the 2.11.2 milestone Sep 20, 2021
@cdce8p
Copy link
Member

cdce8p commented Sep 20, 2021

@DanielNoord Would you like to prepare the PR? I can do the review if you link me.

@DanielNoord
Copy link
Collaborator

Yeah sure! Will probably do that tomorrow if that's okay?

@cdce8p
Copy link
Member

cdce8p commented Sep 20, 2021

Yeah sure! Will probably do that tomorrow if that's okay?

No problem. Thanks!

DanielNoord added a commit to DanielNoord/astroid that referenced this issue Sep 21, 2021
cdce8p pushed a commit to pylint-dev/astroid that referenced this issue Sep 21, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Needs astroid update Needs an astroid update (probably a release too) before being mergable label Sep 25, 2021
cdce8p added a commit that referenced this issue Oct 7, 2021
Closes #5048

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash Needs astroid update Needs an astroid update (probably a release too) before being mergable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants