-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
random.Random.getvalue
and random.Random.setvalue
are unduly opinionated about their types and ill-suited for subclassing
#6063
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
Seems like another use case for defaulted generics. |
For now, the best we could do is to use |
How would others define an interface with an opaque state that could be emitted/passed back? Or is that the point of "defaulted generics" and you want to preserve specific type checking for the standard library implementation until that arrives? Just curious, but if the state is supposed to be opaque (I'm assuming even for the standard library), why preserve type checking around implementation details that leave and return via |
An opaque state could be defined in several ways: Either using the concrete type as done here. But a better way would probably be to use |
Right, so why not strive to keep implementation typing separate from interface typing? from collections.abc import Mapping as MappingC
from typing import Dict, Mapping, NewType, Protocol
_OpaqueT = NewType("_OpaqueT", object)
class Foo(Protocol):
def getblob(self) -> _OpaqueT:
...
def setblob(self, blob: _OpaqueT) -> None:
...
class FooImpl(Foo):
def __init__(self):
self._blob: Dict = {}
@property
def foo_impl_blob(self) -> Mapping:
return self._blob
def getblob(self) -> _OpaqueT:
return _OpaqueT(self._blob)
def setblob(self, blob: _OpaqueT) -> None:
assert isinstance(blob, MappingC)
self._blob = dict(blob)
foo = FooImpl()
blob: _OpaqueT = foo.getblob()
assert "missing" not in blob # Unsupported right operand type for in ("_OpaqueT") 🙌 🎉
foo.setblob(blob)
assert "missing" not in foo.foo_impl_blob # totally cool Or are you saying that the current implementation of |
As this is not something we can express in typeshed right now, but could be improved if typing improves, I've marked this as inexpressable and am closing this per our policy. This can be revisited if typing improves. |
If i understand the docs correctly, this …
… strongly suggests that the return value of
getstate()
(and corresponding argument tosetstate()
) should be opaque and treated as implementation specific. However, typeshed seems to want to enforce that they should beTuple
s.This leads to warnings for subclasses who, e.g., capture state as
bytes
or adict
. E.G.:(Not sure what that second warning is or where it came from.)
The text was updated successfully, but these errors were encountered: