python / python/mypy

[Regression 1.6.x -> 1.7] Invalid "Returning Any from function declared..."

Open
#16,499 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

To Reproduce

from __future__ import annotations
from functools import partialmethod
import operator
from typing import Any, Union

ScalarType = Union[int, bool, float]
ArrayOrScalar = Union["Array", ScalarType]


class Array:
    def _binary_op(self,
            op: Any,
            other: ArrayOrScalar,
            ) -> Array:

        return Array()

    __mul__ = partialmethod(_binary_op, operator.mul)


def dot(a: ArrayOrScalar, b: ArrayOrScalar) -> ArrayOrScalar:
    assert isinstance(a, Array)
    assert isinstance(b, Array)

    return a * b

Expected Behavior

Type-checks correctly, as it does with 1.6.1.

Actual Behavior

/home/andreas/tmp/mypy-array-repro.py:30: error: Returning Any from function declared to return "Array | int | bool | float"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Your Environment

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

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 running the supplied reproducer with mypy 1.7.0 and --strict, then compare its result with mypy 1.6.1. Trace how the type checker handles Array._binary_op, partialmethod, and operator.mul; done means the example no longer reports no-any-return while preserving the expected return type.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.