Skip to content

Use TypeIs for various stdlib functions#11823

Merged
JelleZijlstra merged 3 commits intopython:mainfrom
JelleZijlstra:typeis
Apr 26, 2024
Merged

Use TypeIs for various stdlib functions#11823
JelleZijlstra merged 3 commits intopython:mainfrom
JelleZijlstra:typeis

Conversation

@JelleZijlstra
Copy link
Copy Markdown
Member

See #11453. This should start working when mypy 1.10 is released, which should be very soon.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Copy Markdown
Member Author

Primer analysis:

  • Lots of removed type ignores! That's a very positive sign.
  • pytest: this is a false positive but a pretty understandable one. It monkeypatches functools.partial to not be a class, and then checks inspect.isclass(functools.partial).
  • antidote: I think something is going wrong with the inspect.isfunction call here that causes mypy to narrow _kind down too much. Need to investigate more.

@srittau
Copy link
Copy Markdown
Collaborator

srittau commented Apr 24, 2024

Looks good to me. The mypy bug (if it turns out to be one) seems obscure enough that the improvements this brings are worth it.

@github-actions
Copy link
Copy Markdown
Contributor

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

vision (https://github.com/pytorch/vision)
+ torchvision/models/_api.py:128: error: Unused "type: ignore" comment  [unused-ignore]

antidote (https://github.com/Finistere/antidote)
+ src/antidote/lib/lazy_ext/_lazy.py:171: error: Non-overlapping identity check (left operand type: "Literal[FunctionKind.VALUE]", right operand type: "Literal[FunctionKind.METHOD]")  [comparison-overlap]
+ src/antidote/lib/lazy_ext/_lazy.py:174: error: Unused "type: ignore" comment  [unused-ignore]
+ src/antidote/lib/lazy_ext/_lazy.py:178: error: Non-overlapping identity check (left operand type: "Literal[FunctionKind.VALUE]", right operand type: "Literal[FunctionKind.PROPERTY]")  [comparison-overlap]
+ src/antidote/lib/lazy_ext/_lazy.py:181: error: Unused "type: ignore" comment  [unused-ignore]
+ src/antidote/lib/lazy_ext/_lazy.py:185: error: Non-overlapping identity check (left operand type: "Literal[FunctionKind.VALUE]", right operand type: "Literal[FunctionKind.FUNCTION]")  [comparison-overlap]
+ tests/lib/interface/test_function.py:44: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:45: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:78: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:79: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:83: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:86: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:89: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:145: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:154: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:174: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:188: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:191: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/lib/interface/test_function.py:194: error: Unused "type: ignore" comment  [unused-ignore]

pytest (https://github.com/pytest-dev/pytest)
+ testing/test_monkeypatch.py:418: error: Statement is unreachable  [unreachable]

spark (https://github.com/apache/spark)
+ python/pyspark/sql/worker/write_into_data_source.py:157: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/worker/write_into_data_source.py:164: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/worker/create_data_source.py:131: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/worker/create_data_source.py:156: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/worker/create_data_source.py:166: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/worker/create_data_source.py:168: error: Unused "type: ignore" comment  [unused-ignore]

jinja (https://github.com/pallets/jinja)
+ src/jinja2/async_utils.py:67: error: Redundant cast to "V"  [redundant-cast]

discord.py (https://github.com/Rapptz/discord.py)
+ discord/utils.py:695: error: Unused "type: ignore" comment  [unused-ignore]
+ discord/utils.py:701: error: Incompatible default for argument "check" (default has type "Callable[[object], TypeIs[Awaitable[Any]]]", argument has type "Callable[[T | Awaitable[T]], TypeGuard[Awaitable[T]]]")  [assignment]

@JelleZijlstra
Copy link
Copy Markdown
Member Author

I tracked it down to a preexisting mypy bug: python/mypy#8869. Given that it's a bug that is not directly related to TypeIs, I agree this is worth merging.

@JelleZijlstra JelleZijlstra marked this pull request as ready for review April 26, 2024 23:43
@JelleZijlstra JelleZijlstra merged commit 1dff589 into python:main Apr 26, 2024
@JelleZijlstra JelleZijlstra deleted the typeis branch April 26, 2024 23:43
@hauntsaninja
Copy link
Copy Markdown
Collaborator

hauntsaninja commented Apr 26, 2024

Hooray for the new semantics!

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.

3 participants