We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Bug Report
Function overloads that take type as argument does not match Protocol subclass objects.
This bug prevents use of Protocol classes with DI container like python-inject. https://github.com/ivankorobkov/python-inject/blob/05ad3e87bb179ce56cabb272bd5f75cca2880f7c/inject/__init__.py#L394-L402
To Reproduce
from abc import ABCMeta, abstractmethod from typing import Any, cast, NewType, overload, Protocol, runtime_checkable, TypeVar from typing_extensions import assert_type T = TypeVar( "T") @overload def get_instance(t: type[T]) -> T: ... @overload def get_instance(t: Any) -> object: ... def get_instance(t): return cast(Any, object()) # dummy (e.g. should implement a DI container) # normal class class C: def foo(self) -> str: return '' assert_type(C, type[C]) # abstract class class A(metaclass=ABCMeta): def foo(self) -> str: return '' assert_type(A, type[A]) # pure abstract class class PA(metaclass=ABCMeta): @abstractmethod def foo(self) -> str: ... assert_type(PA, type[PA]) # Protocol type class P(Protocol): def foo(self) -> str: ... assert_type(P, type[P]) # runtime-checkable Protocol type @runtime_checkable class RP(Protocol): def foo(self) -> str: ... assert_type(RP, type[RP]) # NewType NT = NewType("NT", PA) assert_type(NT, type[NT]) c = get_instance(C) assert_type(c, C) a = get_instance(A) assert_type(a, A) pa = get_instance(PA) assert_type(pa, PA) # error: Expression is of type "object", not "PA" [assert-type] p = get_instance(P) assert_type(p, P) # error: Expression is of type "object", not "P" [assert-type] rp = get_instance(RP) assert_type(rp, RP) # error: Expression is of type "object", not "RP" [assert-type] nt = get_instance(NT) assert_type(nt, NT)
Expected Behavior
There should be no type checking error. Protocol class P should match type[P] in overloads.
P
type[P]
Actual Behavior
Type checking errors are raised for Protocol types. Protocol class P does match type[P] in overloads.
Your Environment
mypy.ini
The text was updated successfully, but these errors were encountered:
I've learnt this is actually as spec'ed in PEP 544. https://peps.python.org/pep-0544/#type-and-class-objects-vs-protocols
However, some inconsistencies remain problematic. I will create new issues for them.
Sorry, something went wrong.
assert_type
No branches or pull requests
Bug Report
Function overloads that take type as argument does not match Protocol subclass objects.
This bug prevents use of Protocol classes with DI container like python-inject.
https://github.com/ivankorobkov/python-inject/blob/05ad3e87bb179ce56cabb272bd5f75cca2880f7c/inject/__init__.py#L394-L402
To Reproduce
Expected Behavior
There should be no type checking error.
Protocol class
P
should matchtype[P]
in overloads.Actual Behavior
Type checking errors are raised for Protocol types.
Protocol class
P
does matchtype[P]
in overloads.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: