I have a python function that utilizes the python __pow__ (**) operator with two int's, Mypy tells me that the expression b ** e has type "Any", where b and e have type "int". I have tried to cast the expression to an int with int(b ** e), but I still get the error. How do I correctly type hint this expression?
Also, if the expression b ** e really does return type "Any", can you explain why?
Error:
temp.py:7: error: Expression has type "Any" [misc]
power: Callable[[int, int], int] = lambda b, e: b ** e
^
temp.py
from functools import reduce
from typing import Callable, Dict
def factorization_product(fact: Dict[int, int]) -> int:
'''returns the product which has the prime factorization of fact'''
power: Callable[[int, int], int] = lambda b, e: b ** e # error on expression "b ** e"
product: Callable[[int, int], int] = lambda x, y: x * y
return reduce(product, [power(b, e) for b, e in fact.items()], 1)
Edit:
I realized I can use the builtin pow and operator.mul instead of lambda's, but I still get the error.
Error:
temp.py:8: error: Expression has type "Any" [misc]
return reduce(mul, [pow(b, e) for b, e in fact.items()], 1)
^
revised temp.py
from functools import reduce
from operator import mul
from typing import Dict
def factorization_product(fact: Dict[int, int]) -> int:
'''returns the product which has the prime factorization of fact'''
return reduce(mul, [pow(b, e) for b, e in fact.items()], 1)