-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Allow using modules as subtypes of protocols #5018
New issue
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
Comments
SGTM.
|
(As long as you add it to the PEP -- preferably as a separate PR.)
|
Sounds good. We should perhaps explicitly specify how module-level functions would work, since they don't take a # m.py
def f(x: int) -> None: pass
# main.py
import m
from typing import Protocol
class P(Protocol):
def f(self, x: int) -> None: ...
p: P = m # Should be ok? |
@JukkaL Yes, I think just dropping the |
Another use case would be interchanging the This is a nice way to make functions that either use the global RNG state or support overloading with explicit objects, e.g., import random
from typing import Protocol
class Random(Protocol):
... # all methods from random.Random
def random_add(rng: Random = random):
return rng.rand() + rng.rand() |
Yes, I think everyone is sold on this (there is already a PR to add this to the PEP 544), but we just didn't have time to implement this (even though it is pretty straightforward). |
See python/mypy#5018 for motivation.
looks like the PEP was merged two years ago, but this is still an open issue. Is there any progress on this? |
Please don't do this. You can see there's been no progress. Nagging only makes the (unpaid, volunteer) maintainers feel stressed and underappreciated. If you really need a feature, the best thing you can do is to dig into the codebase and open a PR. |
I understand the etiquette, but from looking at the messages its unclear what the current state is - conversation seems to have just stalled. Its also labeled 'needs discussion'. @ilevkivskyi mentioned its straight forward work, what needs to be done? is it something a newbie can do? |
Hm, I think this is not the best first issue. |
sorry for lecturing 😅 |
Removed the "needs discussion" label since we would definitely want to have this. However, the design of the implementation will still need some discussion :-) |
pyright seems to have implemented this feature. |
The class and its methods are only implemented to work around a bug in mypy: python/mypy#5018
Is there at least a workaround for this? It seems even classes (the classes itself not the instances!) can't be tested against protocols: from typing import Protocol
class AProtocol(Protocol):
a: str
class A:
a = "a"
a_ex: AProtocol = A results in
Or is there no way at the moment with mypy to check protocols for "non-instantiated" objects? I need this functionality to check some static generated code. |
@theCapypara your issue is unrelated. Do
|
Fixes #5018 Fixes #5439 Fixes #10850 The implementation is simple but not the most beautiful one. I simply add a new slot to the `Instance` class that represents content of the module. This new attribute is short lived (it is not serialized, and not even stored on variables etc., because we erase it in `copy_modified()`). We don't need to store it, because all the information we need is already available in `MypyFile` node. We just need the new attribute to communicate between the checker and `subtypes.py`. Other possible alternatives like introducing new dedicated `ModuleType`, or passing the symbol tables to `subtypes.py` both look way to complicated. Another argument in favor of this new slot is it could be useful for other things, like `hasattr()` support and ad hoc callable attributes (btw I am already working on the former). Note there is one important limitation: since we don't store the module information, we can't support module objects stored in nested positions, like `self.mods = (foo, bar)` and then `accepts_protocol(self.mods[0])`. We only support variables (name expressions) and direct instance, class, or module attributes (see tests). I think this will cover 99% of possible use-cases.
There is an idea to allow modules be accepted where a protocol is expected. This pattern is sometimes used for configs and option management, for example:
This will allow better typing than just
types.ModuleType
and should be straightforward to implement. Are there any objections against this?The text was updated successfully, but these errors were encountered: