basedmypy
basedmypy copied to clipboard
`no-any-explicit` false positive with type parameter default `Any`
Describe the problem
There's no explicit Any here:
a = slice(None)
b: slice = a
But the following error is reported:
Explicit "Any" is not allowed (no-any-explicit) [Ln 2, Col 1]
I'm guessing this is because the type parameters of slice default to Any. But I don't see why such an implicit Any should be considered as explicit.
Gist to reproduce
a = slice(None)
b: slice = a
Severity
annoying but workaround is available
Your Environment
basedmypy 2.9.1 (compiled: yes) Based on mypy 1.14.1
And the weirdest thing is that in this example, it occurs twice:
from typing import Generic, TypeVar
T = TypeVar("T", bound=int | slice) # E: no-any-explicit
class A(Generic[T]): ... # <no error>
class B(A[T]): ... # E: no-any-explicit
So with A it doesn't report an error, but with B it does?
For a real life example, see e.g. optype/numpy/_to.py, which doesn't even import Any, yet there are 62 no-any-explicit errors reported, forcing me to use # mypy: disable-error-code="no-any-explicit".
I'm guessing this is because the type parameters of
slicedefault toAny. But I don't see why such an implicitAnyshould be considered as explicit.
so this is more about understandability? or would you not want an error here at all?
I don't think it's useful in this case, because the Any is out of your control. I also don't think that it's useful to report the same error about the same Any on multiple occasions. Given the name of the error, I expected it to only be reported once I explicitly write "Any", so that I can actually see that it's there.
I might be wrong here, but I thought that that's also the way that bpr's reportExplicitAny works 🤷🏻