Nested class in Protocol causes mypy exception
-
Are you reporting a bug, or opening a feature request? bug
-
Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.
from typing_extensions import Protocol
class SomeProto(Protocol):
var: int
class Meta:
abstract = True
class SomeImpl:
def __init__(self) -> None:
var = 5
def a_func(a: SomeProto) -> None:
pass
a_func(SomeImpl())
-
What are the versions of mypy and Python you are using? 0.670
-
Do you see the same issue after installing mypy from Git master? Yes
-
What are the mypy flags you are using? (For example --strict-optional) Doesn't seem to matter what flags are used, Was using defaults on https://mypy-play.net/ to produce the sample code pasted above
-
If mypy crashed with a traceback, please paste the full traceback below.
Traceback (most recent call last):
File "<myvenv>/bin/mypy", line 10, in <module>
sys.exit(console_entry())
File "mypy/checkexpr.py", line 3188, in accept
File "mypy/nodes.py", line 1446, in accept
File "mypy/checkexpr.py", line 235, in visit_call_expr__ExpressionVisitor_glue
File "mypy/checkexpr.py", line 244, in visit_call_expr
File "mypy/checkexpr.py", line 314, in visit_call_expr_inner
File "mypy/checkexpr.py", line 655, in check_call_expr_with_callee_type
File "mypy/checkexpr.py", line 693, in check_call
File "mypy/checkexpr.py", line 776, in check_callable_call
File "mypy/checkexpr.py", line 989, in infer_function_type_arguments
File "mypy/infer.py", line 30, in infer_function_type_arguments
File "mypy/constraints.py", line 66, in infer_constraints_for_callable
File "mypy/constraints.py", line 143, in infer_constraints
File "mypy/types.py", line 620, in accept
File "mypy/constraints.py", line 279, in visit_instance__TypeVisitor_glue
File "mypy/constraints.py", line 328, in visit_instance
File "mypy/constraints.py", line 143, in infer_constraints
File "mypy/types.py", line 620, in accept
File "mypy/constraints.py", line 279, in visit_instance__TypeVisitor_glue
File "mypy/constraints.py", line 344, in visit_instance
File "mypy/subtypes.py", line 472, in is_protocol_implementation
AssertionError:
Hm, actually nested classes in protocols are not supported (also one will be able to mimic it with a protocol field when https://github.com/python/mypy/issues/4536 will be fixed). Anyway, we definitely should not crash.
Thanks, the nested class was in there as a vestige of some refactoring. It took a little while to figure out what was going on because the error doesn't present when running the Protocol subclass through mypy only when you process something with a call to a function using it. So I thought it was worth fixing as other than the mention of "is_protocol_implementation" in the traceback it's not at all clear where to look for the problem.
An even simpler repro of this problem is in https://github.com/python/mypy/issues/8397
Just checked, this still reproes, along with the reproducer in #8397
from typing import TypeVar, Protocol
T = TypeVar("T", covariant=True)
class MyProtocol(Protocol[T]):
class Nested:
pass