Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
inducer opened this issue Nov 15, 2023 · 3 comments
Open
Labels
bug mypy got something wrong topic-overloads

Comments

@inducer
Copy link

inducer commented Nov 15, 2023

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
@inducer inducer added the bug mypy got something wrong label Nov 15, 2023
@AlexWaygood AlexWaygood added topic-disallow-any The disallow-any-* family of flags and removed topic-disallow-any The disallow-any-* family of flags labels Nov 15, 2023
@ilevkivskyi
Copy link
Member

OK, the problem is that partialmethod has an overloaded constructor, and mypy has a rule that if an argument matches multiple overloads with different (inferred) return types, and this argument contains an Any type, then the inferred return type will be also "erased". Although this rule is weird, it has certain good reasons, so technically current behavior is expected.

FWIW there is an easy fix, use object instead of Any here:

    def _binary_op(self,
            op: object,  # <-- here
            other: ArrayOrScalar,
            ) -> Array:

        return Array()

this fixes the problem

@inducer
Copy link
Author

inducer commented Nov 16, 2023

Thanks for responding! I agree that the workaround you provide does address the issue for the reproducer I provided, however unfortunately that is not the case in the full-scale code from which the reproducer is derived. Specifically I am talking about this error:

pytato/array.py:2533: error: Returning Any from function declared to return "Array | number[Any] | int | bool_ | bool | float"  [no-any-return]

being reported for this commit of the full-scale thing:

inducer/pytato@a8f380f

I have not yet succeeded in making a smaller reproducer that still shows the issue.

@inducer
Copy link
Author

inducer commented Nov 17, 2023

FWIW, the corresponding bits of the full-scale code are here:

https://github.com/inducer/pytato/blob/86a55ba423b013150e63b5d6cdc11bc29eabb00f/pytato/array.py#L528-L568

for the call to partialmethod (and the method on which it is being used) and here:

https://github.com/inducer/pytato/blob/86a55ba423b013150e63b5d6cdc11bc29eabb00f/pytato/array.py#L2533

for the affected call site being reported.

inducer added a commit to inducer/pytato that referenced this issue Nov 21, 2023
inducer added a commit to inducer/pytato that referenced this issue Nov 21, 2023
nkoskelo pushed a commit to nkoskelo/pytato that referenced this issue Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overloads
Projects
None yet
Development

No branches or pull requests

3 participants