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

Internal error: base lookup outside class #6033

Closed
PoggledOats opened this issue Dec 8, 2018 · 3 comments
Closed

Internal error: base lookup outside class #6033

PoggledOats opened this issue Dec 8, 2018 · 3 comments

Comments

@PoggledOats
Copy link

I am getting this error with mypy 0.650 from git master branch with python3.5.2 on Ubuntu16.04 and with python3.6.5 on Ubuntu18.04

$ cat tree3.py 

from typing import List, Optional, TypeVar

T = TypeVar('T', bound='TreeBase')

class TreeBase(object):
    def __init__(self : T) -> None:
        self.parent = None # type: Optional[T]
        self.children = [] # type: List[T]

    def addChild(self : T, node : T) -> None:
        self.children.append(node)
        node.parent = self

    def extract(self : T) -> None:
        if self.parent:
            self.parent.children.remove(self)
            self.parent = None

class IdTree(TreeBase):
    def __init__(self, name : str) -> None:
        super().__init__()
        self.id = name      

    def childById(self : 'IdTree', name : str) -> Optional['IdTree']:
        for child in self.children:
            if child.id == name:
                return child
        return None

class IdTree2(TreeBase):
    def __init__(self, name : str) -> None:
        self.parent = None # type: Optional['IdTree2']
        self.children = [] # type: List['IdTree2']
        self.id = name      

    def childById(self, name : str) -> Optional['IdTree2']:
        for child in self.children:
            if child.id == name:
                return child
        return None

$ python3 -m mypy tree3.py 
tree3.py:27: error: "T" has no attribute "id"
tree3.py:28: error: Incompatible return value type (got "T", expected "Optional[IdTree]")
tree3.py:33: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.650+dev.49726682ad93af49ae2efdbe44b55e966d99165a
tree3.py:33: : note: please use --show-traceback to print a traceback when reporting a bug

$ python3 -m mypy --show-traceback tree3.py 
tree3.py:27: error: "T" has no attribute "id"
tree3.py:28: error: Incompatible return value type (got "T", expected "Optional[IdTree]")
tree3.py:33: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.650+dev.49726682ad93af49ae2efdbe44b55e966d99165a
Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.5/dist-packages/mypy/__main__.py", line 11, in 
    main(None)
  File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 92, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 157, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 207, in _build
    graph = dispatch(sources, manager)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 2233, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 2533, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 2656, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1792, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 282, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 848, in accept
    return visitor.visit_class_def(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1536, in visit_class_def
    self.accept(defn.defs)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 913, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 611, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 704, in visit_func_def
    self._visit_func_def(defn)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 708, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 770, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 934, in check_func_def
    self.accept(item.body)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 913, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/nodes.py", line 968, in accept
    return visitor.visit_assignment_stmt(self)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1696, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1751, in check_assignment
    if self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue):
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1849, in check_compatibility_all_supers
    base_type, base_node = self.lvalue_type_from_base(lvalue_node, base)
  File "/usr/local/lib/python3.5/dist-packages/mypy/checker.py", line 1936, in lvalue_type_from_base
    assert self_type is not None, "Internal error: base lookup outside class"
AssertionError: Internal error: base lookup outside class
tree3.py:33: : note: use --pdb to drop into pdb
@JelleZijlstra
Copy link
Member

Confirmed on master. Here's a more concise repro:

from typing import Optional, TypeVar

T = TypeVar('T')

class TreeBase(object):
    def __init__(self : T) -> None:
        self.parent = None # type: Optional[T]

class IdTree2(TreeBase):
    def __init__(self) -> None:
        self.parent = None # type: Optional['IdTree2']

If you're looking for a workaround, the error goes away when I remove the : T annotation from __init__'s self parameter. TreeBase should inherit from Generic[T] instead.

@ilevkivskyi
Copy link
Member

I think this is a duplicate of #5846

@Levitanus
Copy link

Levitanus commented Jan 19, 2019

sorry, wrote to #5846

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

No branches or pull requests

4 participants