basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

`no-any-explicit` false positive with type parameter default `Any`

Open jorenham opened this issue 10 months ago • 4 comments

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

jorenham avatar Jan 12 '25 18:01 jorenham

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?

jorenham avatar Jan 12 '25 18:01 jorenham

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".

jorenham avatar Jan 12 '25 19:01 jorenham

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.

so this is more about understandability? or would you not want an error here at all?

KotlinIsland avatar Jan 13 '25 02:01 KotlinIsland

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 🤷🏻

jorenham avatar Jan 13 '25 05:01 jorenham