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

Crash when class defined in method used for instance variable's type #2559

Closed
gvanrossum opened this issue Dec 12, 2016 · 0 comments
Closed

Comments

@gvanrossum
Copy link
Member

gvanrossum commented Dec 12, 2016

While thinking about #2535 I realized there's a similar case not involving NamedTuple (nor TypedDict):

  • main.py:
import crash
blah  # anything that triggers an error
  • crash.py:
class C:
    def f(self) -> None:
        class A:
            pass
        self.a = A()

This triggers a very similar crash in fixup.py when running mypy -i main.py TWICE (from a cold cache):

$ rm -rf .mypy_cache/
$ mypy -i main.py 
main.py:1: error: Name 'blah' is not defined
$ mypy -i main.py 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/guido/src/mypy/mypy/__main__.py", line 5, in <module>
    main(None)
  File "/Users/guido/src/mypy/mypy/main.py", line 41, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/guido/src/mypy/mypy/main.py", line 86, in type_check_only
    options=options)
  File "/Users/guido/src/mypy/mypy/build.py", line 183, in build
    dispatch(sources, manager)
  File "/Users/guido/src/mypy/mypy/build.py", line 1524, in dispatch
    process_graph(graph, manager)
  File "/Users/guido/src/mypy/mypy/build.py", line 1753, in process_graph
    process_fresh_scc(graph, prev_scc)
  File "/Users/guido/src/mypy/mypy/build.py", line 1822, in process_fresh_scc
    graph[id].fix_cross_refs()
  File "/Users/guido/src/mypy/mypy/build.py", line 1307, in fix_cross_refs
    fixup_module_pass_one(self.tree, self.manager.modules)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 21, in fixup_module_pass_one
    node_fixer.visit_symbol_table(tree.names)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 86, in visit_symbol_table
    self.visit_type_info(value.node)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 55, in visit_type_info
    self.visit_symbol_table(info.names)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 88, in visit_symbol_table
    value.node.accept(self)
  File "/Users/guido/src/mypy/mypy/nodes.py", line 658, in accept
    return visitor.visit_var(self)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 131, in visit_var
    v.type.accept(self.type_fixer)
  File "/Users/guido/src/mypy/mypy/types.py", line 433, in accept
    return visitor.visit_instance(self)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 144, in visit_instance
    node = lookup_qualified(self.modules, type_ref)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 234, in lookup_qualified
    stnode = lookup_qualified_stnode(modules, name)
  File "/Users/guido/src/mypy/mypy/fixup.py", line 245, in lookup_qualified_stnode
    assert '.' in head, "Cannot find %s" % (name,)
AssertionError: Cannot find A
@JukkaL JukkaL added bug mypy got something wrong crash priority-0-high labels Jan 16, 2017
JukkaL pushed a commit that referenced this issue Feb 27, 2017
Fixes #2559

The idea is the same as in PR #2553. The only difference is that mangled names are always stored in globals, otherwise there would be a problem for a method in a nested class that is itself inside a method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants