python / python/mypy

Mypy Gives `Incompatible Definitions` Error When Dynamically Typed

Open
#13,362 3 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

Discussed in https://github.com/python/typing/discussions/1235

Originally posted by adam-grant-hendry August 4, 2022
Related to this SO question, I'm trying to understand why

but

I would have assumed since nothing is statically typed, that mypy would not produced any errors. In fact, the same code on the pyright playground shows no errors.

I though perhaps the error could be due to the potential for a keyword collision, which I gleaned from PEP 692. e.g. If I made the name of the 4th arguments the same, each could fail the same way if I tried to pass foo as a keyword argument:

>>> a = Foo()
>>> a.run(1, 2, 'baz', foo='bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func() got multiple values for argument 'foo'

but if I keep the argument names different, then the above would work if I tried to call Bar.run(1, 2, 'baz', foo='bar'), but break for Foo.

However, I still would originally assume mypy would do nothing because nothing has been statically typed:

By default, mypy will not type check dynamically typed functions. This means that with a few exceptions, mypy will not report any errors with regular unannotated Python.

To Reproduce

Please see the mypy playground (and pyright playground) gists above.

Expected Behavior

mypy issues no errors.

Actual Behavior

mypy issues the error:

Definition of "run" in base class "Foo" is incompatible with definition in base class "Bar"

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.10, x64-bit
  • Operating system and version: Windows 10, version 20H2

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 two linked mypy-play reproductions and compare their behavior with the linked Pyright playground, focusing on the dynamically typed method definitions and added **kwargs. Check the related typing discussion and existing mypy tests for method-definition compatibility; done means the behavior is explained and, if confirmed as a bug, covered by a regression test.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.