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

Value of type "P.args" is not indexable #11826

Closed
wrobell opened this issue Dec 22, 2021 · 3 comments
Closed

Value of type "P.args" is not indexable #11826

wrobell opened this issue Dec 22, 2021 · 3 comments
Labels
bug mypy got something wrong

Comments

@wrobell
Copy link

wrobell commented Dec 22, 2021

Please consider

import typing as tp

P = tp.ParamSpec('P')

def decorator(f: tp.Callable[P, int]) -> tp.Callable[P, int]:
    def wrapper(*args: P.args, **kw: P.kwargs) -> int:
        print(args[0])
        return 0
    return wrapper

then

$ mypy t.py
t.py:7: error: Value of type "P.args" is not indexable
Found 1 error in 1 file (checked 1 source file)

I would expect no error, as args is indexable actually.

Mypy 0.930, Python 3.10.1

@wrobell wrobell added the bug mypy got something wrong label Dec 22, 2021
@pranavrajpal
Copy link
Contributor

What exactly is your use case for indexing P.args? My understanding was the point of a ParamSpec was to pass it through unchanged to another function without looking at any specific argument because it's meant to be a stand-in for any possible set of function arguments.

That means that there's not much you can do with whatever argument you select from args anyways, considering that args[0] can be any type depending on what function the decorator is applied to (and might not even exist if the function doesn't have positional arguments).

@wrobell
Copy link
Author

wrobell commented Dec 23, 2021

Indeed, PEP-612 says

Inside the function, args has the type P.args, not Tuple[P.args, ...] as would be with a normal annotation (and likewise with the **kwargs)

@DMRobertson
Copy link

For cross-referencing, #12668 seems to suppress this error.

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

No branches or pull requests

3 participants