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

“AssertionError: Cannot find component” for local NamedTuple inside class method #10913

Closed
andersk opened this issue Aug 3, 2021 · 1 comment · Fixed by #12803
Closed

Comments

@andersk
Copy link
Contributor

andersk commented Aug 3, 2021

Crash Report

This is a variant of #7281 that still crashes incremental checking on 0.910 and master (9f409c3), both of which include #10432, which was supposed to fix #7281.

The difference in this case is that the local NamedTuple is defined inside a class method rather than a global function.

To Reproduce

#!/bin/sh -eux

rm -rf .mypy_cache

echo 'from typing import NamedTuple' >breaking.py
echo 'class C:' >>breaking.py
echo '  def test(self):' >>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

Traceback

Traceback (most recent call last):
  File "/home/anders/python/mypy/.direnv/python-3.9.5/bin/mypy", line 33, in <module>
    sys.exit(load_entry_point('mypy', 'console_scripts', 'mypy')())
  File "/home/anders/python/mypy/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/anders/python/mypy/mypy/main.py", line 87, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/anders/python/mypy/mypy/main.py", line 165, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/anders/python/mypy/mypy/build.py", line 179, in build
    result = _build(
  File "/home/anders/python/mypy/mypy/build.py", line 254, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/anders/python/mypy/mypy/build.py", line 2698, in dispatch
    process_graph(graph, manager)
  File "/home/anders/python/mypy/mypy/build.py", line 3015, in process_graph
    process_fresh_modules(graph, prev_scc, manager)
  File "/home/anders/python/mypy/mypy/build.py", line 3093, in process_fresh_modules
    graph[id].fix_cross_refs()
  File "/home/anders/python/mypy/mypy/build.py", line 1992, in fix_cross_refs
    fixup_module(self.tree, self.manager.modules,
  File "/home/anders/python/mypy/mypy/fixup.py", line 26, in fixup_module
    node_fixer.visit_symbol_table(tree.names, tree.fullname)
  File "/home/anders/python/mypy/mypy/fixup.py", line 77, in visit_symbol_table
    stnode = lookup_qualified_stnode(self.modules, cross_ref,
  File "/home/anders/python/mypy/mypy/fixup.py", line 306, in lookup_qualified_stnode
    return lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
  File "/home/anders/python/mypy/mypy/lookup.py", line 47, in lookup_fully_qualified
    assert key in names, "Cannot find component %r for %r" % (key, name)
AssertionError: Cannot find component 'NT@4' for 'breaking.C.NT@4'

Your Environment

  • Mypy version used: 0.910, 0.920+dev.9f409c3d89c7fa65eff8cf67883473487e6effd4
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.5
  • Operating system and version: NixOS 21.11
@andersk andersk added the crash label Aug 3, 2021
andersk added a commit to andersk/zulip that referenced this issue Aug 3, 2021
The locally defined NamedTuple was triggering a mypy caching bug
(python/mypy#10913), and we don’t use the
tuple behavior anyway.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip that referenced this issue Aug 3, 2021
The locally defined NamedTuple was triggering a mypy caching bug
(python/mypy#10913), and we don’t use the
tuple behavior anyway.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip that referenced this issue Aug 3, 2021
The locally defined NamedTuple was triggering a mypy caching bug
(python/mypy#10913), and we don’t use the
tuple behavior anyway.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
@AlexWaygood
Copy link
Member

Similar traceback in #11121

JukkaL added a commit that referenced this issue May 17, 2022
Make sure the fullname of a named tuple defined within a method matches
the nesting of the definition in the symbol table. Otherwise we'll have a
crash during deserialization.

In particular, a named tuple defined within a method will now be always
stored in the symbol table of the surrounding class, instead of the global
symbol table. Previously there was an inconsistency between old-style
and new-style syntax.

Fix #10913.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants