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

SupportsGetItem name confusing #11822

Open
anuraaga opened this issue Apr 24, 2024 · 7 comments
Open

SupportsGetItem name confusing #11822

anuraaga opened this issue Apr 24, 2024 · 7 comments
Labels
status: deferred Issue or PR deferred until some precondition is fixed

Comments

@anuraaga
Copy link
Contributor

anuraaga commented Apr 24, 2024

I'm not sure if this is an issue with the type annotation for itemgetter or SupportsGetItem.

SupportsGetItem requires defining __contains__.

def __contains__(self, x: Any, /) -> bool: ...

This is surprising to me since I'd expect this trait to be about being able to index with [n], which only should need __getitem__. Perhaps there is a reason for it though, but then it means itemgetter can only be used with types that define __contains__.

def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...

This is despite itemgetter not using in or __contains__, just indexing, so it should be allowed I think

https://github.com/python/cpython/blob/main/Lib/operator.py#L288

One important effect is that while you'd expect itemgetter to always work instead of a lambda x: x[0], it doesn't for these types.


Deferred until SupportsContainsAndGetItem has made it into type checkers. Most likely after mypy 1.11.0 has been released.

@srittau
Copy link
Collaborator

srittau commented Apr 24, 2024

It's a bit weird that SupportsGetItem also requires __contains__ as this conflicts with our usual naming policy. This was probably done to support the old-style iteration protocol for container checks, but is highly confusing and misused in typeshed.

We should:

  1. At first:
    • Remove the "stable" marker from SupportsGetItem.
    • Add a SupportsGetItemAndContains SupportsContainsAndGetItem protocol and a "true", but temporary SupportsGetItem protocol, potentially named something intentionally awkward like TempSupportsGetItem.
    • Review existing stdlib annotations and use the new protocols where suitable.
  2. After TempSupportsGetItem has propagated to type checkers, we should replace annotations in stubs as well.
  3. After about a year, remove SupportsGetItem.
  4. After another grace period of maybe a year, rename TempSupportsGetItem to SupportsGetItem.

srittau added a commit to srittau/typeshed that referenced this issue Apr 24, 2024
@srittau
Copy link
Collaborator

srittau commented Apr 24, 2024

#11825 shows that there's only one place in the stdlib that actually meant to use the existing SupportsGetItem, while there are multiple places that only require __getitem__.

@srittau srittau changed the title Cannot use operator.itemgetter on a user type without __contains__ SupportsGetItem name confusing Apr 24, 2024
@Akuli
Copy link
Collaborator

Akuli commented Apr 24, 2024

I'm getting the impression that most usages of the old SupportsGetItem would be converted to use the new SupportsGetItem (that is, they don't need __contains__). If so, then why don't we just delete __contains__ right away? It would make most stubs more correct.

@srittau
Copy link
Collaborator

srittau commented Apr 24, 2024

SupportsGetItem is marked as "stable", so I'm wary changing it without a deprecation process. It would be much easier, of course.

@srittau
Copy link
Collaborator

srittau commented Apr 24, 2024

Checking the third-party stubs, I only found one place where SupportsContainsAndGetItem would be used and several where SupportsGetItem would be correct.

@Akuli
Copy link
Collaborator

Akuli commented Apr 24, 2024

IMO stable shouldn't mean "will not receive bug fixes". I see this as a bug, not a feature. I would see it as a feature if many stubs relied on the current behavior.

But to me, the meaning of stable/bug/feature doesn't really matter, and it's more about "practicality beats purity".

@srittau
Copy link
Collaborator

srittau commented Apr 24, 2024

Let's try this after SupportsContainsAndGetItem has made it into type checkers.

@srittau srittau added the status: deferred Issue or PR deferred until some precondition is fixed label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: deferred Issue or PR deferred until some precondition is fixed
Projects
None yet
Development

No branches or pull requests

3 participants