Skip to content

classmethod of Generic class fail with TypeVarType is already analyzed #1898

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

Closed
tharvik opened this issue Jul 18, 2016 · 2 comments
Closed
Assignees
Labels

Comments

@tharvik
Copy link
Contributor

tharvik commented Jul 18, 2016

Stacktrack very closely related to #1682, but unable to find a way to related both issues.

from typing import TypeVar, Generic

T = TypeVar('T')

class A(Generic[T]):
    def __init__(self, a: T) -> None: ...
    @classmethod
    def f(cls) -> None: ...

gives

*** INTERNAL ERROR ***
asd4.py:7: error: Internal error -- please report a bug at https://github.com/python/mypy/issues
NOTE: you can use "mypy --pdb ..." to drop into the debugger when this happens.
Traceback (most recent call last):
  File "/usr/lib/python-exec/python3.4/mypy", line 6, in <module>
    main(__file__)
  File "/usr/lib64/python3.4/site-packages/mypy/main.py", line 40, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/lib64/python3.4/site-packages/mypy/main.py", line 81, in type_check_only
    options=options)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 177, in build
    dispatch(sources, manager)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1331, in dispatch
    process_graph(graph, manager)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1469, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1542, in process_stale_scc
    graph[id].semantic_analysis()
  File "/usr/lib64/python3.4/site-packages/mypy/build.py", line 1296, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 243, in visit_file
    self.accept(d)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 2306, in accept
    node.accept(self)
  File "/usr/lib64/python3.4/site-packages/mypy/nodes.py", line 660, in accept
    return visitor.visit_class_def(self)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 564, in visit_class_def
    defn.defs.accept(self)
  File "/usr/lib64/python3.4/site-packages/mypy/nodes.py", line 723, in accept
    return visitor.visit_block(self)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 1014, in visit_block
    self.accept(s)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 2306, in accept
    node.accept(self)
  File "/usr/lib64/python3.4/site-packages/mypy/nodes.py", line 544, in accept
    return visitor.visit_decorator(self)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 1626, in visit_decorator
    dec.func.accept(self)
  File "/usr/lib64/python3.4/site-packages/mypy/nodes.py", line 475, in accept
    return visitor.visit_func_def(self)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 316, in visit_func_def
    self.analyze_function(defn)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 468, in analyze_function
    defn.type = self.anal_type(defn.type)
  File "/usr/lib64/python3.4/site-packages/mypy/semanal.py", line 1039, in anal_type
    return t.accept(a)
  File "/usr/lib64/python3.4/site-packages/mypy/types.py", line 606, in accept
    return visitor.visit_callable_type(self)
  File "/usr/lib64/python3.4/site-packages/mypy/typeanal.py", line 204, in visit_callable_type
    return t.copy_modified(arg_types=self.anal_array(t.arg_types),
  File "/usr/lib64/python3.4/site-packages/mypy/typeanal.py", line 274, in anal_array
    res.append(t.accept(self))
  File "/usr/lib64/python3.4/site-packages/mypy/types.py", line 606, in accept
    return visitor.visit_callable_type(self)
  File "/usr/lib64/python3.4/site-packages/mypy/typeanal.py", line 204, in visit_callable_type
    return t.copy_modified(arg_types=self.anal_array(t.arg_types),
  File "/usr/lib64/python3.4/site-packages/mypy/typeanal.py", line 274, in anal_array
    res.append(t.accept(self))
  File "/usr/lib64/python3.4/site-packages/mypy/types.py", line 448, in accept
    return visitor.visit_type_var(self)
  File "/usr/lib64/python3.4/site-packages/mypy/typeanal.py", line 201, in visit_type_var
    raise RuntimeError('TypeVarType is already analyzed')
RuntimeError: TypeVarType is already analyzed

Removing the Generic subclassing also triggers it.

@gvanrossum gvanrossum added this to the 0.4.x milestone Jul 18, 2016
@gvanrossum
Copy link
Member

Looking into this, but making slow progress. I just rediscovered that removing Generic[T] from the base still triggers this. The immediate cause seems to be that the type of cls in the class method is derived from the constructor signature, but apparently if that contains type variables it always thinks it's a generic function, and that always causes this crash.

So there are really two bugs -- the crash when the constructor is actually a generic function; and the way the cls type believes the constructor is a generic function even when its type variable belongs to the class.

@gvanrossum gvanrossum self-assigned this Aug 10, 2016
gvanrossum pushed a commit that referenced this issue Aug 10, 2016
The raise caused two specific errors that are now fixed.  The fix
causes no new errors.  I don't understand what's going on, but the
raise has been there since prehistoric times, and may well have been
misguided in the first place.

Fixes #1898.
@gvanrossum
Copy link
Member

My best guess is that the raise causing this crash was wrong.

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

No branches or pull requests

2 participants