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

Union between a callable and a type not respected by functools.partial #17741

Closed
eli-schwartz opened this issue Sep 6, 2024 · 2 comments · Fixed by #17903
Closed

Union between a callable and a type not respected by functools.partial #17741

eli-schwartz opened this issue Sep 6, 2024 · 2 comments · Fixed by #17903
Assignees
Labels
bug mypy got something wrong

Comments

@eli-schwartz
Copy link
Contributor

Seems similar to #17646 but not fixed in https://mypy-play.net/?mypy=master&python=3.12
The linked #17659 is not applicable because I am not using Self.

Sample code:

import functools
import typing as T
from typing_extensions import TypeAlias

FooBarFunc: TypeAlias = T.Callable[..., 'FooBar']

class FooBar:
    def __init__(self, arg1: str) -> None:
        pass

def transform(t: T.Union[T.Type[FooBar], FooBarFunc]) -> None:
    val = functools.partial(t)

Error:

main.py:12: error: "type[FooBar] | Callable[..., FooBar]" not callable  [misc]

The codebase where this was discovered: https://github.com/mesonbuild/meson/blob/83f8de5357f31d6448ae033e1e8ed2b22c8c306a/mesonbuild/dependencies/factory.py#L75-L95

mesonbuild/dependencies/factory.py:95:38: error: "type[CMakeDependency] | Callable[..., CMakeDependency]" not callable  [misc]

Both sides of the union if cast and used independently, work.

@eli-schwartz eli-schwartz added the bug mypy got something wrong label Sep 6, 2024
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 8, 2024

This is sort of underlying behaviour in mypy that is exposed now that mypy checks functools.partial

E.g. on mypy 1.11 both of these will complain

def join(t1: T.Type[FooBar], t2: FooBarFunc):
    val1 = [t1, t2]
    val1[0]("asdf")
    
    val2 = t1 if random.random() < 0.5 else t2
    val2("asdf")

This could be fixed by improving the join logic (improving callable joins would fix many existing mypy issues) or potentially by changing the functools.partial logic to avoid using joins on unions (this could get messy)

@eli-schwartz
Copy link
Contributor Author

eli-schwartz commented Oct 9, 2024

Thanks! I've tested mypy from git and there is one less error report now when upgrading mypy (and looks like the others are all either our fault, unused "type: ignore", or a renamed error code). <3

JukkaL added a commit that referenced this issue Oct 10, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants