python / python/mypy

dmypy suggest should not suggest for abstract methods?

Open
#18,965 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

with --no-errors the suggestions from dmypy suggest for abstract methods can lead to type errors

To Reproduce

import abc

class C:
    @abc.abstractmethod
    def get_key(self): pass

class D(C):
    def get_key(self):
        return 'key'

Expected Behavior

either no suggestion or () -> str for t.C.get_key

Actual Behavior

$ dmypy run t.py
...
$ dmypy suggest t.D.get_key
() -> str
$ dmypy suggest t.C.get_key
() -> None

applying both of these suggestions results in a type error:

$ mypy t.py
t.py:8: error: Return type "str" of "get_key" incompatible with return type "None" in supertype "C"  [override]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: see above
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.13.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 dmypy suggest entry point and reproduce the example using dmypy run t.py, then compare suggestions for t.D.get_key and t.C.get_key. The fix is done when an abstract method does not produce a conflicting None return suggestion, and the example passes mypy without an override error.

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
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.