Skip to content

Use TypeIs for various stdlib functions #11823

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

Merged
merged 3 commits into from
Apr 26, 2024
Merged

Conversation

JelleZijlstra
Copy link
Member

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

This comment has been minimized.

This comment has been minimized.

@JelleZijlstra
Copy link
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
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.

Copy link
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
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
54 checks passed
@JelleZijlstra JelleZijlstra deleted the typeis branch April 26, 2024 23:43
@hauntsaninja
Copy link
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