Skip to content

Conversation

emmanuel-ferdman
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

My first PR to astroid - hope the PR was implemented correctly.

This PR adds a new brain module brain_statistics.py that improves inference for statistics.quantiles() function calls. The statistics.quantiles() function has complex runtime behavior that returns lists with sizes dependent on the n parameter (specifically n-1 elements), but static analysis can only see the empty list initializations (result = []) in the function body and cannot follow the control flow that populates these lists. This causes astroid to infer empty lists, leading to false positives in pylint's unbalanced-tuple-unpacking checker. The brain module detects calls to statistics.quantiles() and returns Uninferable instead, allowing downstream tools to handle the "unknown size" case appropriately rather than acting on incorrect inference results.

Example:

import statistics

# This should work - statistics.quantiles(data, n=4) returns 3 quantiles
q1, q2, q3 = statistics.quantiles(list(range(100)), n=4)

Relevant Pylint issue: pylint-dev/pylint#10317

@Pierre-Sassoulas Pierre-Sassoulas added the Brain 🧠 Needs a brain tip label Sep 25, 2025
@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.0.0 milestone Sep 25, 2025
Copy link

codecov bot commented Sep 25, 2025

Codecov Report

❌ Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.
βœ… Project coverage is 93.36%. Comparing base (7c3b481) to head (558d070).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
astroid/brain/brain_statistics.py 93.10% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2832   +/-   ##
=======================================
  Coverage   93.36%   93.36%           
=======================================
  Files          91       92    +1     
  Lines       11115    11145   +30     
=======================================
+ Hits        10377    10406   +29     
- Misses        738      739    +1     
Flag Coverage Ξ”
linux 93.23% <93.33%> (+<0.01%) ⬆️
pypy 93.36% <93.33%> (+<0.01%) ⬆️
windows 93.35% <93.33%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Ξ”
astroid/brain/helpers.py 100.00% <100.00%> (ΓΈ)
astroid/brain/brain_statistics.py 93.10% <93.10%> (ΓΈ)

... and 1 file with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Nice addition!

Tests LGTM, only a comment about the use of hasattr

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.

Thanks!

@DanielNoord DanielNoord merged commit 3db2bd9 into pylint-dev:main Sep 26, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Brain 🧠 Needs a brain tip
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants