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
@codecov
Copy link

codecov bot commented Oct 4, 2025

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 93.39%. Comparing base (4a7e23f) to head (3bcec6b).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2846      +/-   ##
==========================================
+ Coverage   93.35%   93.39%   +0.04%     
==========================================
  Files          92       92              
  Lines       11190    11208      +18     
==========================================
+ Hits        10446    10468      +22     
+ Misses        744      740       -4     
Flag Coverage Ξ”
linux 93.26% <100.00%> (+0.04%) ⬆️
pypy 93.39% <100.00%> (+0.04%) ⬆️
windows 93.37% <100.00%> (+0.04%) ⬆️

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 100.00% <100.00%> (ΓΈ)
astroid/helpers.py 95.56% <100.00%> (+0.23%) ⬆️
πŸš€ 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.

jacobtylerwalls
jacobtylerwalls previously approved these changes Oct 4, 2025
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.

@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
@jacobtylerwalls jacobtylerwalls removed the Blocked 🚧 A PR or issue blocked by another PR or issue label Nov 15, 2025
@jacobtylerwalls
Copy link
Member

Hey @zenlyj πŸ‘‹

I think this is mergeable. What do we need for the pylint job to pass, do we need a separate PR to unblock, or is it just a rebase?

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Nov 15, 2025

This pylint issue I fixed on main then forgot I fixed it and fixed it again on maintenance 2 week later... so I can tell you this is just a rebase πŸ˜„ (Need 325ebf6)

@jacobtylerwalls
Copy link
Member

In that case, I'll just pull it in πŸ˜‰

@jacobtylerwalls jacobtylerwalls merged commit 9108781 into pylint-dev:main Nov 15, 2025
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement ✨ Improvement to a component inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants