python / python/mypy

Deal better with "def __init__(self)" return type

Open
#7,291 3 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs discussion priority-1-normal topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

If untyped defs are disallowed, mypy requires that def __init__(self): has an explicit -> None return type, even though normally the return type is optional for __init__. This can be surprising, so it would be better for mypy to produce a special note in this case about why the return type is required even for __init__.

Another idea would be to drop the error message if using --check-untyped-defs, since the explicit return type arguably doesn't add much value.

See #5943 for context.

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 tracing how mypy reports the missing return type for untyped init methods and how --check-untyped-defs affects that diagnostic. Resolve whether the intended behavior is a special explanatory note or suppressing the error, then verify the chosen behavior with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.