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

AttributeError: 'Attribute' object has no attribute 'name' #7380

Closed
bleudev opened this issue Aug 29, 2022 · 9 comments · Fixed by #7386
Closed

AttributeError: 'Attribute' object has no attribute 'name' #7380

bleudev opened this issue Aug 29, 2022 · 9 comments · Fixed by #7386
Labels
Crash 💥 A bug that makes pylint crash Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@bleudev
Copy link

bleudev commented Aug 29, 2022

Bug description

Linting:

lst = []
for _var in lst:
    del _var

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 769, in _lint_file
    check_astroid_module(module)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1029, in check_astroid_module
    retval = self._check_astroid_module(
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1079, in _check_astroid_module
    walker.walk(node)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 93, in walk
    self.walk(child)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 93, in walk
    self.walk(child)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 93, in walk
    self.walk(child)
  [Previous line repeated 1 more time]
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 90, in walk
    callback(astroid)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/checkers/modified_iterating_checker.py", line 60, in visit_for
    self._modified_iterating_check_on_node_and_children(body_node, iter_obj)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/checkers/modified_iterating_checker.py", line 66, in _modified_iterating_check_on_node_and_children
    self._modified_iterating_check(body_node, iter_obj)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/checkers/modified_iterating_checker.py", line 96, in _modified_iterating_check
    args=(iter_obj.name,),
AttributeError: 'Attribute' object has no attribute 'name'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 734, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 771, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

No crash.

Pylint version

pylint-2.15.0
astroid-2.12.5
CPython 3.8.13

OS / Environment

ubuntu-latest

@bleudev bleudev added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Aug 29, 2022
@jacobtylerwalls jacobtylerwalls added Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 30, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.1 milestone Aug 30, 2022
@jacobtylerwalls jacobtylerwalls added the Good first issue Friendly and approachable by new contributors label Aug 30, 2022
@DanielNoord
Copy link
Collaborator

Note that the error in the OP doesn't actually reproduce. The post before the edit does provide the necessary info though.

@itttgg As I had to dive into your codebase to find the issue I had a look around. I noticed that you use NoReturn to annotate functions that don't return anything. This is unsolicited advice but you should use -> None for such functions. NoReturn implies that a function will always raise an exception, see https://docs.python.org/3/library/typing.html#typing.NoReturn.
An __init__ does return, it just returns nothing, thus the -> None.

For the issue reported I have pushed a fix!

@bleudev
Copy link
Author

bleudev commented Aug 31, 2022

I noticed that you use NoReturn to annotate functions that don't return anything. This is unsolicited advice but you should use -> None for such functions. NoReturn implies that a function will always raise an exception, see https://docs.python.org/3/library/typing.html#typing.NoReturn. An __init__ does return, it just returns nothing, thus the -> None.

I thought that NoReturn == None, but it's not. Sorry

@DanielNoord
Copy link
Collaborator

I thought that NoReturn == None, but it's not. Sorry

No problem, just thought I'd let you know. A year ago I had never even heard of Python typing. We're all just learning about it 😄

@bleudev
Copy link
Author

bleudev commented Aug 31, 2022

No problem, just thought I'd let you know. A year ago I had never even heard of Python typing. We're all just learning about it 😄

I mean, is there a difference?

@DanielNoord
Copy link
Collaborator

Between NoReturn and None?

@bleudev
Copy link
Author

bleudev commented Aug 31, 2022

Between NoReturn and None?

Yes

@DanielNoord
Copy link
Collaborator

Yeah!

For example:

def func(x: int) -> None:
    print(x)
    x = x + 1
    print(x)

def func_two(x: int) -> NoReturn:
    print(x)
    raise ValueError

func()
func_two()

With the following code, func_two will execute after func and then the program exists with a ValueError.
If you switch them around func will never be executed as the program already stops at the ValueError raised by func_two. That's why NoReturn is helpful. It shows that without some mitigation calling a certain function will create an error that will terminate the program.

@bleudev
Copy link
Author

bleudev commented Aug 31, 2022

With the following code, func_two will execute after func and then the program exists with a ValueError. If you switch them around func will never be executed as the program already stops at the ValueError raised by func_two. That's why NoReturn is helpful. It shows that without some mitigation calling a certain function will create an error that will terminate the program.

Ok i'm fix this error in my code

@jacobtylerwalls
Copy link
Member

Note that the error in the OP doesn't actually reproduce. The post before the edit does provide the necessary info though.

Sorry @DanielNoord, I was the one who did the edit. I was trying to bisect the file to get to the minimum reproducer, and I guess I made a mistake somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants