python / python/mypy

Cannot assert a callable returns `None` when it must return `None`

Open
#17,568 1 comment 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

mypy prevents you making assertions about callables that must return None when they're called.
This is important as not everyone works in a type-checked context, and so library code must be robust to this.
In my case, I have a chain of callables, the ones in the middle take iterables and return iterables, but the one at the start must take no arguments and the one at the end must consume the upstream iterable and return nothing.

To Reproduce

def returns_none() -> None:
    pass

assert returns_none() is None

Expected Behavior

No mypy complaints

Actual Behavior

"returns_none" does not return a value (it only ever returns None) [func-returns-value]

Your Environment

  • Mypy version used: mypy 1.10.1 (compiled: yes)
  • Mypy command-line flags: none
  • Python version used: Python 3.12.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

Reproduce the report with mypy 1.10.1 on Python 3.12.1 using the shown returns_none function and assertion. Trace the func-returns-value diagnostic, then verify that assertions about callables annotated to return None produce no complaint while the existing diagnostic remains for inappropriate value use.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.