Skip to content

"AssertionError: Cannot find component" for local NamedTuple #7281

@karlicoss

Description

@karlicoss

I've got an obscure bug which I narrowed down to some cache issues when you have a local class inheriting from NamedTuple.

Stacktrace:

Traceback (most recent call last):
  File "/home/karlicos/.local/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/home/karlicos/.local/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 83, in main
  File "mypy/build.py", line 164, in build
  File "mypy/build.py", line 224, in _build
  File "mypy/build.py", line 2579, in dispatch
  File "mypy/build.py", line 2885, in process_graph
  File "mypy/build.py", line 2963, in process_fresh_modules
  File "mypy/build.py", line 1876, in fix_cross_refs
  File "mypy/fixup.py", line 25, in fixup_module
  File "mypy/fixup.py", line 76, in visit_symbol_table
  File "mypy/fixup.py", line 265, in lookup_qualified_stnode
  File "mypy/lookup.py", line 47, in lookup_fully_qualified
AssertionError: Cannot find component 'NT@3' for 'breaking.NT@3

Easiest to reproduce with the following script (it's crashing on the last mypy invocation)

#!/bin/bash -eux
cd "$(dirname "$0")"

rm -rf .mypy_cache

echo 'from typing import NamedTuple' > breaking.py
echo 'def test():'                  >> breaking.py
echo '  class NT(NamedTuple):'      >> breaking.py
echo '    pass'                     >> breaking.py

echo 'import breaking' > script.py
mypy script.py

echo '' > script.py
mypy script.py

echo 'import breaking' > script.py
mypy script.py

Most similar issue to this one I found seems to be #2931, but using singleline NamedTuple definition (e.g. NT = NamedTuple('NT', [])) doesn't result in failure, so I figured this is worth separate issue.
Using @dataclass doesn't result in failure either.

I'm using mypy 0.720. (upd: still happens on 0.800)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions