You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Verification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which inherits also passed parameter to that function, a proper verification shall be done.
Pitch
In case of overloaded functions with type hint e.g. validate_enum_list(key: str, value: str, allowed_values: Iterable[str]) and validate_enum_list(key: str, value: Iterable[str], allowed_values: Iterable[Iterable[str]]) it is not possible without additional implementation of own abstract base class MyIterable to differentiate str from Iterable[str].
I have seen #11001 but it is still open without any clear resolution. It also mentions python/typing#256 and #5090
Use Cases
fromtypingimportAny, Iterable, TypeVar, List, ContainerfrommultimethodimportmultimethodfromtypesimportGenericAliasT=TypeVar("T")
classMyIterable(Iterable[T]):
@classmethoddef__subclasshook__(cls, subclass: type) ->bool:
returnnotissubclass(subclass, str)
def__class_getitem__(cls, key: Any) ->GenericAlias:
returnGenericAlias(cls, key)
@multimethoddefvalidate_enum_list(key: str, value: str, allowed_values: MyIterable[str]) ->None:
print("key: str, value: str, allowed_values: MyIterable[str]")
print(type(value))
print(type(allowed_values))
@validate_enum_list.registerdef_(key: str, value: MyIterable[str], allowed_values: MyIterable[MyIterable[str]]) ->None:
print("key: str, value: MyIterable[str], allowed_values: MyIterable[MyIterable[str]]")
print(type(value))
print(type(allowed_values))
value=sorted(value) # this causes mypy errorallowed_values=sorted([sorted(item) foriteminallowed_values]) # this causes mypy errorvalidate_enum_list("test1", "test1", ["test1"])
validate_enum_list("test2", ["test2"], [["test1"]])
#validate_enum_list("test1", "test1", [["test1"]]) # uncommenting this will cause runtime exception which is GOOD, but no mypy error which is also WRONGdefget_enum_str(key: str, value: str, allowed_values: MyIterable[str]) ->None:
validate_enum_list(key, value, allowed_values)
print("get_enum_str")
defget_enum_str_list(key: str, value: MyIterable[str], allowed_values: MyIterable[MyIterable[str]]) ->None:
validate_enum_list(key, value, allowed_values)
get_enum_str("test1", "test1", ["test1"])
get_enum_str_list("test2", ["test2"], [["test2"]])
get_enum_str_list("test2", "test2", [["test2"]]) # this causes mypy error
As commented in the code, line validate_enum_list("test1", "test1", [["test1"]]) does not return an error from mypy, but the same issue is with singledispatch decorator which I have already found is a bug.
P.S. I have found some additional informations regardless class_getitem() which is supposed to handle type hinting in such cases, but It is also mentioned that:
Custom implementations of __class_getitem__() on classes defined outside of the standard library may not be understood by third-party type-checkers such as mypy
So for now I'm confused as to whether it should be a feature request or a bug...
The text was updated successfully, but these errors were encountered:
Dvergatal
changed the title
Allow mypy to verify if passed object suits to the typehint of an abstract base class
Allow mypy to verify if passed object suits to the type hint of an abstract base class
Jan 27, 2023
Mypy appears to be doing the right thing here. The code is attempting to assign a value back to value that violates its declared type. The same is true for allowed_values. And the other errors are all legitimate. Mypy and pyright are in agreement on all of the errors in the file. Recommend closing.
Feature
Verification of inheritance with an abstract base classes, meaning that if a variable in method is of type hint of an abstract base class derived from another abstract base class from which inherits also passed parameter to that function, a proper verification shall be done.
Pitch
In case of overloaded functions with type hint e.g.
validate_enum_list(key: str, value: str, allowed_values: Iterable[str])
andvalidate_enum_list(key: str, value: Iterable[str], allowed_values: Iterable[Iterable[str]])
it is not possible without additional implementation of own abstract base classMyIterable
to differentiatestr
from Iterable[str].I have seen #11001 but it is still open without any clear resolution. It also mentions python/typing#256 and #5090
Use Cases
As commented in the code, line
validate_enum_list("test1", "test1", [["test1"]])
does not return an error from mypy, but the same issue is withsingledispatch
decorator which I have already found is a bug.P.S. I have found some additional informations regardless class_getitem() which is supposed to handle type hinting in such cases, but It is also mentioned that:
So for now I'm confused as to whether it should be a feature request or a bug...
The text was updated successfully, but these errors were encountered: