python / python/mypy

Exact error codes for mypy errors

Open
#10,843 10 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

Pylint / Flake8 style, unique error codes to be shown with every error that mypy raises.

Pitch

Mypy currently has ~40 general error names, that are shown next to the error if you pass the --show-error-code flag to mypy. For eg:

$ cat sample.py
def foo(a: str) -> str:
    return '(' + a.split() + ')'

$ mypy sample.py --show-error-codes
sample.py:2: error: Unsupported operand types for + ("str" and "List[str]")  [operator]
Found 1 error in 1 file (checked 1 source file)

But in total there's at least 80 probably around 400 unique error messages, each for a different kind of error.

The idea
  • Create a proper list of numeric error codes, say 001 though 080 for eg., identifying each error uniquely.
  • Have the error code shown directly before the error message in mypy output, just like pylint, flake8, and others.
$ cat mytest.py      
def a():
    print(foo)

$ pylint mytest.py
************* Module mytest
mytest.py:1:0: C0114: Missing module docstring (missing-module-docstring)
mytest.py:1:0: C0103: Function name "a" doesn't conform to snake_case naming style (invalid-name)
mytest.py:2:10: E0602: Undefined variable 'foo' (undefined-variable)
[...]

$ flake8 mytest.py
mytest.py:1:1: D100 Missing docstring in public module
mytest.py:1:1: D103 Missing docstring in public function
mytest.py:2:11: F821 undefined name 'foo'

I've run the idea past @ethanhs and I'm told that this would be a great-to-have in mypy. I'm willing to contribute this, once I've run it past the mypy team 😄

cc @srijan-deepsource

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 mypy's existing --show-error-code behavior and the sample.py and mytest.py examples in the issue. Define a stable numeric code for each distinct error and ensure every reported error displays it before the message, with coverage for the existing error cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.