-
Notifications
You must be signed in to change notification settings - Fork 258
Disallow TypeVar with only one constraint #82
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
Could it be used like type synonyms in Haskell? Sure, it's a Foo, but you're communicating specifically what kind of Foo it it is. type Address = [Char]
type Message = [Char]
send_email :: Address -> Message -> IO () which is far more informative than: send_email :: [Char] -> [Char] -> IO () Yes, technically it's just two strings, but we're getting more information out of the type signature than "just input strings". |
For that we have already type aliases
|
…t paragraph normative. Fixes issue #82.
I'm not sure I understand this. What if I want to constrain a method such that two parameters use the exact same type, not just compatible types? T = TypeVar('T', Model)
def bulk_create(model: Type[T], instances: Sequence[T]):
pass How could I do this without a single-constraint TypeVar? |
How about def bulk_create(model: Type[Model], instances: Sequence[Model]):
pass |
Oh, I see, maybe you actually wanted |
Ah, yes, that's exactly what I wanted. My apologies for missing that. Thank you! :) |
For my understanding, is |
No, these are totally different things. Upper bound is what it says -- an upper bound. While type variable with value restrictions can take only exactly a restriction. For example (read PEP 484) passing a subtype of |
I see, thanks. Here's an example for future reference: from typing import TypeVar
from typing import Union
class MyInt(int):
pass
T = TypeVar("T", int, str)
U = TypeVar("U", bound=Union[int, str])
def takes_t(x: T) -> T:
return x
def takes_u(x: U) -> U:
return x
a: int = takes_t(42)
b: int = takes_u(42)
c: str = takes_t("hi")
d: str = takes_u("hi")
e: MyInt = takes_t(MyInt()) # Does not typecheck: takes_t returns int
f: MyInt = takes_u(MyInt()) # Typechecks: takes_u returns MyInt |
Forget it - it was a typo (the class should not derive from the typevar). |
Hi, I just met a situation that may need a TypeVar with only one constraint: from enum import Enum
from typing import Type
StateEnum = TypeVar("StateEnum", Enum)
class StateMachine:
def __init__(self, state_enum: Type[StateEnum], initial_state: StateEnum): ... Here I want to annotate that the |
@Magine, you should use |
Types like
TypeVar('T', Foo)
don't make sense since you can always replace them with justFoo
. Although type vars with the only constraint don't break anything, they look really confusing and send the wrong message about usingTypeVar
. Issue #39 is a good example of this problem.The text was updated successfully, but these errors were encountered: