Perhaps you could solve this with type variables. Here's a little demonstration program: ``` from decimal import Decimal from typing import TypeVar F = TypeVar('F', float, Decimal) def add(a: F, b: F) -> F: return a+b print(add(4.2, 3.14)) print(add(Decimal('4.2'), Decimal('3.14'))) print(add(Decimal('4.2'), 3.14)) ``` Note that the last line is invalid. mypy correctly finds this: ``` flt.py:8: error: Type argument 1 of "add" has incompatible value "object" ``` (We could work on the error message.)
Now, I'm not sure that this is the best solution given the audience -- but the type variable 'F' only needs to be defined once, so this might actually work. -- --Guido van Rossum (python.org/~guido)
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com