Skip to content

Regression on validation of manual type annotations to functools.partial between stable version and git #17301

Closed

Description

@surrealoatmeal

Bug Report

When validating manual type hints to the return of a call to functools.partial, there has occurred a regression between the latest stable release (1.10.0) and the master branch, in that the git version asserts that the correct annotation should be partial[partial[T]], where T is the type manually annotated for the functools.partial call.

To Reproduce
TL;DR:
master branch: https://mypy-play.net/?mypy=master&python=3.12&gist=e45e31f06514aabe6a039dfc759e9a3b
1.10.0: https://mypy-play.net/?mypy=latest&python=3.12&gist=e45e31f06514aabe6a039dfc759e9a3b

Consider:

from functools import partial
from typing import Type, TypeVar

T = TypeVar("T")


def generic(string: str, integer: int, resulting_type: Type[T]) -> T:
    if isinstance(string, resulting_type):
        return string
    elif isinstance(integer, resulting_type):
        return integer
    raise RuntimeError

Expected Behavior
For the below definition:

p: partial[str] = partial(generic, resulting_type=str)

The stable release does not generate any errors, as expected.

Actual Behavior
However, on the current git build, the following error message is displayed:

error: Incompatible types in assignment (expression has type "partial[partial[str]]", variable has type "partial[str]")  [assignment]
error: Argument 1 to "generic" has incompatible type "type[str]"; expected "type[partial[str]]"  [arg-type]

If the type annotation is changed to something nonsensical:

p: partial[bool] = partial(generic, resulting_type=str)

mypy now generates an error asserting the type of the expression is of type partial[partial[TYPE GIVEN]]:

main.py:13: error: Incompatible types in assignment (expression has type "partial[partial[bool]]", variable has type "partial[bool]")  [assignment]
main.py:13: error: Argument 1 to "generic" has incompatible type "type[str]"; expected "type[partial[bool]]"  [arg-type]

Playground for this: https://mypy-play.net/?mypy=master&python=3.12&gist=8b38f943d002dc6ce084e8771251e78a

Your Environment
N/A, reproducible in mypy Playground environment.

Thank you for your hard work maintaining mypy!

Activity

  1. surrealoatmeal commented on May 31, 2024

    @surrealoatmeal
    Author

    Actually, on further inspection, the current stable release probably doesn't generate any errors because the type given to the annotation is not validated at all.
    For example, the partial[bool] annotation is also considered correct by version 1.10.0: https://mypy-play.net/?mypy=latest&python=3.12&gist=8b38f943d002dc6ce084e8771251e78a

    So, rather than being a regression, it could be the incomplete and/or buggy implementation of a new check introduced to the codebase after 1.10.0.

    I am not sure whether to file a separate bug report or even change the title to reflect my hypothesis though, as they could be unrelated bugs, so I'm leaving it up to the discretion of the maintainers.

  2. hauntsaninja commented on Jun 22, 2024

    @hauntsaninja
    Collaborator

    Thanks for testing against latest! If your repo is open source, lmk, I can add it to https://github.com/hauntsaninja/mypy_primer

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 wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions