Skip to content

question to the signature of list.index #134025

Not planned
Not planned
@weipeng1999

Description

@weipeng1999

Recently I switch to use pyright as my lsp server. This server is very strict at type annotation, and I found some errors as following.

  error: Type "list[int]" is not assignable to declared type "SequenceNotStr[int]"
    "list[int]" is incompatible with protocol "SequenceNotStr[int]"
      "index" is an incompatible type
        Type "(value: int, start: SupportsIndex = 0, stop: SupportsIndex = sys.maxsize, /) -> int" is not assignable to type "(value: Any, /, start: int = 0, stop: int = ...) -> int"
          Missing keyword parameter "start"
          Missing keyword parameter "stop"
            Position-only parameter mismatch; parameter "start" is not position-only
            Position-only parameter mismatch; parameter "stop" is not position-only (reportAssignmentType)
1 error, 0 warnings, 0 informations

so I check the "index" signature of list.index and SequenceNotStr.index, found that:

>>> inspect.signature(list.index)
<Signature (self, value, start=0, stop=9223372036854775807, /)>
>>> inspect.signature(pd_typing.SequenceNotStr.index)
<Signature (self, value: 'Any', /, start: 'int' = 0, stop: 'int' = Ellipsis) -> 'int'>

The signature of list.index make me surprised. Why we set start and stop as the position-only argument to the list.index? I think defined them as position-and-keyword argument is a better idea, so that we can using keyword to set them. Meanwhile i think give default value to position-only argument is a little strange

Activity

JelleZijlstra

JelleZijlstra commented on May 15, 2025

@JelleZijlstra
Member

I think the typing issue you see was fixed in hauntsaninja/useful_types#23 last year. We could give list.index positional-or-keyword arguments but I don't feel strongly.

weipeng1999

weipeng1999 commented on May 15, 2025

@weipeng1999
Author

I think the typing issue you see was fixed in hauntsaninja/useful_types#23 last year. We could give list.index positional-or-keyword arguments but I don't feel strongly.

Thanks for reply, but the SequenceNotStr is pandas._typing.SequenceNotStr, so it still not fix for me (its my wrong that not clearify which package the SequenceNotStr is provided). But I think this show that more third party typing system set the arguments as position-and-keyword, so this question may be more important than we think.

JelleZijlstra

JelleZijlstra commented on May 15, 2025

@JelleZijlstra
Member

Well, then this is a bug in pandas.

weipeng1999

weipeng1999 commented on May 15, 2025

@weipeng1999
Author

Well, then this is a bug in pandas.

even this is a bug and we keep old behaviour, maybe at least we should clearly set the argument type of this two parameters in python standard document to guild third party package what is the correct signature of index?

feoh

feoh commented on May 22, 2025

@feoh
Contributor

@weipeng1999 Pardon my ignorance, but could you please include a code snippet that's causing pyright to emit that error?

AA-Turner

AA-Turner commented on May 22, 2025

@AA-Turner
Member

Closing, for help please use other fora.

A

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

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      question to the signature of list.index · Issue #134025 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant