3 ms·
Or maybe it does: https://github.com/breuleux/ovld/blob/f254dcc6215d50d5f9c3d1c033bfc80b6968b413/src/ovld/__init__.py#L59 https://github.com/breuleux/ovld/blob/
by olejorgenb 1y ago
Or maybe it does: https://github.com/breuleux/ovld/blob/f254dcc6215d50d5f9c3d1c033bfc80b6968b413/src/ovld/__init__.py#L59 https://github.com/breuleux/ovld/blob/f254dcc6215d50d5f9c3d1...
- mpeg 1y agoThat's both genius and a massive hack, love it. For those who are wondering: they alias @typing.overload as @ovld only for type checkers. Normally, @typing.overload is a way to define multiple function signatures with empty bodies so your type checker can work nicely with functions that for example only return a certain type when called with a certain signature, but the function implementation is left to you and usually involves a bunch of runtime type checks to branch your logic the right way
- paddy_m 1y agoWhat is the return type of an overloaded function? I don't see that in any of the examples. I think it would make sense to have each function return either the exact same type, or a generic. @ovld(int) #all of these overloads should return an int len(a:list) -> int len(a:str) -> int #the effective type of len is len(a:Union[int, str]) -> int and then a generic example T = TypeVar('T') @ovld(T) add(a:np.int8, b:np.int8) -> np.int8 @ovld(T) add(a:np.int16, b:np.int16) -> np.int16 In the second example, I don't know how you would connect the input params to the generic T. If you put concrete types in, I see how ovld works. Am I missing something?
- breuleux 1y agoIt works, ish. Type checkers have a rule that you must have a non-stub, non-decorated overload, so if using ty for example, you need to --ignore invalid-overload. mypy doesn't seem to recognize the hack, although I could've sworn it did? Anyway, it'd be nice if we could mark a decorator as functioning like @overload but without the need for stubs.