Skip to content

Conversation

@Avasam
Copy link
Collaborator

@Avasam Avasam commented Feb 10, 2025

python/mypy#14032 has been fixed in mypy 1.11.0 (I didn't go as far as to search for the exact commit).

This updates operator.itemgetter to include a change I originally wanted to do in #9117, removes the outdated comment, adds a comment explaining why we still can't do that change, and adds a regression test.

@Avasam Avasam changed the title Improve operator.itemgetter generic following mypy 1.11 fix Improve operator.itemgetter generic following mypy 1.11 fix Feb 10, 2025
@github-actions

This comment has been minimized.

@Avasam
Copy link
Collaborator Author

Avasam commented Feb 10, 2025

There seems to be a variance issue in the interaction with map and/or groupby. But I honestly can't quite figure it out.

@github-actions

This comment has been minimized.

@Avasam Avasam changed the title Improve operator.itemgetter generic following mypy 1.11 fix Improve operator.itemgetter.__call__ generic following mypy 1.11 fix Feb 10, 2025
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@srittau
Copy link
Collaborator

srittau commented Feb 27, 2025

I'm also not sure why this doesn't work. Still I think for now it's best to postpone (i.e. close) this PR, until this works better.

@Avasam Avasam added the help wanted An actionable problem of low to medium complexity where a PR would be very welcome label Mar 3, 2025
@Avasam
Copy link
Collaborator Author

Avasam commented Mar 10, 2025

If you close this, please make it an issue. The behaviour here feels unexpected and seems worth investigating.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment on lines 206 to 207
# preventing [_T_co, _T] instead of [Any, _T]
def __call__(self, obj: SupportsGetItem[Any, _T]) -> _T: ...
Copy link
Collaborator Author

@Avasam Avasam Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind abandoning the changes of this PR if I can at least update the reason why we have to keep def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ..., given that python/mypy#14032 is closed as completed.

I just don't fully understand what's the issue at hand.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a variance issue. SupportsGetItem's second type var is covariant, while _T is invariant. Maybe introduce a _T1_co type var and try to use that instead?

Copy link
Collaborator Author

@Avasam Avasam Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took another look at it, and I think I understand my main issue: There's no way to preserve the itemgetter typevar as a literal
itemgetter(0) should be a itemgetter[Literal[0]] but type-checkers understand it as a is a itemgetter[int].

groupby iterates tuples (tuple[_T_co, Iterator[_S_co]]). But there's no way afaik to use map(operator.itemgetter(0), groupby(iterable)) such that type checkers know that it returns a map of the first element (map[_T_co] and not map[_T_co | Iterator[_S_co]])

(I think getter: operator.itemgetter[Literal[0]] = operator.itemgetter(0) technically works, but it's even more verbose and you still deal with variance issues with groupby anyway)

@Avasam Avasam changed the title Improve operator.itemgetter.__call__ generic following mypy 1.11 fix Update comments for operator.itemgetter.__call__ generic following mypy 1.11 fix Sep 30, 2025
@github-actions

This comment has been minimized.

@Avasam Avasam removed the help wanted An actionable problem of low to medium complexity where a PR would be very welcome label Oct 3, 2025
@Avasam Avasam requested a review from srittau October 3, 2025 16:26
@github-actions
Copy link
Contributor

github-actions bot commented Oct 7, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Avasam Avasam requested a review from srittau October 7, 2025 15:55
@srittau srittau merged commit cffeff0 into python:main Oct 8, 2025
66 checks passed
@Avasam Avasam deleted the Improve-operator.itemgetter-generic-following-mypy-1.11-fix branch October 8, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants