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

Bug with combination of contextmanager, partial, and ParamSpec #10011

Open
dmontagu opened this issue Mar 1, 2025 · 1 comment
Open

Bug with combination of contextmanager, partial, and ParamSpec #10011

dmontagu opened this issue Mar 1, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@dmontagu
Copy link

dmontagu commented Mar 1, 2025

Describe the bug
If I have a generic dataclass with a ParamSpec in its generic parameters, and it has a method decorated with @contextmanager or @asynccontextmanager that accepts the ParamSpec's args/kwargs, and I use a functools.partial as the input (which is used to infer the ParamSpec), there seems to be a bug where pyright thinks I need to pass a value for self on the method. Minimal reproduction below.

Please let me know if I'm misunderstanding something, but as far as I can tell the issue seems to be coming from the combination of all three of these things (ParamSpec, contextmanager, and partial). If I remove any one of these, the spurious type error goes away.

Code or Screenshots
The following examples execute without errors and as far as I can tell should not produce type errors. I've included the pyright errors and printed outputs in in-line comments.

Sync example:

import dataclasses
from contextlib import contextmanager
from functools import partial
from typing import ParamSpec, Generic, Callable, Iterator

P = ParamSpec('P')


@dataclasses.dataclass
class MyCaller(Generic[P]):
    my_callable: Callable[P, None]

    @contextmanager
    def my_context_manager_method(self, *args: P.args, **kwargs: P.kwargs) -> Iterator[None]:
        self.my_callable(*args, **kwargs)
        yield


def my_callable(x: int, y: int) -> None:
    print(f'{x=} {y=}')


def main():
    task = partial(my_callable, y=2)
    with MyCaller(task).my_context_manager_method(x=1):  # error: Argument missing for parameter "self" (reportCallIssue)
        pass

main()
#> x=1 y=2

Async example:

import asyncio 
import dataclasses
from contextlib import asynccontextmanager
from functools import partial
from typing import ParamSpec, Generic, Callable, AsyncIterator, Awaitable

P = ParamSpec('P')


@dataclasses.dataclass
class MyCaller(Generic[P]):
    my_callable: Callable[P, Awaitable[None]]

    @asynccontextmanager
    async def my_context_manager_method(self, *args: P.args, **kwargs: P.kwargs) -> AsyncIterator[None]:
        await self.my_callable(*args, **kwargs)
        yield


async def my_callable(x: int, y: int) -> None:
    print(f'{x=} {y=}')


async def main():
    task = partial(my_callable, y=2)
    async with MyCaller(task).my_context_manager_method(x=1):  # error: Argument missing for parameter "self" (reportCallIssue)
        pass


asyncio.run(main())
#> x=1 y=2

VS Code extension or command-line
Command line. I get this behavior on both v1.1.389 and v1.1.395.


Pyright is awesome, thank you so much!

@dmontagu dmontagu added the bug Something isn't working label Mar 1, 2025
@dmontagu
Copy link
Author

dmontagu commented Mar 1, 2025

Reductions of the above that do NOT produce type errors:

Each of these is basically the same as the problematic code above, showing that it really is the combination of all three of these things together that causes the problem.


No @contextmanager:

import dataclasses
from functools import partial
from typing import ParamSpec, Generic, Callable, Iterator

P = ParamSpec('P')


@dataclasses.dataclass
class MyCaller(Generic[P]):
    my_callable: Callable[P, None]

    def my_context_manager_method(self, *args: P.args, **kwargs: P.kwargs) -> Iterator[None]:
        self.my_callable(*args, **kwargs)
        yield


def my_callable(x: int, y: int) -> None:
    print(f'{x=} {y=}')


def main():
    task = partial(my_callable, y=2)
    MyCaller(task).my_context_manager_method(x=1)

main()
#> x=1 y=2

No ParamSpec:

import dataclasses
from contextlib import contextmanager
from functools import partial
from typing import Callable, Iterator, Any


@dataclasses.dataclass
class MyCaller:
    my_callable: Callable[..., None]

    @contextmanager
    def my_context_manager_method(self, *args: Any, **kwargs: Any) -> Iterator[None]:
        self.my_callable(*args, **kwargs)
        yield


def my_callable(x: int, y: int) -> None:
    print(f'{x=} {y=}')


def main():
    task = partial(my_callable, y=2)
    with MyCaller(task).my_context_manager_method(x=1):
        pass


main()
# > x=1 y=2

No partial:

import dataclasses
from contextlib import contextmanager
from functools import partial
from typing import ParamSpec, Generic, Callable, Iterator

P = ParamSpec('P')


@dataclasses.dataclass
class MyCaller(Generic[P]):
    my_callable: Callable[P, None]

    @contextmanager
    def my_context_manager_method(self, *args: P.args, **kwargs: P.kwargs) -> Iterator[None]:
        self.my_callable(*args, **kwargs)
        yield


def my_callable(x: int, y: int) -> None:
    print(f'{x=} {y=}')


def main():
    with MyCaller(my_callable).my_context_manager_method(x=1, y=2):
        pass

main()
#> x=1 y=2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant