Answer by Patrick Haugh for Python type hint for classes that support...
If you're willing to install a not-quite-offical extension to typing, typing-extensions, you can use a Protocol, which should be an implementation of PEP-0544: from typing_extensions import Protocol...
View ArticleAnswer by Felk for Python type hint for classes that support __getitem__
It sounds like you essentially want to define your own abstract base class (abc). Following the documentation above, you can define a custom abc that only dictates the presence of __getitem__, but...
View ArticleAnswer by Liran Funaro for Python type hint for classes that support __getitem__
This will work for dict and list, but not for any generic type: from typing import Any, Mapping, Sequence, Union def my_function(hasitems: Union[Mapping, Sequence], locator: Any) -> Any: return...
View ArticlePython type hint for classes that support __getitem__
I want to add type hints to a function that will accept any object with a __getitem__ method. For instance, in def my_function(hasitems, locator): hasitems[locator] I don't want to restrict hasitems...
View Article