Skip to content

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

Closed

Description

@wrobell

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

Activity

  1. pranavrajpal commented on Dec 23, 2021

    @pranavrajpal
    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).

  2. wrobell commented on Dec 23, 2021

    @wrobell
    Author

    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)

  3. DMRobertson commented on May 7, 2022

    @DMRobertson

    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

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