Python Forum
Type hinting - return type based on parameter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type hinting - return type based on parameter
#1
I've had the pleasure of writing some Python at work recently, but I was stumped by something. I want something like this:
def get_from_dict(d: dict, key: str, value_type = int) -> type(value_type): value = d.get(key) if value is None: raise RuntimeError(f"Tried to fetch {key} from dictionary {d} but dictionary did not have the key.") return value_type(value)
My workaround was to hard-code it as int because that's my use case, but while I was writing the code, I wanted to make it more generic. I doubt this is possible, at least in the current state of Python's type hinting, but does anyone happen to have any ideas?
Reply
#2
You always can define a custom decorator, e.g.

import inspect from functools import wraps from typing import Any def set_rettype_as_var(variable_name): def decorator(f): @wraps(f) def wrapper(*args, **kwargs_inner): return f(*args, **kwargs_inner) desired_type = Any signature = inspect.getfullargspec(f) if signature.kwonlydefaults and variable_name in signature.kwonlydefaults: desired_type = type(signature.kwonlydefaults.get(variable_name)) # probably, you can take into account signature.annotations ... if signature.args and variable_name in signature.args: if signature.defaults is not None: desired_type = type(signature.defaults[signature.args.index(variable_name)]) else: desired_type = signature.annotations.get(variable_name, Any) # maybe you will need to define some other conditions .... wrapper.__annotations__['return'] = desired_type return wrapper return decorator
@set_rettype_as_var('x') def a(x:float): pass
a.__annotations__
Error:
{'x': float, 'return': float}
Reply
#3
That's not quite what I'm looking for. The return type is specified at function definition, when I want it to depend on the type of the value of the argument, not a fix parameter type.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python class members based on a type value voidtrance 7 2,441 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  type object 'man' has no attribute 'centerX' Tempo 7 2,253 Mar-07-2025, 03:47 AM
Last Post: deanhystad
Question how to type hint a function in a dataclass? Calab 3 1,832 Feb-27-2025, 04:40 AM
Last Post: Calab
  help with a script that adds docstrings and type hints to other scripts rickbunk 1 1,847 Feb-24-2025, 05:12 AM
Last Post: from1991
  Changing client.get() method type based on size of data... dl0dth 1 1,270 Jan-02-2025, 08:30 PM
Last Post: dl0dth
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 6,082 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  A question on Boolean Data Type Hudjefa 5 2,001 Aug-13-2024, 11:03 AM
Last Post: Hudjefa
  Precision with Decimal Type charlesrkiss 10 4,482 Aug-06-2024, 10:07 AM
Last Post: calculadora03
  How can I create this type hint. deanhystad 0 974 Aug-05-2024, 07:55 PM
Last Post: deanhystad
  How to type *_ deanhystad 0 883 Jun-12-2024, 10:22 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.