Skip to content

Function returning NotImplementedType cannot return NotImplemented if strict=True #18914

Open

Description

@schuelermine

Bug Report

A function returning NotImplementedType cannot return NotImplemented if strict=True.

(A clear and concise description of what the bug is.)

To Reproduce

from types import NotImplementedType

def foo() -> NotImplementedType:
    return NotImplemented

Expected Behavior

Type checks even with --strict

Actual Behavior

main.py:4: error: Returning Any from function declared to return "_NotImplementedType"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12

Activity

  1. sterliakov commented on Apr 12, 2025

    @sterliakov
    Collaborator

    Ohh. NotImplemented is really underspecified in typing context. For all comparison methods we silently assume that it can be returned from bool-returning methods like __lt__. I agree that NotImplemented shouldn't be treated as Any everywhere, but this change will be rather difficult to release - everything from stdlib to third-party stubs relies on it.

    However, it's certainly reasonable to make NotImplemented a better exception - perhaps by tweaking its TypeOfAny to stop it from showing in the reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-disallow-anyThe disallow-any-* family of flags

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions