Skip to content
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

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

Closed
surrealoatmeal opened this issue May 31, 2024 · 2 comments · Fixed by #17424
Labels
bug mypy got something wrong

Comments

@surrealoatmeal
Copy link

surrealoatmeal commented May 31, 2024

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!

@surrealoatmeal surrealoatmeal added the bug mypy got something wrong label May 31, 2024
@surrealoatmeal
Copy link
Author

surrealoatmeal commented May 31, 2024

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.

@hauntsaninja
Copy link
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
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants