Relaxing usage of `type` with `typing.Protocol` in typing spec
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Quiet
- Tech stack
- python
- Domain
- documentation
Research direction
Start with the typing specification discussion and the linked Python Discourse thread, then compare the behavior described for mypy, pyright, and ty. Review mypy#4717 for related examples. Done means reaching agreement on whether type[P] may accept non-instantiable Protocol types and documenting the resulting rule.
Written by the indexing model from the issue text.
Description
Some time ago I opened a thread on the Python discourse about implementing an AbstractType semantic that would allow functions to receive non-instantiable types so to try and conform a bit the situation presented in the the thread.
Consider the snippet:
from abc import abstractmethod
from typing import runtime_checkable, Protocol, TypeVar, TypeGuard
@runtime_checkable
class MyProtocol(Protocol):
@abstractmethod
def my_method(self) -> str:
...
class MyClass:
def my_method(self) -> str:
return "Hello from MyClass!"
class MyOtherClass:
def another_method(self) -> str:
return "Hello from MyOtherClass!"
P = TypeVar('P', bound=MyProtocol)
def check_protocol(obj: object, protocol: type[P]) -> TypeGuard[P]:
return isinstance(obj, protocol)
# Example usage
my_obj = MyClass()
my_other_obj = MyOtherClass()
if check_protocol(my_obj, MyProtocol):
print(my_obj.my_method())
if check_protocol(my_other_obj, MyProtocol):
print(my_other_obj.my_method())
else:
print("Object does not implement MyProtocol.")
Output:
Hello from MyClass!
my_obj does not implement MyOtherClass protocol.
At runtime, this works as intended. At type checking time, mypy complains that check_protocol cannot accept a non-instantiable type, while pyright and ty are accepting this behavior.
According to the typing specification, mypy is technically correct (the best kind of correct), but this code is perfectly acceptable. There are other examples referenced in python/mypy#4717 (which is the 3rd most upvoted issue by 👍 by the way).
I was hoping for more discussion points presented in the thread but not many replied. I didn't want to monopolize it, so I'm trying my luck here.
@JelleZijlstra (sorry for the ping) did point out that it might be worthwhile to change the spec to allow this behavior but I'm guessing there should be some discussion on how to deal with it first. Jelle suggested: "If you want it to be instantiable, use Callable[] instead."
I do agree it makes sense, but this snippet:
from typing import Protocol
class MyProtocol(Protocol):
def method(self) -> str:
...
print(callable(MyProtocol)) # prints True
confuses me.
So my point is: could the spec allow for type to accept non-instantiable types, since it seems to be a pretty common pattern? Is there a way to effectively deal with the separation between instantiable and non-instantiable types?
I tried adding the "typing spec" label but couldn't. At any rate, thank you for the consideration.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·