Bug Report & To Reproduce
False-positive-emitting behaviour of a type-alias of typing.Callable[P, R] vs a callback Protocol[P, R] is inconsistent when
- Comparing with each other;
- In the context of a stand-alone expression vs being in a
typing.TypeVar(bound=...) expression; - When being accessed from a module as opposed to a straight import.
Source module
# my_module.pyimporttypingastP=t.ParamSpec("P")
R=t.TypeVar("R", covariant=True)
CallableAlias=t.Callable[P, R]
classCallableProtocol(t.Protocol[P, R]):
def__call__(self, *args: P.args, **kwargs: P.kwargs) ->R:
...
t.Callable[..., t.Any] # OKCallableAlias[..., t.Any] # mypy: Type application is only supported for generic classes [misc] \# mypy: Unexpected "..." [misc]CallableProtocol[..., t.Any] # OKC0T=t.TypeVar("C0T", bound=t.Callable[..., t.Any])
C1T=t.TypeVar("C1T", bound=CallableAlias[..., t.Any]) # mypy: Unexpected "..." [misc]C2T=t.TypeVar("C2T", bound=CallableProtocol[..., t.Any])
# All OKc0: t.Callable[..., t.Any]
c1: CallableAlias[..., t.Any]
c2: CallableProtocol[..., t.Any]Module importing from source module
importtypingastimportmy_moduleasmfrommy_moduleimportCallableAlias, CallableProtocolt.Callable[..., t.Any] # OKm.CallableAlias[..., t.Any] # mypy: Type application is only supported for generic classes [misc] \# mypy: Unexpected "..."m.CallableProtocol[..., t.Any] # mypy: Unexpected "..." [misc]CallableAlias[..., t.Any] # mypy: Type application is only supported for generic classes [misc] \# mypy: Unexpected "..."CallableProtocol[..., t.Any] # OKC0T=t.TypeVar("C0T", bound=t.Callable[..., t.Any]) # OKC1T=t.TypeVar("C1T", bound=m.CallableAlias[..., t.Any]) # mypy: Unexpected "..."C2T=t.TypeVar("C2T", bound=m.CallableProtocol[..., t.Any]) # mypy: Unexpected "..."C3T=t.TypeVar("C3T", bound=CallableAlias[..., t.Any]) # mypy: Unexpected "..."C4T=t.TypeVar("C4T", bound=CallableProtocol[..., t.Any]) # OK# All OKc0: t.Callable[..., t.Any]
c1: t.CallableAlias[..., t.Any]
c2: t.CallableProtocol[..., t.Any]
c3: CallableAlias[..., t.Any]
c4: CallableProtocol[..., t.Any]Expected Behavior
No errors emitted by mypy
Your Environment
- Mypy version used:
v1.3.0 - Mypy command-line flags: None
- Mypy configuration options from
mypy.ini (and other config files):
[mypy]strict = True
warn_unreachable = True
enable_error_code = redundant-expr, truthy-bool, ignore-without-code, unused-awaitable
disallow_untyped_calls = False
warn_unused_configs = True
- Python version used: 3.10
Bug Report & To Reproduce
False-positive-emitting behaviour of a type-alias of
typing.Callable[P, R]vs a callbackProtocol[P, R]is inconsistent whentyping.TypeVar(bound=...)expression;Source module
Module importing from source module
Expected Behavior
No errors emitted by mypy
Your Environment
v1.3.0mypy.ini(and other config files):