Rewrite `operator` Library with More Specific Annotations
More detailed operator annotations could be very useful (see #6448).
For example,
def polynomial_derivative(coefficients: Sequence[float]) -> list[float] / list[str]:
"""Compute the first derivative of a polynomial.
f(x) = x³ -4x² -17x + 60
f'(x) = 3x² -8x -17
"""
# polynomial_derivative([1, -4, -17, 60]) -> [3, -8, -17]
n = len(coefficients)
powers = reversed(range(1, n))
return list(map(operator.mul, coefficients, powers))
This used to type check with either return type, but should only work with list[float] now. I haven't done all the annotations as I wanted to test a proof of concept.
This seems useful. In fact, I don't know why we're using Any at all here.
I ran the mypy primer manually, and there are no changes. For some reason, mypy and GitHub actions don't go well together for larger projects.
This is almost ready. I'm unsure whether to delete the fallback overload, as they make the type checking worse. If I remove them, these will no longer be backwards compatible though.
I don't know why the CI is failing, but I've had similar issues with mypy in the past.
Another thing to note is that for the overloads, the interpreter may run an earlier version (eg __add__ over __radd__) and cause an error when the types don't match. This is different from the type-checker's interpretation.
I've managed to reproduce some of the failures locally, and they seem to be due to memory limits. It's crashing on large repos: pandas and homeassistant