Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reportAny is hard to use #1131

Open
npip99 opened this issue Mar 11, 2025 · 2 comments
Open

reportAny is hard to use #1131

npip99 opened this issue Mar 11, 2025 · 2 comments
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@npip99
Copy link

npip99 commented Mar 11, 2025

Description

reportAny is hard to actually enable in a codebase, because there seems to be no way to avoid the errors. Preferably, by setting reportAny: true and reportExplicitAny: false, it should make it so that all uses of Any require some explicit annotation (So as to not "accidentally" let an Any from a poorly typed library spread all over your codebase).

Here are some errors that make it difficult to use this configuration option in practice:

  1. Can't use as return type of a function
    Image
    Even though dict[Any, Any] works,
    Image

  2. It's not possible to explicitly cast something into Any
    Image

  3. It's not possible to destructure an Any from a tuple
    Image

  4. It's not possible to explicitly cast to a primitive
    Image
    (Using cast() works, but that is unsafe - explicit casting will legitimately try to cast it or throw an error if it cant.)


But, maybe this is barking up the wrong tree, maybe the correct way to get the desired behavior is just to make it so that imports from external libraries always presume Unknown rather than Any, even if the library explicitly says the return type is Any.

@DetachHead DetachHead added needs investigation awaiting verification by a maintainer that the issue is valid type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules labels Mar 11, 2025
@KotlinIsland
Copy link
Collaborator

It's not possible to explicitly cast to a primitive

from typing import SupportsFloat, cast

my_float = float(cast(SupportsFloat, my_any))

float isn't really a "cast", or a "primitive", it's a call that performs a conversation based on an interface, I don't think it should be special cased

@DetachHead
Copy link
Owner

  • 1 & 2 - i agree those should probably be reported as reportExplicitAny instead of reportAny.
  • 3 - i think this one is fine as is, since it's not an explicit usage of Any therefore i would consider it implicit. it's more likely that the Any is not being used intentionally
  • 4 - i agree with @KotlinIsland. while it's not ideal it still technically is an implicit Any, and changing the behavior would probably involve special casing those builtin constructors which i think would just make the rule's behavior more confusing

@DetachHead DetachHead removed the needs investigation awaiting verification by a maintainer that the issue is valid label Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
Development

No branches or pull requests

3 participants