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

Callback protocol with TypeVar defined in class body leads to crash later #12337

Closed
kstauffer opened this issue Mar 11, 2022 · 0 comments · Fixed by #13526
Closed

Callback protocol with TypeVar defined in class body leads to crash later #12337

kstauffer opened this issue Mar 11, 2022 · 0 comments · Fixed by #13526

Comments

@kstauffer
Copy link
Contributor

kstauffer commented Mar 11, 2022

Crash Report

Two simple programs trigger similar crashes. They both define & use a TypeVar within the body of a class, so that may be the common issue. First program:

from typing import *
class C(Protocol):
    T = TypeVar('T')
    def __call__(self, t: T) -> T:
        ...

def f(t: int) -> int:
    return t

g: C = f  # mypy crashes here

Second program:

from typing import *
class C(Protocol):
    T = TypeVar('T')
    def foo(self, t: T) -> T:
        ...

class D:
    def foo(self, t):
        return t

c: C = D()  # mypy crashes here

Traceback

First program:

a.py:10: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.950+dev.7ee84c71e851ce373d1824ecc09332caa373148c
a.py:10: : note: use --pdb to drop into pdb
Traceback (most recent call last):
  File "/tmp/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/tmp/venv/lib/python3.10/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 180, in build
    result = _build(
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 256, in _build
    graph = dispatch(sources, manager, stdout)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 2727, in dispatch
    process_graph(graph, manager)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 3071, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 3169, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 2186, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 318, in check_first_pass
    self.accept(d)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 424, in accept
    stmt.accept(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/nodes.py", line 1152, in accept
    return visitor.visit_assignment_stmt(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 2193, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 2354, in check_assignment
    rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, context=rvalue,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 3281, in check_simple_assignment
    self.check_subtype(rvalue_type, lvalue_type, context, msg,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 5214, in check_subtype
    if is_subtype(subtype, supertype):
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 94, in is_subtype
    return _is_subtype(left, right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 151, in _is_subtype
    return left.accept(SubtypeVisitor(orig_right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/types.py", line 1328, in accept
    return visitor.visit_callable_type(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 354, in visit_callable_type
    return self._is_subtype(left.fallback, right)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 205, in _is_subtype
    return is_subtype(left, right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 94, in is_subtype
    return _is_subtype(left, right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 151, in _is_subtype
    return left.accept(SubtypeVisitor(orig_right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/types.py", line 1065, in accept
    return visitor.visit_instance(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 286, in visit_instance
    if right.type.is_protocol and is_protocol_implementation(left, right):
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 588, in is_protocol_implementation
    assert supertype is not None
AssertionError: 

Second program crashes like this (almost the same, except frames of the first crash -8:-4 are absent in this stack:

b.py:11: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.950+dev.7ee84c71e851ce373d1824ecc09332caa373148c
b.py:11: : note: use --pdb to drop into pdb
Traceback (most recent call last):
  File "/tmp/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/tmp/venv/lib/python3.10/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/main.py", line 96, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 180, in build
    result = _build(
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 256, in _build
    graph = dispatch(sources, manager, stdout)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 2727, in dispatch
    process_graph(graph, manager)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 3071, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 3169, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/tmp/venv/lib/python3.10/site-packages/mypy/build.py", line 2186, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 318, in check_first_pass
    self.accept(d)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 424, in accept
    stmt.accept(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/nodes.py", line 1152, in accept
    return visitor.visit_assignment_stmt(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 2193, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 2354, in check_assignment
    rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, context=rvalue,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 3281, in check_simple_assignment
    self.check_subtype(rvalue_type, lvalue_type, context, msg,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/checker.py", line 5214, in check_subtype
    if is_subtype(subtype, supertype):
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 94, in is_subtype
    return _is_subtype(left, right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 151, in _is_subtype
    return left.accept(SubtypeVisitor(orig_right,
  File "/tmp/venv/lib/python3.10/site-packages/mypy/types.py", line 1065, in accept
    return visitor.visit_instance(self)
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 286, in visit_instance
    if right.type.is_protocol and is_protocol_implementation(left, right):
  File "/tmp/venv/lib/python3.10/site-packages/mypy/subtypes.py", line 588, in is_protocol_implementation
    assert supertype is not None
AssertionError: 

Your Environment

  • Mypy version used: Github master 0.950+dev.7ee84c71e851ce373d1824ecc09332caa373148c
  • Mypy command-line flags: mypy a.py --show-traceback a.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
  • Operating system and version: WSL Ubuntu 20.04
ilevkivskyi added a commit that referenced this issue Aug 26, 2022
Fixes #6801
Fixes #10577
Fixes #12642
Fixes #12337
Fixes #10639 
Fixes #13390 

All these crashes are in a sense duplicates of each other. Fix is trivial, except I decided to ban type aliases in protocol bodies. Already in the examples in issues, I have found two cases where people wrote `foo = list[str]`, where they clearly wanted `foo: list[str]`. This can cause hard to spot false negatives.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants