Skip to content

Wrong inferred return type for functions with overloads #315

@sk-

Description

@sk-

In our codebase, which uses sqlalchemy, we have a bunch of errors due to how overloads are handled in presence of Any.

The following code shows the issue

from typing import Any

import sqlalchemy as sa
from sqlalchemy import Table
from sqlalchemy.sql.functions import sum


def build_label(table: Table, label: str) -> sa.Label[Any]:
    reveal_type(sa.func.sum(table.c.value_col))
    reveal_type(sa.func.coalesce(sa.func.sum(table.c.value_col), 0))
    return sa.func.coalesce(sa.func.sum(table.c.value_col), 0).label(label)


def build_label_v2(table: Table, label: str) -> sa.Label[Any]:
    sum_col: sum[Any] = sa.func.sum(table.c.value_col)
    return sa.func.coalesce(sum_col, 0).label(label)


def build_label_v3(table: Table, label: str) -> sa.Label[Any]:
    sum_col: sum[int] = sa.func.sum(table.c.value_col)
    return sa.func.coalesce(sum_col, 0).label(label)

In mypy, this type checks correctly and outputs:

zuban_sa_func.py:9: note: Revealed type is "sqlalchemy.sql.functions.sum[Any]"
zuban_sa_func.py:10: note: Revealed type is "sqlalchemy.sql.functions.coalesce[Any]"
Success: no issues found in 1 source file

while zuban reports

zuban_sa_func.py:9: note: Revealed type is "Any"
zuban_sa_func.py:10: note: Revealed type is "Any"
zuban_sa_func.py:11: error: Returning Any from function declared to return "Label[Any]"  [no-any-return]
zuban_sa_func.py:16: error: Returning Any from function declared to return "Label[Any]"  [no-any-return]
Found 2 errors in 1 file (checked 1 source file)

Note that we were able to correctly type the function build_label_v3 by completely avoiding the Any type.

I'm under the impression this may be related to #263.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions