python / python/mypy

Incorrect note references builtins.pyi instead of the class definition

Open
#20,911 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-error-reporting
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When reporting a "Unexpected keyword argument" error for an initializer, the note can incorrectly reference builtins.pyi instead of the class definition.

To Reproduce
https://mypy-play.net/?gist=b50a11afee5d039fb382667dee8b529f

class Foo:
    bar: str
    
def f() -> Foo:
    foo = Foo(bar="bar")
    return foo

f()

For context, I was using pydantic, but forgot to make the class in question inherit from pydantic.BaseModel. But this isn't specific to pydantic.

Expected Behavior

The note should include the class definition, not an incorrect link to builtins.pyi.
This was misleading because I thought my class was shadowing a built-in one, so I tried renaming it.

Actual Behavior

main.py:5: error: Unexpected keyword argument "bar" for "Foo"  [call-arg]
/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/typeshed/stdlib/builtins.pyi:117: note: "Foo" defined here

I believe the line referenced here is for object.__init__().

Your Environment

  • Mypy version used: 1.19.1
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a (default for mypy playground)
  • Python version used: python3.12

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 running the Python reproducer from the issue and compare the diagnostic note for the initializer with the expected class definition reference. Trace how the error identifies the initializer and formats its source location, then confirm the fix still reports the unexpected keyword argument correctly without pointing to builtins.pyi.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.