python / python/mypy

emit a warning for "unused Any"

Open
#20,078 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-disallow-any
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

A common problem afflicting novices getting into python static type annotations is that they assume Any is the top type in Python, like how Never is its bottom type. Hence, they use Any too much, where it is not necessary.

I think we should emit a warning when an explicit Any annotation is used but didn't have to be, much like what we do for warn-redundant-casts.

In fact, I think there are three related warnings we should emit:

  1. If Any is used but object would have worked
  2. If Any is used but mypy was able to deduce a type that worked (using the regular mypy deduction)
  3. If there is a Callable special form with ... (Ellipsis) in place of the list of parameter types (basically just another way to spell Any, see https://typing.python.org/en/latest/spec/callables.html#meaning-of-in-callable) but one of the previous two cases holds

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing mypy's existing warn-redundant-casts behavior and the typing specification's Callable ellipsis semantics. The work is done when mypy can distinguish the three proposed cases: unnecessary Any where object works, Any where inference provides a usable type, and the corresponding Callable form, with warnings for each applicable case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.