Skip to content

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Oct 4, 2025

Type of Changes

Type
πŸ› Bug fix
βœ“ ✨ New feature
πŸ”¨ Refactoring
πŸ“œ Docs

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 in brains.

Refs pylint-dev/pylint#1162
Refs pylint-dev/pylint#4635
Refs pylint-dev/pylint#10469

@zenlyj zenlyj added Enhancement ✨ Improvement to a component inference labels Oct 4, 2025
Copy link

codecov bot commented Oct 4, 2025

Codecov Report

❌ Patch coverage is 81.08108% with 7 lines in your changes missing coverage. Please review.
βœ… Project coverage is 93.35%. Comparing base (0562ce3) to head (e5217a2).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
astroid/helpers.py 66.66% 4 Missing ⚠️
astroid/constraint.py 86.95% 3 Missing ⚠️

❌ 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

Impacted file tree graph

@@            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     
Flag Coverage Ξ”
linux 93.22% <81.08%> (-0.02%) ⬇️
pypy 93.35% <81.08%> (-0.02%) ⬇️
windows 93.33% <81.08%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Ξ”
astroid/brain/brain_builtin_inference.py 92.68% <100.00%> (+0.65%) ⬆️
astroid/constraint.py 97.08% <86.95%> (-2.92%) ⬇️
astroid/helpers.py 93.20% <66.66%> (-2.13%) ⬇️
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zenlyj
Copy link
Contributor Author

zenlyj commented Oct 4, 2025

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.

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a 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):
Copy link
Member

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.

Copy link
Member

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():
Copy link
Member

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

@jacobtylerwalls jacobtylerwalls added this to the 4.0.0 milestone Oct 4, 2025
Copy link
Collaborator

@DanielNoord DanielNoord left a 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.

@jacobtylerwalls
Copy link
Member

I wondered as well, happy to wait for 4.1. Let's release 4.0 final tomorrow in that case.

@DanielNoord
Copy link
Collaborator

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 pylint yet. 4.0.0 has enough new features already :)

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 4.0.0, 4.1.0 Oct 5, 2025
@Pierre-Sassoulas Pierre-Sassoulas added the Blocked 🚧 A PR or issue blocked by another PR or issue label Oct 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked 🚧 A PR or issue blocked by another PR or issue Enhancement ✨ Improvement to a component inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants