3 ms·
def concat_tuples(tuple1: Tuple[int, str, float], tuple2: Tuple[str, str, str]) -> Tuple[int, str, float, str, str, str]: return tuple1 + tuple2 Your se
by corethree 3y ago
def concat_tuples(tuple1: Tuple[int, str, float], tuple2: Tuple[str, str, str]) -> Tuple[int, str, float, str, str, str]:
return tuple1 + tuple2
Your second function you just broke out of the type system with Any. Give me a more exact, are you saying Any is a constrained type variable? The only possibility here is this:
T = TypeVar('T')
def transform(x: Dict[str, T]) -> Dict[str, Callable[[], T]]:
return {key: lambda : value for key, value in x.items()}
If you want that lambda to do something else you have to constrain T. Constraining T will give me more options in the definition:
from numbers import Number
def transform2(x: Dict[str, Number]) -> Dict[str, Callable[[Number], Number]]:
return {key: lambda y: value + y for key, value in x.items()}
def transform3(x: Dict[str, Number]) -> Dict[str, Callable[[], Number]]:
return {key: lambda: value + value for key, value in x.items()}
etc...
The thing is because these type checkers are external, anyone can add arbitrary features to them and extend it. Someone can make it to the level of proof checking... eliminating the need for testing in general. Of course the syntax has to support it too.
- Timon3 3y ago> concat_tuples I specifically talked about arbitrary tuples. Your function only handles specific tuples. > Your second function you just broke out of the type system with Any. Give me a more exact, are you saying Any is a constrained type variable? I'm not sure what you're trying to say here. Yes, you can implement the function, I never claimed otherwise. I'm talking about the _type system_. > The thing is because these type checkers are external, anyone can add arbitrary features to them and extend it. The type system is a core feature of Python. Until it supports what I want, I can't really use what I want with Python.
- corethree 3y ago>I'm not sure what you're trying to say here. Yes, you can implement the function, I never claimed otherwise. I'm talking about the _type system_. The type constrains the definition. Look at the definition, there is ONLY one possible definition when converting this: Dict[str, T] -> Dict[str, Callable[[], T]] Literally. Try to think of another way to define the lambda, you can't. The more open the type the more constrained the definition the more constrained the type the more open the definition. > I specifically talked about arbitrary tuples. Your function only handles specific tuples. I remarked on this in other replies. How will you define the return value of the concat and have it exactly type correct? It's not possible bro. You can't do much with tuples of arbitrary types. Not unless you have dependent types. >The type system is a core feature of Python. Until it supports what I want, I can't really use what I want with Python. Type checking in python is an external application. The interpreter doesn't do any type check. Type checking is achieved by running another program. All kinds of additional features and custom interpretations on types can be inserted into these type checkers.
- Timon3 3y ago> The type constrains the definition. Look at the definition, there is ONLY one possible definition when converting this: Yes, and if Python had a better type system, I could write a more concrete definition. Why don't you understand that? > I remarked on this in other replies. How will you define the return value of the concat and have it exactly type correct? It's not possible bro. You can't do much with tuples of arbitrary types. Not unless you have dependent types. Of course it's possible, the type system even supports almost everything that's necessary. You can already define what I want with a single fixed-size tuple of arbitrary size, you just can't do it with multiple ones. Why do you have this strange fixation on calling this "wrong" and "impossible"?
- corethree 3y ago>Yes, and if Python had a better type system, I could write a more concrete definition. Why don't you understand that? I understand everything. It's you who doesn't understand. What I described is for ALL type systems in general. Not python types. >Of course it's possible, the type system even supports almost everything that's necessary. You can already define what I want with a single fixed-size tuple of arbitrary size, you just can't do it with multiple ones. Why do you have this strange fixation on calling this "wrong" and "impossible"? It's not a fixation. It's because I know what I'm talking about. Look just prove it to me. Define the type signature for concat function that will concat two tuples with variadic parameters in TS. Make sure the return value has the exact required shape. Additionally don't resort to using Any. Also define the intersection function as well. Again, Don't use any. Should be trivial if what you say is true.
- Timon3 3y ago> I understand everything. It's you who doesn't understand. What I described is for ALL type systems in general. Not python types. Then why is what you're claiming to be impossible possible in Typescript? How can that be? Is the whole world wrong, aside from you? > Look just prove it to me. Define the type signature for concat function that will concat two tuples with variadic parameters in TS. You mean the one I already posted yesterday? https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDaA7ArgWwEYQE4BooBnYXASxQHMBdAKFEigCEFksB7dgGwgEMVC6bHjq0AxuxQkovAFzIhOAsVIUarJABYATIQBEACwhcu7KAHd2uLgBM9dCVOBQs8pB258BURSI0AzXi4iCEIABlF-NBQxYDJJKH9OAB4AFV4oCAAPYAgUGyJkADoS-hAkampCVKxMnLyC4tKUcsqAPgAKOSh0wlcerABKNxKi3qhRmuooAG9aKChcCGA0XBQmot5CUaw6AF9acUlpMVYk9i6+waOpTyLTSg6xQaA https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDaA... Or, if you prefer Python, run this: from typing import Tuple, TypeVar, TypeVarTuple, reveal_type T = TypeVar("T") Ts = TypeVarTuple("Ts") def concat(element: T, tup: Tuple[*Ts]) -> Tuple[T, *Ts]: return (element, *tup) element = "foo" tup = (True, 42) reveal_type(concat(element, tup)) You said it's impossible for the return type to be dependent on the input type. Why does Python disagree with you?