This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
To clarify, I'm after something like the following. Let's say I have a function that takes some arbitrary inputs and provides some arbitrary output. It's lightweight and type-stable, so that the return type is predictable from the input types, but there are no other restrictions.
I want to be able to call the function with type objects to infer the return type given the input types. Obviously I can't do that with raw python type objects:
def my_func(a, b):
return a b
inferred_return = my_func(int, int) # fail
But this would work:
class MyInt:
pytype = int
def __add__(self, other):
if isinstance(other, MyInt):
return MyInt()
return NotImplemented
inferred_return = my_func(MyInt(), MyInt()).pytype # == int
I could make such wrappers for all the types I'm interested in and do this the manual way. It wouldn't be hard, just tedious, and I'm likely to be hunting down edge cases for a while. Is there a library that provides such type wrappers?
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Python/comm...