-
-
Notifications
You must be signed in to change notification settings - Fork 303
Support isinstance
constraints in inference
#2846
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
base: main
Are you sure you want to change the base?
Conversation
Codecov Reportβ Patch coverage is
β Your patch check has failed because the patch coverage (81.08%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #2846 +/- ##
==========================================
- Coverage 93.37% 93.35% -0.02%
==========================================
Files 92 92
Lines 11148 11170 +22
==========================================
+ Hits 10409 10428 +19
- Misses 739 742 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
π New features to boost your workflow:
|
Seems like test coverage is lacking. Shouldn't be an issue to get it up to 100%, but I would like a review first to see if this implementation makes sense. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful!
As we do more of these, it would be nice to add a CI step that installs pylint and runs the primer. I had an experiment (work in progress) at jacobtylerwalls#157 and
pylint-dev/pylint@main...jacobtylerwalls:pylint:run-with-custom-astroid. Something for later.
- negate=False: satisfied when inferred is an instance of the checked types. | ||
- negate=True: satisfied when inferred is not an instance of the checked types. | ||
""" | ||
if isinstance(inferred, util.UninferableBase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker, but would be nice to get a microbenchmark to prove whether we should be doing isinstance
or is
. I'm assuming is Uninferable
is faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> from astroid.util import UninferableBase
>>> from timeit import timeit
>>> uninf = UninferableBase()
>>> x = UninferableBase()
>>> timeit("x is uninf", globals=globals())
0.04412866701022722
>>> timeit("isinstance(x, UninferableBase)", globals=globals())
0.051591374998679385
assert inferred[0] is Uninferable | ||
|
||
|
||
def test_isinstance_supertype(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple ideas for test cases at your option:
- class A(B, C): pass;
isinstance(a(), C)
- diamond inheritance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobtylerwalls Should this be merged for 4.0.0 considering we already released a RC? Feels like a big change to include after a RC.
I wondered as well, happy to wait for 4.1. Let's release 4.0 final tomorrow in that case. |
Let's do so, especially as we haven't tested this with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat ! Glad to see that the constraint api is getting some love.
Type of Changes
Description
These changes implements the constraint interface to add a new constraint -
TypeConstraint
to improve the accuracy of inference for objects used in type-narrowing guards.Currently, the implementation only supports the
isinstance
pattern, but it should be relatively straightforward to extend it to support the negated case (not isinstance
) in the future.To determine whether an object is an instance of the given types, the implementation reuses existing inference mechanism for
isinstance
inbrains
.Refs pylint-dev/pylint#1162
Refs pylint-dev/pylint#4635
Refs pylint-dev/pylint#10469