Skip to content

Crash inferring a subclass of typing.NamedTuple #7268

Open

Description

Bug description

Hello,

As suggested in #7011 (message), I open a separate issue about that crash.

I managed to spot the exact part of the code that is causing the crash, so maybe that could help.

Here is the faulty snippet:

from typing import NamedTuple

class Color(NamedTuple):
    RED,CYAN,BLUE,BLACK,GREEN,WHITE = 31,36,34,30,32,37

Replacing this code with a declaration line by line resolved my problem:

class Color(NamedTuple):
    RED = 31
    CYAN = 36
    BLUE = 34
    BLACK = 30
    GREEN = 32
    WHITE = 37

I'm using VSC and pylint is running on each save, if that may matter....

Cheers

Configuration

No response

Command used

none (happened in VSC, checking the file on save)

When running `pylint ./create_project/utilities.py`, I get the same kind of stack trace than the one I found in your log `.txt` file

Pylint output

Traceback (most recent call last):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/inference_tip.py", line 37, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_typing_namedtuple_class at 0x7fbdf7d555a0>, <ClassDef.Color l.16 at 0x7fbdf7851120>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 727, in _check_file
    check_astroid_module(ast_node)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 926, in check_astroid_module
    retval = self._check_astroid_module(
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 976, in _check_astroid_module
    walker.walk(node)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 93, in walk
    self.walk(child)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 90, in walk
    callback(astroid)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/checkers/base/basic_checker.py", line 498, in visit_functiondef
    self._check_dangerous_default(node)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/checkers/base/basic_checker.py", line 513, in _check_dangerous_default
    value = next(default.infer())
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 168, in infer
    yield from self._infer(context=context, **kwargs)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/decorators.py", line 136, in raise_if_nothing_inferred
    yield next(generator)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/decorators.py", line 105, in wrapped
    for res in _func(node, context, **kwargs):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/inference.py", line 308, in infer_attribute
    for owner in self.expr.infer(context):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 182, in infer
    for i, result in enumerate(generator):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/decorators.py", line 136, in raise_if_nothing_inferred
    yield next(generator)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/decorators.py", line 105, in wrapped
    for res in _func(node, context, **kwargs):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/bases.py", line 134, in _infer_stmts
    for inf in stmt.infer(context=context):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 158, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/inference_tip.py", line 44, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/astroid/brain/brain_namedtuple_enum.py", line 483, in infer_typing_namedtuple_class
    generated_class_node.locals[attr] = class_node.locals[attr]
KeyError: 'tuple'

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

Traceback (most recent call last):
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 684, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "/home/.../.cache/pypoetry/virtualenvs/create-project-wqycC8Vp-py3.10/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 729, in _check_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

'Shouldn't crash...

Pylint version

pylint 2.14.5
astroid 2.11.7
Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0]

OS / Environment

Ubuntu 22.04

Additional dependencies

poetry 1.1.14, with the following toml file:

[tool.poetry.dependencies]
python = "^3.10"
pydantic = "^1.9.1"

[tool.poetry.dev-dependencies]
pylint = "^2.14.5"
pytest = "^7.1.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    AstroidRelated to astroidCrash 💥A bug that makes pylint crashNeeds reproduction 🔍Need a way to reproduce it locally on a maintainer's machine

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions