Skip to content

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Sep 19, 2025

Type of Changes

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

Description

This PR builds on #1189 by implementing the constraint interface to handle boolean constraints, similar to how None constraints are currently treated.

Previously, constraints only accounted for None checks:

x = 1

if x is None:
    x    # Uninferable, `1` is filtered out because x is not None

These changes support constraints derived from boolean truthiness:

  1. If condition is True, keep the inferred value.
x = 1    # truthy

if x:
    x    # inferred as `1`
  1. If condition is False, filter out the inferred value.
x = []    # falsy

if x:
    x    # Uninferable, `[]` filtered out
  1. Negation rules are the same as None constraints.
x = 1

if x:
    pass
else:
    x    # Uninferable, `1` filtered out


y = []

if not y:
    y    # inferred as `[]`
  1. If the boolean value cannot be inferred, assume constraint is satisfied.

Closes pylint-dev/pylint#9515

Copy link

codecov bot commented Sep 19, 2025

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 93.35%. Comparing base (90188b4) to head (04eb2cb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2823   +/-   ##
=======================================
  Coverage   93.35%   93.35%           
=======================================
  Files          91       91           
  Lines       11100    11113   +13     
=======================================
+ Hits        10362    10375   +13     
  Misses        738      738           
Flag Coverage Ξ”
linux 93.22% <100.00%> (+<0.01%) ⬆️
pypy 93.35% <100.00%> (+<0.01%) ⬆️
windows 93.34% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Ξ”
astroid/constraint.py 100.00% <100.00%> (ΓΈ)
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zenlyj zenlyj added inference Enhancement ✨ Improvement to a component labels Sep 19, 2025
@zenlyj zenlyj changed the title Extend constraint interface to include boolean constraint Implement boolean constraints with constraint interface Sep 19, 2025
@jacobtylerwalls jacobtylerwalls self-requested a review September 21, 2025 18:20
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.

Looks amazing !

DanielNoord
DanielNoord previously approved these changes Sep 21, 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.

Perhaps we should add a Changelog entry? Rest LGTM!

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.

Lovely, I'm glad more knowledge of the constraints interface is starting to spread πŸͺ

@jacobtylerwalls jacobtylerwalls added this to the 4.0.0 milestone Sep 21, 2025
@zenlyj
Copy link
Contributor Author

zenlyj commented Sep 22, 2025

Thanks for the reviews! A lot of thought and effort went into the constraints interface, and it deserves to be built upon rather than left unused. More extensions coming soon in separate PRs!

@zenlyj zenlyj changed the title Implement boolean constraints with constraint interface Support boolean truthiness constraints in inference Sep 22, 2025
@jacobtylerwalls jacobtylerwalls merged commit ea3772e into pylint-dev:main Sep 22, 2025
19 checks passed
@zenlyj zenlyj deleted the boolean-constraint branch September 22, 2025 14:22
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.

[unsubscriptable-object] False Positive on Optional list
4 participants