[mypyc] feat: specialize isinstance for tuple of primitive types#19949
[mypyc] feat: specialize isinstance for tuple of primitive types#19949JukkaL merged 14 commits intopython:masterfrom
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
…obTheBuidler/mypy into isinstance-tuple-of-primitives
|
Not really sure where a run test best fits. Maybe the IR test is straightforward enough? |
JukkaL
left a comment
There was a problem hiding this comment.
What about adding a run test to run-misc.test?
| from typing import Any | ||
|
|
||
| def is_instance(x: Any) -> bool: | ||
| return isinstance(x, (str, int, bytes)) |
There was a problem hiding this comment.
Test also single-item tuple as an edge case.
|
Both of those just as run tests, or IR too? |
|
Added run tests for various cases. no additional IR tests at this time. |
JukkaL
left a comment
There was a problem hiding this comment.
Thanks for adding the tests! Looks mostly good now, just one thing remains.
mypyc/irbuild/specialize.py
Outdated
| is_last = i == len(descs) - 1 | ||
| next_block = fail_block if is_last else BasicBlock() | ||
| builder.add_bool_branch( | ||
| builder.primitive_op(desc, [obj], expr.line), pass_block, next_block # type: ignore [arg-type] |
There was a problem hiding this comment.
Can you avoid the # type: ignore? If there is some typing issue, I'd prefer a cast since it's more specific and tells which argument is the problematic one.
There was a problem hiding this comment.
Done. It was because descs has type list[PrimitiveDescription | None]
I'm assuming the if None in descs: return None can probably be considered as a TypeGuard of sorts by mypy but that's above my paygrade
for more information, see https://pre-commit.ci
| is_last = i == len(descs) - 1 | ||
| next_block = fail_block if is_last else BasicBlock() | ||
| builder.add_bool_branch( | ||
| builder.primitive_op(cast(PrimitiveDescription, desc), [obj], expr.line), |
There was a problem hiding this comment.
Here a slightly better way to narrow the type would be to use assert desc is not None before the call, but this is fine as well.
This PR specializes isinstance calls where the type argument is a tuple of primitive types.
We can skip tuple creation and the associated refcounting, and daisy-chain the primitive checks with an early exit option at each step.