How do I ensure that function arguments must be of the same type? #10801
Unanswered
ashenstrata
asked this question in
Q&A
Replies: 1 comment
-
I don't think this is possible. If you turn the function into a method of a generic class, you can enforce that both arguments are of the same type, but you also have to specify the type: V = TypeVar("V")
class MyClass(Generic[V]):
@staticmethod
def some_method(foo: V, bar: V) -> V: ...
res = MyClass[int].some_method(1, "s") # this is now a type error With the proposed PEP for subscriptable functions, this would probably also work. res = some_function[int](1, "s") # should be a type error |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want to ensure on a type checker level that 2 arguments of a function must always be the same exact type.
I expect an error to be shown when calling the function with arguments of different types:
However, it doesn't work. The V type just gets broader (
int | str
) and there is no error.As I understand, this is intended behavior. But how can I achieve what I need?
Beta Was this translation helpful? Give feedback.
All reactions