Closed
Description
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
- It also happens similarly with a ParamSpec bounded from a passed-in example function in
__init__
: https://mypy-play.net/?mypy=latest&python=3.11&gist=f5c32e85161882381933f637628d84ba - It also happens with a custom wrapper function typed like contextmanager decorator is: https://mypy-play.net/?mypy=latest&python=3.11&gist=2f1e63d9786f1dfe34a284615593bf0a
- Possibly related issues:
- Decorated function leaks type variables #11816: Almost the same, but it was decorating a function, not a method.
- ParamSpec + Generics doesn't work #14968: A recent one, that seemed to give similar errors. But it is not using a wrapper.
- Generic decorators don't work with generic functions #1317: Still open, but does not quite capture the same issue.