Skip to content

specially crafted __rpow__ signature causes mypy to terminate with an "Internal Error" #5419

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
gossrock opened this issue Aug 5, 2018 · 5 comments

Comments

@gossrock
Copy link

gossrock commented Aug 5, 2018

Originally posted as Typeshed issue python/typeshed#2358

I discovered this bug mainly because I didn't know how __repr__ was supposed to work.
This line (even if it's the only line in the python file):

def __rpow__(self, x:int, mod:int = None) -> int: ...

produces this output:

./arbitrary_base_math_bug.py:5: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.620
Traceback (most recent call last):
  File "/home/peter/Documents/scratch_code/.venv/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/main.py", line 91, in main
    res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/main.py", line 148, in type_check_only
    fscache=fscache)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/build.py", line 177, in build
    flush_errors, fscache)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/build.py", line 350, in _build
    graph = dispatch(sources, manager)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/build.py", line 2560, in dispatch
    process_graph(graph, manager)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/build.py", line 2853, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/build.py", line 2976, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/build.py", line 2151, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 253, in check_first_pass
    self.accept(d)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 352, in accept
    stmt.accept(self)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/nodes.py", line 591, in accept
    return visitor.visit_func_def(self)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 663, in visit_func_def
    self._visit_func_def(defn)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 667, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 729, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 779, in check_func_def
    self.check_reverse_op_method(item, typ, name, defn)
  File "/home/peter/Documents/scratch_code/.venv/lib/python3.7/site-packages/mypy/checker.py", line 1006, in check_reverse_op_method
    assert len(reverse_type.arg_types) == 2
AssertionError: 
./arbitrary_base_math_bug.py:5: : note: use --pdb to drop into pdb

these very similar but slightly different lines will cause various warnings but will not cause the above problem:

def __rpow__(self, x:int, mod:int = None): ...
def __rpow__(self, x:int, mod:int) -> int: ...
def __rpow__(x:int, mod:int = None) -> int: ...
def __rpow2__(self, x:int, mod:int = None) -> int: ...
@gossrock
Copy link
Author

gossrock commented Aug 5, 2018

gvanrossum made the following comment on the original typeshed issue:

Yeah, this definitely belongs in the mypy tracker. Note that on master it crashes with a different traceback, because that assert was changed to >= 2 by #5348. (@srittau take note)

@Michael0x2a
Copy link
Collaborator

Just to double-check -- is this an issue with __repr__ or with reverse-operator methods like __rpow__?

Your title uses the former but your post uses the latter.

@srittau
Copy link
Contributor

srittau commented Aug 5, 2018

For a file containing just:

def __rpow__(self, x: int, mod: int = ...) -> int: ...

I get the following output in mypy HEAD:

foo.pyi:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.630+dev-def2c63990dd1a86907d94a3c32860e35c64a537
Traceback (most recent call last):
  File "/home/srittau/.virtualenvs/mypy/bin/mypy", line 11, in <module>
    load_entry_point('mypy==0.630+dev.def2c63990dd1a86907d94a3c32860e35c64a537', 'console_scripts', 'mypy')()
  File "/home/srittau/Projekte/mypy/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/home/srittau/Projekte/mypy/mypy/main.py", line 91, in main
    res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
  File "/home/srittau/Projekte/mypy/mypy/main.py", line 148, in type_check_only
    fscache=fscache)
  File "/home/srittau/Projekte/mypy/mypy/build.py", line 177, in build
    flush_errors, fscache)
  File "/home/srittau/Projekte/mypy/mypy/build.py", line 350, in _build
    graph = dispatch(sources, manager)
  File "/home/srittau/Projekte/mypy/mypy/build.py", line 2572, in dispatch
    process_graph(graph, manager)
  File "/home/srittau/Projekte/mypy/mypy/build.py", line 2866, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/srittau/Projekte/mypy/mypy/build.py", line 2989, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/srittau/Projekte/mypy/mypy/build.py", line 2162, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 253, in check_first_pass
    self.accept(d)
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 352, in accept
    stmt.accept(self)
  File "/home/srittau/Projekte/mypy/mypy/nodes.py", line 591, in accept
    return visitor.visit_func_def(self)
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 663, in visit_func_def
    self._visit_func_def(defn)
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 667, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 729, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 779, in check_func_def
    self.check_reverse_op_method(item, typ, name, defn)
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 1028, in check_reverse_op_method
    context=defn)
  File "/home/srittau/Projekte/mypy/mypy/checker.py", line 1098, in check_overlapping_op_methods
    forward_base, forward_name, context)
  File "/home/srittau/Projekte/mypy/mypy/messages.py", line 1002, in operator_method_signatures_overlap
    reverse_method, reverse_class.name(),
AttributeError: 'NoneType' object has no attribute 'name'
foo.pyi:1: : note: use --pdb to drop into pdb

If __rpow__ is part of a class, this checks fine:

class Foo:
    def __rpow__(self, x: int, mod: int = ...) -> int: ...

srittau added a commit to srittau/mypy that referenced this issue Aug 5, 2018
srittau added a commit to srittau/mypy that referenced this issue Aug 5, 2018
Previously, functions that had the same name as a reversible dunder
method (for example, __rpow__) were checked as if they were such a
method. This could lead to crashes and false positives.

Closes: python#5419
@gvanrossum gvanrossum changed the title specially crafted __repr__ signiture causes mypy to terminate with an "Internal Error" specially crafted __rpow__ signature causes mypy to terminate with an "Internal Error" Aug 5, 2018
@srittau
Copy link
Contributor

srittau commented Aug 5, 2018

So it seems we saw two different bugs here: If __rpow__ was part of a class, it crashed. This was fixed in #5348. If __rpow__ was a module-level function, it caused a different crash, which I try to fix in #5421.

@gvanrossum
Copy link
Member

gvanrossum commented Aug 5, 2018 via email

Michael0x2a pushed a commit that referenced this issue Aug 5, 2018
Previously, functions that had the same name as a reversible dunder
method (for example, __rpow__) were checked as if they were such a
method. This could lead to crashes and false positives.

Closes #5419
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