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

pylint throws an exception with scapy 2.4.5 while processing NamedTuple typing #4383

Closed
bdraco opened this issue Apr 20, 2021 · 0 comments · Fixed by pylint-dev/astroid#956
Closed
Assignees
Labels
Astroid Related to astroid Crash 💥 A bug that makes pylint crash

Comments

@bdraco
Copy link

bdraco commented Apr 20, 2021

https://github.com/secdev/scapy/blob/master/scapy/compat.py

Steps to reproduce

Given a file a.py:

# (Disable message unrelated to the bug)
# pylint: disable=missing-docstring,...
# Broken < Python 3.7
if sys.version_info >= (3, 7):
    from typing import NamedTuple
else:
    # Hack for Python < 3.7 - Implement NamedTuple pickling
    def _unpickleNamedTuple(name, len_params, *args):
        return collections.namedtuple(
            name,
            args[:len_params]
        )(*args[len_params:])

    def NamedTuple(name, params):
        tup_params = tuple(x[0] for x in params)
        cls = collections.namedtuple(name, tup_params)

        class _NT(cls):
            def __reduce__(self):
                """Used by pickling methods"""
                return (_unpickleNamedTuple,
                        (name, len(tup_params)) + tup_params + tuple(self))
        _NT.__name__ = cls.__name__
        return _NT

Current behavior

Result of pylint a.py:

Traceback (most recent call last):
  File "/root/home-assistant/venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1044, in get_ast
    return MANAGER.ast_from_file(filepath, modname, source=True)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/manager.py", line 101, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/builder.py", line 139, in file_build
    return self._post_build(module, encoding)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/builder.py", line 163, in _post_build
    module = self._manager.visit_transforms(module)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/manager.py", line 78, in visit_transforms
    return self._transform.visit(node)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 89, in visit
    module.body = [self._visit(child) for child in module.body]
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 89, in <listcomp>
    module.body = [self._visit(child) for child in module.body]
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 61, in _visit_generic
    return [self._visit_generic(child) for child in node]
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 61, in <listcomp>
    return [self._visit_generic(child) for child in node]
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 57, in _visit
    return self._transform(node)
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/transforms.py", line 39, in _transform
    if predicate is None or predicate(node):
  File "/root/home-assistant/venv/lib/python3.8/site-packages/astroid/brain/brain_namedtuple_enum.py", line 472, in <lambda>
    lambda node: node.name == "NamedTuple" and node.parent.name == "typing",
AttributeError: 'If' object has no attribute 'name'
************* Module run
run.py:1:0: F0002: <class 'AttributeError'>: 'If' object has no attribute 'name' (astroid-error)

Expected behavior

pylint --version output

Result of pylint --version output:

pylint 2.7.4
astroid 2.5.3
Python 3.8.1 (default, Dec 31 2019, 18:42:42) 
[GCC 7.4.0]

Also fails with

# pylint --version
pylint 3.0.0a1
astroid 2.5.3
Python 3.8.1 (default, Dec 31 2019, 18:42:42) 
[GCC 7.4.0]
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants