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 attempting a generic NamedTuple #1682

Closed
gnprice opened this issue Jun 8, 2016 · 2 comments
Closed

Crash when attempting a generic NamedTuple #1682

gnprice opened this issue Jun 8, 2016 · 2 comments

Comments

@gnprice
Copy link
Collaborator

gnprice commented Jun 8, 2016

This program causes mypy to crash:

from typing import TypeVar, NamedTuple, Generic

T = TypeVar('T')

class C(NamedTuple('C', [('x', T),]), Generic[T]):
    def get(self):
        # type: () -> T
        pass
Traceback (most recent call last):
  File "/home/gregprice/w/mypy/env/bin/mypy", line 12, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/home/gregprice/w/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/main.py", line 54, in main
    res = type_check_only(sources, bin_dir, options)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/main.py", line 102, in type_check_only
    python_path=options.python_path)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/build.py", line 211, in build
    dispatch(sources, manager)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/build.py", line 1336, in dispatch
    process_graph(graph, manager)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/build.py", line 1467, in process_graph
    process_stale_scc(graph, scc)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/build.py", line 1493, in process_stale_scc
    graph[id].semantic_analysis()
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/build.py", line 1303, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 247, in visit_file
    self.accept(d)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 2283, in accept
    node.accept(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/nodes.py", line 650, in accept
    return visitor.visit_class_def(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 567, in visit_class_def
    defn.defs.accept(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/nodes.py", line 715, in accept
    return visitor.visit_block(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 1018, in visit_block
    self.accept(s)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 2283, in accept
    node.accept(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/nodes.py", line 462, in accept
    return visitor.visit_func_def(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 322, in visit_func_def
    self.analyze_function(defn)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 474, in analyze_function
    defn.type = self.anal_type(defn.type)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/semanal.py", line 1043, in anal_type
    return t.accept(a)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/types.py", line 496, in accept
    return visitor.visit_callable_type(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/typeanal.py", line 189, in visit_callable_type
    return t.copy_modified(arg_types=self.anal_array(t.arg_types),
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/typeanal.py", line 259, in anal_array
    res.append(t.accept(self))
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/types.py", line 637, in accept
    return visitor.visit_tuple_type(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/typeanal.py", line 203, in visit_tuple_type
    return TupleType(self.anal_array(t.items), fallback, t.line)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/typeanal.py", line 259, in anal_array
    res.append(t.accept(self))
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/types.py", line 340, in accept
    return visitor.visit_type_var(self)
  File "/home/gregprice/w/mypy/env/lib/python3.4/site-packages/mypy/typeanal.py", line 186, in visit_type_var
    raise RuntimeError('TypeVarType is already analyzed')
RuntimeError: TypeVarType is already analyzed

*** INTERNAL ERROR ***

/tmp/foo.py:6: error: Internal error -- please report a bug at https://github.com/python/mypy/issues

This looks a lot like #685 originally did, but the repro there no longer crashes.

Spotted this in python/typeshed#236 -- this repro is a reduction of that code. The difference from the repros in #658 that no longer crash seems to be the method definition.

@elazarg
Copy link
Contributor

elazarg commented Nov 17, 2016

It does not crash anymore. Instead It gives

error: 'T' is a type variable and only valid in type context

@ilevkivskyi
Copy link
Member

Yes, this no longer crashes, and we have #685 to track the error. So that I am closing this.

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

5 participants