-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Analysis time binding of restricted Generic
s
#11565
Comments
The problem is that For your def read(msg: Message[AnyStr]) -> None:
msg.content.method_in_neither_str_nor_bytes() which correctly shows errors saying that neither
|
I am aware of this, so I realise I should give a better example. I should have typed it out properly, but my thinking behind the typing I used was to imply that the correct type is hard to infer statically. Say we are rather doing this, or something similar: m3: Message = Message(header="foo", content="bar") if random.random() < 0.5 else Message(header=b"foo", content=b"bar")
reveal_type(m1) # Message[Any]... If the stochasticity is wrapped in a function call, I realise I can annotate the function with return type Here is yet another common case - and perhaps a better example - that shows up in the module scope, where I don't have the luxury of binding the type by help of the # Verbatim
MessageDict: TypeAlias = dict[Hashable, Message] What I really am trying to express, with less verbosity, is this: # Desired interpretation
MessageDict: TypeAlias = dict[Hashable, Union[Message[str], Message[bytes]]] but what I end up with is, as you point out: # Actual interpretation
MessageDict: TypeAlias = dict[Hashable, Message[Any]] Given what you propose for my function example, it might be reasonable to look for a solution involving MessageDict: TypeAlias = dict[Hashable, Message[AnyStr]] This just results in a new def dict_consumer1(md: MessageDict[AnyStr]) -> None:
reveal_type(md) # Union[Dict[Hashable, Message[str], Dict[Hashable, Message[bytes]] Hence, this is a case where I really have no choice but to spell out the
You are right that this isn't too bad of a solution in this case, but it still is more verbose than it needs to be. I'd prefer not having to import
I'm already using It is precisely because of the poisonousness of |
Generic
sGeneric
s
Feature
A generic type defined with reference to a restricted type variable only makes sense when containing a value of type within the restrictions of the type variable.
It would then make sense to assume that when the user does not bind the generic type (that is, they do not provide a type argument), the implied type is the
Union
of the possible bound types.To Reproduce
Pitch
Say I have a function that accepts any valid
Message
as an arg:If I annotate it like I just did, I get no hand-holding from the type checker.
msg.content
is understood asAny
, so everything goes. We know, however, that this has to be one ofAnyStr
. If I want type checker assistance, I now have to manually list the possibleMessage
types, like so:This is fine when the restriction consists of two types, but it should be easy to imagine examples with a more verbose restriction.
The text was updated successfully, but these errors were encountered: