Skip to content

Subclassed operator returning Never not recognized #14424

Closed as not planned
Closed as not planned
@Dr-Irv

Description

@Dr-Irv

Bug Report
This came up in pandas-stubs development.

If you have a base class with an operator like __add__() and a subclass that is not allowed to call __add__(), and you notate the subclass as returning Never to indicate that the subclass should not support the operator, mypy does not see the use of the operator as invalid. pyright does pick this up.

To Reproduce

from __future__ import annotations
from typing import Any
from typing_extensions import Never, assert_type


class Base:
    def __add__(self, other: Any) -> Base:
        return Base()


class Sub:
    def __add__(self, other: Any) -> Never:
        pass


b = Base()
s = Sub()
br1 = b + 10
s + 10

Expected Behavior

mypy should report that the last line s+10 is invalid. With pyright, we get "Operator "+" not supported for types "Sub" and "Literal[10]""

Actual Behavior

No output. Actually, we get this:

never.py:12: error: Implicit return in function which does not return  [empty-body]

That's fine - because we actually would be using the line def __add__(self, other: Any) -> Never: in a stubs file.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9 and 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions