File: test_statistics.py

package info (click to toggle)
astroid 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: python: 38,560; makefile: 24
file content (68 lines) | stat: -rw-r--r-- 2,265 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

"""Tests for brain statistics module."""

from __future__ import annotations

import unittest

from astroid import extract_node
from astroid.util import Uninferable


class StatisticsBrainTest(unittest.TestCase):
    """Test the brain statistics module functionality."""

    def test_statistics_quantiles_inference(self) -> None:
        """Test that statistics.quantiles() returns Uninferable instead of empty list."""
        node = extract_node(
            """
        import statistics
        statistics.quantiles(list(range(100)), n=4)  #@
        """
        )
        inferred = list(node.infer())
        self.assertEqual(len(inferred), 1)
        self.assertIs(inferred[0], Uninferable)

    def test_statistics_quantiles_different_args(self) -> None:
        """Test statistics.quantiles with different arguments."""
        node = extract_node(
            """
        import statistics
        statistics.quantiles([1, 2, 3, 4, 5], n=10, method='inclusive')  #@
        """
        )
        inferred = list(node.infer())
        self.assertEqual(len(inferred), 1)
        self.assertIs(inferred[0], Uninferable)

    def test_statistics_quantiles_assignment_unpacking(self) -> None:
        """Test the specific case that was causing false positives."""
        node = extract_node(
            """
        import statistics
        q1, q2, q3 = statistics.quantiles(list(range(100)), n=4)  #@
        """
        )
        call_node = node.value
        inferred = list(call_node.infer())
        self.assertEqual(len(inferred), 1)
        self.assertIs(inferred[0], Uninferable)

    def test_other_statistics_functions_not_affected(self) -> None:
        """Test that other statistics functions are not affected by our brain module."""
        node = extract_node(
            """
        import statistics
        statistics.mean([1, 2, 3, 4, 5])  #@
        """
        )
        inferred = list(node.infer())
        self.assertNotEqual(len(inferred), 0)


if __name__ == "__main__":
    unittest.main()