Closed
Description
Using issubclass with a runtime checkable Protocol and a union-bound TypeVar fails to narrow the type. The below example is from invoking mypy with mypy --python-version=3.8 reproduce-issubclass-bug.py
. The issue seems similar to #7920. Tested on both 0.770 and master (0.770+dev.afe0667bbe85e048b6bb4e3fc1b7d5a375b7d01f).
from typing import Any
from typing import Protocol
from typing import runtime_checkable
from typing import Type
from typing import TypeVar
from typing import Union
T = TypeVar("T", bound=Any)
@runtime_checkable
class Parsable(Protocol):
@classmethod
def parse(cls: Type[T], data: Any) -> T:
...
R = TypeVar("R", bound=Union[Parsable, str])
def parse_object(response_type: Type[R], item: Any) -> R:
if issubclass(response_type, Parsable):
# note: Revealed type is 'Type[reproduce-issubclass-bug.Parsable]'
reveal_type(response_type)
# error: Incompatible return value type (got "Parsable", expected "R")
return response_type.parse(item)
# note: Revealed type is 'Type[R`-1]'
reveal_type(response_type)
# error: Too many arguments for "Parsable"
# error: Incompatible return value type (got "Union[Parsable, str]", expected "R")
return response_type(item)
Output:
reproduce-issubclass-bug.py:25: note: Revealed type is 'Type[reproduce-issubclass-bug.Parsable]'
reproduce-issubclass-bug.py:27: error: Incompatible return value type (got "Parsable", expected "R")
reproduce-issubclass-bug.py:29: note: Revealed type is 'Type[R`-1]'
reproduce-issubclass-bug.py:32: error: Too many arguments for "Parsable"
reproduce-issubclass-bug.py:32: error: Incompatible return value type (got "Union[Parsable, str]", expected "R")
Found 3 errors in 1 file (checked 1 source file)