|
| 1 | +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 | +# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 | +# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 | + |
| 5 | +"""Astroid hooks for understanding statistics library module. |
| 6 | +
|
| 7 | +Provides inference improvements for statistics module functions that have |
| 8 | +complex runtime behavior difficult to analyze statically. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from collections.abc import Iterator |
| 14 | +from typing import TYPE_CHECKING |
| 15 | + |
| 16 | +from astroid.context import InferenceContext |
| 17 | +from astroid.inference_tip import inference_tip |
| 18 | +from astroid.manager import AstroidManager |
| 19 | +from astroid.nodes.node_classes import Attribute, Call, ImportFrom, Name |
| 20 | +from astroid.util import Uninferable |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from astroid.typing import InferenceResult |
| 24 | + |
| 25 | + |
| 26 | +def _looks_like_statistics_quantiles(node: Call) -> bool: |
| 27 | + """Check if this is a call to statistics.quantiles.""" |
| 28 | + # Case 1: statistics.quantiles(...) |
| 29 | + if isinstance(node.func, Attribute): |
| 30 | + if node.func.attrname != "quantiles": |
| 31 | + return False |
| 32 | + if isinstance(node.func.expr, Name): |
| 33 | + if node.func.expr.name == "statistics": |
| 34 | + return True |
| 35 | + |
| 36 | + # Case 2: from statistics import quantiles; quantiles(...) |
| 37 | + if isinstance(node.func, Name) and node.func.name == "quantiles": |
| 38 | + # Check if quantiles was imported from statistics |
| 39 | + try: |
| 40 | + frame = node.frame() |
| 41 | + if "quantiles" in frame.locals: |
| 42 | + # Look for import from statistics |
| 43 | + for stmt in frame.body: |
| 44 | + if ( |
| 45 | + isinstance(stmt, ImportFrom) |
| 46 | + and stmt.modname == "statistics" |
| 47 | + and any(name[0] == "quantiles" for name in stmt.names or []) |
| 48 | + ): |
| 49 | + return True |
| 50 | + except (AttributeError, TypeError): |
| 51 | + # If we can't determine the import context, be conservative |
| 52 | + pass |
| 53 | + |
| 54 | + return False |
| 55 | + |
| 56 | + |
| 57 | +def infer_statistics_quantiles( |
| 58 | + node: Call, context: InferenceContext | None = None |
| 59 | +) -> Iterator[InferenceResult]: |
| 60 | + """Infer the result of statistics.quantiles() calls. |
| 61 | +
|
| 62 | + Returns Uninferable because quantiles() has complex runtime behavior |
| 63 | + that cannot be statically analyzed, preventing false positives in |
| 64 | + pylint's unbalanced-tuple-unpacking checker. |
| 65 | +
|
| 66 | + statistics.quantiles() returns a list with (n-1) elements, but static |
| 67 | + analysis sees only the empty list initializations in the function body. |
| 68 | + """ |
| 69 | + yield Uninferable |
| 70 | + |
| 71 | + |
| 72 | +def register(manager: AstroidManager) -> None: |
| 73 | + """Register statistics-specific inference improvements.""" |
| 74 | + manager.register_transform( |
| 75 | + Call, |
| 76 | + inference_tip(infer_statistics_quantiles), |
| 77 | + _looks_like_statistics_quantiles, |
| 78 | + ) |
0 commit comments