Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14185

How can I infer return type for object based on parameter?

$
0
0

Let's say we have a function

def get_attr_wrapper(obj: object, attr: str) -> ???:    return getattr(obj, attr)

How can I infer the return type of get_attr_wrapper based on the parameters given?

Maybe with a generic somehow?

For example, if I passed in

from dataclasses import dataclass@dataclassclass Foo:    bar: strfoo = Foo(bar="baz")rv = get_attr_wrapper(foo, "bar")

rv would be inferred by Python's type checker as being of type string.


Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>