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 on double inheritance #2001

Closed
dmoisset opened this issue Aug 9, 2016 · 1 comment
Closed

Crash on double inheritance #2001

dmoisset opened this issue Aug 9, 2016 · 1 comment
Labels

Comments

@dmoisset
Copy link
Contributor

dmoisset commented Aug 9, 2016

While writing an iterable file-like class class MyFileLike(object) I was having trouble getting the Iterable protocol to be implicitly recognized. So I decided to add an explicit Iterable parent:

from typing import Iterator, Iterable

class MyFileLike(object, Iterable[str]):
    def readlines(self) -> Iterator[str]:
        return iter(["one", "two", "three"])

    __iter__ = readlines

m = MyFileLike()
it = iter(m)
for l in it: print(l)

That snippet crashes mypy:

Traceback (most recent call last):
  File "/home/machinalis/.virtualenvs/mypy-django/bin/mypy", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/home/machinalis/oss/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/home/machinalis/oss/mypy/mypy/main.py", line 38, in main
    res = type_check_only(sources, bin_dir, options)
  File "/home/machinalis/oss/mypy/mypy/main.py", line 79, in type_check_only
    options=options)
  File "/home/machinalis/oss/mypy/mypy/build.py", line 180, in build
    dispatch(sources, manager)
  File "/home/machinalis/oss/mypy/mypy/build.py", line 1335, in dispatch
    process_graph(graph, manager)
  File "/home/machinalis/oss/mypy/mypy/build.py", line 1478, in process_graph
    process_stale_scc(graph, scc)
  File "/home/machinalis/oss/mypy/mypy/build.py", line 1551, in process_stale_scc
    graph[id].semantic_analysis()
  File "/home/machinalis/oss/mypy/mypy/build.py", line 1300, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 244, in visit_file
    self.accept(d)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 2462, in accept
    node.accept(self)
  File "/home/machinalis/oss/mypy/mypy/nodes.py", line 664, in accept
    return visitor.visit_class_def(self)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 572, in visit_class_def
    defn.defs.accept(self)
  File "/home/machinalis/oss/mypy/mypy/nodes.py", line 727, in accept
    return visitor.visit_block(self)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 1028, in visit_block
    self.accept(s)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 2462, in accept
    node.accept(self)
  File "/home/machinalis/oss/mypy/mypy/nodes.py", line 765, in accept
    return visitor.visit_assignment_stmt(self)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 1060, in visit_assignment_stmt
    s.rvalue.accept(self)
  File "/home/machinalis/oss/mypy/mypy/nodes.py", line 1116, in accept
    return visitor.visit_name_expr(self)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 1906, in visit_name_expr
    n = self.lookup(expr.name, expr)
  File "/home/machinalis/oss/mypy/mypy/semanal.py", line 2250, in lookup
    return self.type[name]
  File "/home/machinalis/oss/mypy/mypy/nodes.py", line 1864, in __getitem__
    raise KeyError(name)
KeyError: 'readlines'

*** INTERNAL ERROR ***

myiter.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.

The crash goes away if I remove the object reference and only inherit from Iterable[str].

@gvanrossum gvanrossum added this to the 0.4.x milestone Aug 9, 2016
@gvanrossum
Copy link
Member

Yeah, this is an easy repro. There are too many similar crashes where apparently the mro is used after an error message about it has been produced. :-( In this case, the code first produced the error

error: Cannot determine consistent method resolution order (MRO) for "MyFileLike"

Errors are buffered so you don't see them when there's a crash. (Hmm... Maybe we could fix that!)

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