python / python/mypy

mypy doesn't infer **kwargs types from superclass

Open
#10,079 5 comments 16 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-inference topic-inheritance
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report
If a subclass __init__ passes **kwargs to it's superclass, the superclasses parameter types should be used to validate the subclass arguments.

To Reproduce

Run mypy on this file:

import datetime


class Bar:
    def __init__(self, a: int, b: str, c: datetime.datetime):
        pass


class Baz(Bar):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


Baz(b=3)

Expected Behavior

Something like
error: Argument "b" to "Baz" has incompatible type "int"; expected "str"

Actual Behavior

mypy found no errors

Your Environment

  • Mypy version used: mypy 0.800
  • Mypy command-line flags: python filename
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.6
  • Operating system and version: macOS 10.15.7

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 mypy on the Python reproducer in the issue and compare the reported behavior with the expected error for Baz(b=3). Trace the type-checking entry point for subclass init calls that forward **kwargs, then add or update coverage for the mismatch. Done means incompatible keyword types passed to Baz are reported using the superclass parameter types.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.