Skip to content

contextmanager wrapping a method using ParamSpec fails to infer correct type #15002

Closed
@AxelVoitier

Description

@AxelVoitier

Bug Report

When using contextmanager decorator on an object method, which uses a ParamSpec, then the inferred type of the resulting method seems to be one for an unbounded method. And therefore it expects an extra "self" parameter at the beginning.

To Reproduce

from contextlib import contextmanager
from typing import Iterator, ParamSpec, Generic, reveal_type

_P = ParamSpec('_P')

class AClass(Generic[_P]):

    @contextmanager
    def a_cm_with_param_spec(self, *args: _P.args, **kwargs: _P.kwargs) -> Iterator[None]:
        yield

an_object = AClass[(int, str)]()

with reveal_type(an_object.a_cm_with_param_spec)(12, 'hello'):
    pass

https://mypy-play.net/?mypy=latest&python=3.11&gist=bc44336f0994253277609e8163f1e5b5

Expected Behavior

Expecting no error.
Revel type should have shown something like def (builtins.int, builtins.str) -> contextlib._GeneratorContextManager[None]

Actual Behavior

main.py:16: error: Too few arguments for "a_cm_with_param_spec" of "AClass"  [call-arg]
main.py:16: note: Revealed type is "def (self: __main__.AClass[[builtins.int, builtins.str]], builtins.int, builtins.str) -> contextlib._GeneratorContextManager[None]"
main.py:16: error: Argument 1 to "a_cm_with_param_spec" of "AClass" has incompatible type "int"; expected "AClass[[int, str]]"  [arg-type]
main.py:16: error: Argument 2 to "a_cm_with_param_spec" of "AClass" has incompatible type "str"; expected "int"  [arg-type]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

Comments

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