python / python/mypy

`mypy --any-exprs-report` depends on formatting

Open
#18,405 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

The report any-exprs.txt generated by mypy --any-exprs-report changes its output, even if the AST has not changed in any way.

To Reproduce

This is a minimal example:

Error = (
    int
    | int
)

mypy reports

 Name   Anys   Exprs   Coverage
-------------------------------
    t      2       4     50.00%
-------------------------------
Total      2       4     50.00%

On the other hand, this example produces no errors

OK = int | int

Again the table, for completeness.

 Name   Anys   Exprs   Coverage
-------------------------------
    s      0       2    100.00%
-------------------------------
Total      0       2    100.00%

The underlying reason is that mypy counts expressions set as TYPE_UNANALYZED as Anys expression. This is very surprising to me. The specific expression is assigned TYPE_UNANALYZED in StatisticsVisitor.type.

Expected Behavior

The invocation mypy --any-exprs-report matches the number of errors encountered by running mypy --disallow-any-exprs. Or at the very least clear up the documentation:

Causes mypy to generate a text file report documenting how many
    expressions of type ``Any`` are present within your codebase.

Your Environment

I don't think this matters, but:

  • Mypy version used:
    mypy 1.14.0 (compiled: yes)
    mypy 1.15.0+dev.d79d89e3ff31ed67a41b0663da9ec8e037d41fa0 (compiled: no)
  • Mypy command-line flags: --any-exprs-report
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12.1

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 with the any-exprs report implementation and the StatisticsVisitor.type location named in the report. Compare its TYPE_UNANALYZED handling with the behavior of --disallow-any-exprs, then check the --any-exprs-report documentation wording. Done means the report no longer changes for equivalent formatting, or the documented behavior accurately describes what it counts.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.