| 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 |
"""Tests for brain statistics module.""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import unittest |
| 10 |
|
| 11 |
from astroid import extract_node |
| 12 |
from astroid.util import Uninferable |
| 13 |
|
| 14 |
|
| 15 |
class StatisticsBrainTest(unittest.TestCase): |
| 16 |
"""Test the brain statistics module functionality.""" |
| 17 |
|
| 18 |
def test_statistics_quantiles_inference(self) -> None: |
| 19 |
"""Test that statistics.quantiles() returns Uninferable instead of empty list.""" |
| 20 |
node = extract_node(""" |
| 21 |
import statistics |
| 22 |
statistics.quantiles(list(range(100)), n=4) #@ |
| 23 |
""") |
| 24 |
inferred = list(node.infer()) |
| 25 |
self.assertEqual(len(inferred), 1) |
| 26 |
self.assertIs(inferred[0], Uninferable) |
| 27 |
|
| 28 |
def test_statistics_quantiles_different_args(self) -> None: |
| 29 |
"""Test statistics.quantiles with different arguments.""" |
| 30 |
node = extract_node(""" |
| 31 |
import statistics |
| 32 |
statistics.quantiles([1, 2, 3, 4, 5], n=10, method='inclusive') #@ |
| 33 |
""") |
| 34 |
inferred = list(node.infer()) |
| 35 |
self.assertEqual(len(inferred), 1) |
| 36 |
self.assertIs(inferred[0], Uninferable) |
| 37 |
|
| 38 |
def test_statistics_quantiles_assignment_unpacking(self) -> None: |
| 39 |
"""Test the specific case that was causing false positives.""" |
| 40 |
node = extract_node(""" |
| 41 |
import statistics |
| 42 |
q1, q2, q3 = statistics.quantiles(list(range(100)), n=4) #@ |
| 43 |
""") |
| 44 |
call_node = node.value |
| 45 |
inferred = list(call_node.infer()) |
| 46 |
self.assertEqual(len(inferred), 1) |
| 47 |
self.assertIs(inferred[0], Uninferable) |
| 48 |
|
| 49 |
def test_other_statistics_functions_not_affected(self) -> None: |
| 50 |
"""Test that other statistics functions are not affected by our brain module.""" |
| 51 |
node = extract_node(""" |
| 52 |
import statistics |
| 53 |
statistics.mean([1, 2, 3, 4, 5]) #@ |
| 54 |
""") |
| 55 |
inferred = list(node.infer()) |
| 56 |
self.assertNotEqual(len(inferred), 0) |
| 57 |
|
| 58 |
|
| 59 |
if __name__ == "__main__": |
| 60 |
unittest.main() |