Trouting (short for Type Routing) is a simple class decorator that allows to define multiple interfaces for a method that behave differently depending on input types.
Project Links
Meta
Author: Luca Soldaini
Requires Python: >=3.8
Classifiers
Development Status
- 5 - Production/Stable
License
- OSI Approved :: Apache Software License
Operating System
- OS Independent
Programming Language
- Python :: 3
Topic
- Software Development :: Libraries :: Python Modules
- Utilities
Typing
- Typed
Trouting

Trouting (short for Type Routing) is a simple class decorator that allows to define multiple interfaces for a method that behave differently depending on input types.
To install for PyPI trouting, run:
pip install trouting
The logo of trouting was generated using Stable Diffusion with prompt "A Kandinsky painting titled The Trout Who Routes" and slightly edited by the author.
Example
Imagine you want to define a class whose method behaves differently depending on whether the input is a string or an integer. You can do this with trouting as follows:
from typing import Any, Union
from trouting import trouting
class MyClass:
@trouting
def add_one(self, a: Any) -> Any:
# fallback method
raise TypeError(f"Type {type(a)} not supported")
@add_one.add_interface(a=(int, float))
def add_one_int(self, a: Union[int, float]) -> float:
# a is an int or float
return float(a + 1)
@add_one.add_interface(a=str)
def add_one_str(self, a: str) -> str:
# a is a str
return a + "1"
Now, when using MyClass, the method add_one will behave differently depending on the input type:
my_class = MyClass()
my_class.add_one(1) # returns 2.0
my_class.add_one("1") # returns "11"
my_class.add_one([1]) # raises TypeError
0.3.3
Jan 17, 2023
0.3.2
Oct 17, 2022
0.3.1
Oct 14, 2022
0.3.0
Sep 08, 2022
0.2.2
Sep 05, 2022
0.2.1
Sep 05, 2022
0.2.0
Sep 04, 2022
0.1.4
Sep 04, 2022
0.1.3
Sep 04, 2022
0.1.2
Sep 03, 2022
0.1.1
Sep 03, 2022
0.1.0
Sep 03, 2022