Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve operator.itemgetter.__call__ generic following mypy 1.11 fix #13489

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

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, 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

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.

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

This comment has been minimized.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/builders/gettext.py: note: In member "__iter__" of class "Catalog":
+ sphinx/builders/gettext.py:93:51: error: Argument "locations" to "Message" has incompatible type "list[str | int]"; expected "list[tuple[str, int]]"  [arg-type]
+ sphinx/builders/gettext.py:93:68: error: Argument "uuids" to "Message" has incompatible type "list[str | int]"; expected "list[str]"  [arg-type]
+ sphinx/ext/intersphinx/_load.py: note: In function "load_mappings":
+ sphinx/ext/intersphinx/_load.py:200:63: error: Argument "key" to "sorted" has incompatible type "itemgetter[tuple[int, int]]"; expected "Callable[[tuple[str, int, dict[str, dict[str, _InventoryItem]]]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]
+ sphinx/ext/intersphinx/_load.py:200:63: note: "itemgetter[tuple[int, int]].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"

rich (https://github.com/Textualize/rich)
+ rich/text.py:247: error: Argument "key" to "sort" of "list" has incompatible type "itemgetter[tuple[int, int]]"; expected "Callable[[tuple[int, bool, str | Style]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]
+ rich/text.py:247: note: "itemgetter[tuple[int, int]].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"
+ rich/columns.py:153: error: Argument 1 to "Constrain" has incompatible type "int | ConsoleRenderable | RichCast | str"; expected "ConsoleRenderable | RichCast | str"  [arg-type]
+ rich/columns.py:160: error: Argument 1 has incompatible type "int | ConsoleRenderable | RichCast | str"; expected "ConsoleRenderable | RichCast | str"  [arg-type]
+ rich/columns.py:170: error: Argument 1 has incompatible type "*list[int | ConsoleRenderable | RichCast | str | None]"; expected "ConsoleRenderable | RichCast | str | None"  [arg-type]
+ rich/layout.py:362: error: Argument "key" to "sorted" has incompatible type "itemgetter[int]"; expected "Callable[[tuple[Layout, Region]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]
+ rich/layout.py:362: note: "itemgetter[int].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"

altair (https://github.com/vega/altair)
+ altair/utils/core.py:950: error: Key expression in dictionary comprehension has incompatible type "type[SchemaBase] | str"; expected type "str"  [misc]

graphql-core (https://github.com/graphql-python/graphql-core)
+ src/graphql/type/validate.py:562: error: Argument 1 to "join" of "str" has incompatible type "map[str | GraphQLInputField]"; expected "Iterable[str]"  [arg-type]

bidict (https://github.com/jab/bidict)
+ bidict/_iter.py: note: In function "inverted":
+ bidict/_iter.py:52:16: error: Argument 1 to "map" has incompatible type "itemgetter[tuple[int, int]]"; expected "Callable[[tuple[KT, VT]], tuple[VT, KT]]"  [arg-type]
+ bidict/_iter.py:52:16: note: "itemgetter[tuple[int, int]].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"

flake8 (https://github.com/pycqa/flake8)
+ src/flake8/checker.py:186: error: Argument "key" to "sort" of "list" has incompatible type "itemgetter[int]"; expected "Callable[[tuple[str, list[tuple[str, int, int, str, str | None]], dict[str, int]]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]
+ src/flake8/checker.py:186: note: "itemgetter[int].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"
+ src/flake8/checker.py:188: error: Argument "key" to "sort" of "list" has incompatible type "itemgetter[tuple[int, int]]"; expected "Callable[[tuple[str, int, int, str, str | None]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]
+ src/flake8/checker.py:188: note: "itemgetter[tuple[int, int]].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"

core (https://github.com/home-assistant/core)
+ homeassistant/util/language.py:194: error: Argument "key" to "sorted" has incompatible type "itemgetter[int]"; expected "Callable[[tuple[Dialect, tuple[float, float], str]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]
+ homeassistant/util/language.py:194: note: "itemgetter[int].__call__" has type "Callable[[Arg(SupportsGetItem[Any, _T], 'obj')], _T]"

@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
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
help wanted An actionable problem of low to medium complexity where a PR would be very welcome
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants