Skip to content

Crash 'AssignAttr' object has no attribute 'name' when not TYPE_CHECKING #5111

Closed
@JacobHayes

Description

@JacobHayes

Bug description

This bug arises under somewhat odd conditions. My original code was trying to patch around python/cpython#28714 with:

from graphlib import TopologicalSorter
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    print("do some stuff")
else:
    from types import GenericAlias

    TopologicalSorter.__class_getitem__ = classmethod(GenericAlias)

I've tried to reduce as much as possible and the key parts seem to be:

  • in the else block after if TYPE_CHECKING (if not TYPE_CHECKING doesn't seem to trigger)
  • when referencing an import within the block (doesn't have to be GenericAlias and moving import to top fixes)
  • when assigning an attribute (doesn't have to be TopologicalSorter nor __class_getitem__)

Configuration

No response

Command used

pylint min_repro.py

Pylint output

************* Module min_repro
min_repro.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Exception on node <Name.GenericAlias l.9 at 0x10dae0580> in file '/private/tmp/min_repro.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/variables.py", line 1106, in visit_name
    ) = self._is_variable_violation(
  File "/usr/local/lib/python3.9/site-packages/pylint/checkers/variables.py", line 1533, in _is_variable_violation
    defined_in_or_else = any(
  File "/usr/local/lib/python3.9/site-packages/pylint/checkers/variables.py", line 1534, in <genexpr>
    target.name == name for target in definition.targets
AttributeError: 'AssignAttr' object has no attribute 'name'
min_repro.py:1:0: F0001: Fatal error while checking 'min_repro.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/Users/jacobhayes/Library/Caches/pylint/pylint-crash-2021-10-03-18.txt'. (fatal)

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

Expected behavior

************* Module min_repro
min_repro.py:1:0: C0114: Missing module docstring (missing-module-docstring)

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

Pylint version

pylint 2.11.1
astroid 2.8.0
Python 3.9.7 (default, Sep  3 2021, 12:37:55)
[Clang 12.0.5 (clang-1205.0.22.9)]

OS / Environment

macOS

Additional dependencies

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions