Skip to content

[_operator] Remove Anys that can be improved#15598

Merged
srittau merged 6 commits intopython:mainfrom
srittau:_operator-any
Apr 2, 2026
Merged

[_operator] Remove Anys that can be improved#15598
srittau merged 6 commits intopython:mainfrom
srittau:_operator-any

Conversation

@srittau
Copy link
Copy Markdown
Collaborator

@srittau srittau commented Apr 2, 2026

No description provided.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, though I haven't checked the primer report

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/chain/ethereum/oracles/uniswap.py:326: error: Argument 1 to "reduce" has incompatible type overloaded function; expected "Callable[[int, FVal], int]"  [arg-type]

sympy (https://github.com/sympy/sympy)
+ sympy/polys/matrices/dense.py:104: error: Incompatible types in assignment (expression has type "RingElement", target has type "R")  [assignment]

jax (https://github.com/google/jax)
+ jax/experimental/mosaic/gpu/dialect_lowering.py:1365: error: Argument "impl" to "_binary_op_lowering_rule" has incompatible type "function"; expected "Callable[[FragmentedArray, FragmentedArray], FragmentedArray]"  [arg-type]
+ jax/_src/pallas/mosaic_gpu/lowering.py:1088: error: Argument "grid" to "_lower_as_gpu_kernel" has incompatible type "tuple[int, ...]"; expected "tuple[int, int, int]"  [arg-type]

@srittau
Copy link
Copy Markdown
Collaborator Author

srittau commented Apr 2, 2026

Looking at the primer output:

rotki: Some reduce/@overload-related weirdness. The line in question:

        price = FVal(reduce(mul, [item.price for item in prices_and_tokens], 1))

item.price is an FVal, which means that reduce's first overload should match:

@overload
def reduce(function: Callable[[_T, _S], _T], iterable: Iterable[_S], initial: _T, /) -> _T: ...

That is consistent with the error message, which expects map to have the signature Callable[[int, FVal], int]. map's first overload doesn't match, as int.__mul__ expects an int as parameter. But the second overload should match with _T_contra = int | FVal and _T_co = FVal.

I blame some kind of mypy bug.

sympy: Unclear problem. The line in question:

            ai[j] = sum(map(mul, bi, cTj), ai[j])

bi and ai[j] both have type R, a type var bound to the protocol RingElement, which has this method:

    def __mul__(self, other: Self | int, /) -> Self: ...

So, according to reveal_type map(mul, bi, cTj) is inferred as map[Any] (which is strange to me, it should be inferred as map[R]). sum(map(mul, bi, cTj), ai[j]) is inferred as Any | R. So I have no idea where the conrete RingElement in the error messages comes from.

I (again) blame some kind of mypy bug.

jax #1: Type is inferred from a list of different functions. Previously, due to arguments being Any, it seems that mypy (kind of incorrectly) inferred the correct types from some of jax's own functions. Probably needs explicit annotations for this complicated situation.

jax #2: Previously tuple[Any, ...] was (imprecisely) inferred, now tuple[int, ...] is (precisely) inferred. Unfortunately while the former was compatible with tuple[int, int, int], the latter isn't. This is actually kind of a former false positive (from a type checking perspective).

Overall I'm not too worried about the hits, looks to be either mypy bugs or complicated situations that were previously not flagged due to (imprecisely) inferred Anys.

@srittau srittau merged commit 25bc534 into python:main Apr 2, 2026
51 checks passed
@srittau srittau deleted the _operator-any branch April 2, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants