Skip to content

gh-131798: JIT: Narrow the return type of isinstance for some known arguments #133172

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

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

Conversation

tomasr8
Copy link
Member

@tomasr8 tomasr8 commented Apr 29, 2025

In this PR:

  • narrows isintance(obj, cls) to True if obj is a known type and cls is a known class and obj is a subclass of cls (and vice versa for False)
  • in all other cases narrows isinstance to bool.

Brandt also suggested adding an optimization for tuples which I'd like to add in a followup in order to keep the sizes of the individual PRs smaller. Though if you prefer to have it in one PR I can do it as well :)

// isinstance(obj, cls) where both obj and cls have known types
// We can deduce either True or False
PyTypeObject *inst_type = sym_get_type(inst_sym);
if (sym_matches_type(inst_sym, cls) || PyType_IsSubtype(inst_type, cls)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This simulates PyObject_TypeCheck

@@ -886,6 +886,44 @@ dummy_func(void) {
}
}

op(_CALL_ISINSTANCE, (callable, self_or_null, args[oparg] -- res)) {
if (sym_is_null(self_or_null) || sym_is_not_null(self_or_null)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I've seen this guard used elsewhere with self_or_null but it's not clear to me whether it is needed here as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant