Skip to content

'ListComp' object has no attribute 'name' when variable is named "type" #5461

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

Closed
xavfernandez opened this issue Dec 3, 2021 · 5 comments · Fixed by pylint-dev/astroid#1285 or #5623
Labels
Bug 🪲 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

@xavfernandez
Copy link

Bug description

Pylint/Astroid crashes on list comprehension using a variable named type.

$ cat test.py 
[type for type in [] if type['id']]

crashes but

$ cat test.py 
[toto for toto in [] if toto['id']]

doesn't.

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:1:0: W0104: Statement seems to have no effect (pointless-statement)
Exception on node <Subscript l.1 at 0x7f919c740040> in file '/home/xafer/.virtualenvs/tmp-5062295dba883df/test.py'
Traceback (most recent call last):
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/astroid/inference_tip.py", line 28, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_type_sub at 0x7f919cc4d1f0>, <Name.type l.1 at 0x7f919c7400d0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/pylint/checkers/typecheck.py", line 1820, in visit_subscript
    self._check_invalid_sequence_index(node)
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/pylint/checkers/typecheck.py", line 1510, in _check_invalid_sequence_index
    parent_type = safe_infer(subscript.value)
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/pylint/checkers/utils.py", line 1242, in safe_infer
    value = next(infer_gen)
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 146, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/astroid/inference_tip.py", line 30, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/astroid/brain/brain_type.py", line 51, in infer_type_sub
    if node_scope.qname() != "builtins":
  File "/home/xafer/.virtualenvs/tmp-5062295dba883df/lib/python3.9/site-packages/astroid/nodes/scoped_nodes.py", line 246, in qname
    return f"{self.parent.frame().qname()}.{self.name}"
AttributeError: 'ListComp' object has no attribute 'name'
test.py:1:0: F0001: Fatal error while checking 'test.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/xafer/.cache/pylint/pylint-crash-2021-12-03-11.txt'. (fatal)

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

Expected behavior

No crash

Pylint version

pylint 2.12.1
astroid 2.9.0
Python 3.9.9 (main, Nov 20 2021, 21:30:06) 
[GCC 11.1.0]

OS / Environment

No response

Additional dependencies

astroid==2.9.0
isort==5.10.1
lazy-object-proxy==1.6.0
mccabe==0.6.1
platformdirs==2.4.0
pylint==2.12.1
toml==0.10.2
typing_extensions==4.0.1
wrapt==1.13.3

@xavfernandez xavfernandez added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 3, 2021
@Pierre-Sassoulas Pierre-Sassoulas added 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 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 3, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Dec 3, 2021
@cdce8p
Copy link
Member

cdce8p commented Dec 5, 2021

@xavfernandez Thanks for opening the issue. Personally, I wouldn't recommend to use type for anything other than the builtin function and as generic type but this crash can nevertheless be fixed. For reference: pylint-dev/astroid#1284 (review)

@xavfernandez
Copy link
Author

xavfernandez commented Dec 6, 2021

@cdce8p Yes, I usually also avoid builtin names (and I actually fixed the crash in my build by renaming the variable).
The issue appeared when I was migrating a legacy project from Python 3.8 to Python 3.9.

And now that I think about it, the bug doesn't happen in Python 3.8:

$ cat test.py
[type for type in [] if type['id']]
$ pylint --version
pylint 2.12.2
astroid 2.9.0
Python 3.8.9 (default, May 19 2021, 07:50:51) 
[GCC 10.2.0]
$ pylint test.py
************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:1:0: W0104: Statement seems to have no effect (pointless-statement)

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

vs

$ cat test.py
[type for type in [] if type['id']]
$ pylint --version
pylint 2.12.2
astroid 2.9.0
Python 3.9.9 (main, Nov 20 2021, 21:30:06) 
[GCC 11.1.0]
$ pylint test.py
************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:1:0: W0104: Statement seems to have no effect (pointless-statement)
Exception on node <Subscript l.1 at 0x7fb9a5f624f0> in file '/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/test.py'
Traceback (most recent call last):
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/astroid/inference_tip.py", line 28, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_type_sub at 0x7fb9a64715e0>, <Name.type l.1 at 0x7fb9a5f625b0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/pylint/checkers/typecheck.py", line 1820, in visit_subscript
    self._check_invalid_sequence_index(node)
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/pylint/checkers/typecheck.py", line 1510, in _check_invalid_sequence_index
    parent_type = safe_infer(subscript.value)
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/pylint/checkers/utils.py", line 1251, in safe_infer
    value = next(infer_gen)
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 146, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/astroid/inference_tip.py", line 30, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/astroid/brain/brain_type.py", line 51, in infer_type_sub
    if node_scope.qname() != "builtins":
  File "/home/xafer/.virtualenvs/tmp-6d7976bdba8cd7/lib/python3.9/site-packages/astroid/nodes/scoped_nodes.py", line 246, in qname
    return f"{self.parent.frame().qname()}.{self.name}"
AttributeError: 'ListComp' object has no attribute 'name'
test.py:1:0: F0001: Fatal error while checking 'test.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/xafer/.cache/pylint/pylint-crash-2021-12-06-10.txt'. (fatal)

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

@cdce8p
Copy link
Member

cdce8p commented Dec 6, 2021

And now that I think about it, the bug doesn't happen in Python 3.8

That's to be expected. The bug is related to the handling of builtin types in Python 3.9 and higher. It will be fixed with pylint-dev/astroid#1285.

@DanielNoord
Copy link
Collaborator

@cdce8p Do we still want to add a test for this in pylint?

@DanielNoord
Copy link
Collaborator

Made a PR with little regression test

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 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
4 participants