| 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 |
"""Unit Tests for the builtins brain module.""" |
| 6 |
|
| 7 |
import unittest |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from astroid import nodes, objects, util |
| 12 |
from astroid.builder import _extract_single_node, extract_node |
| 13 |
|
| 14 |
|
| 15 |
class BuiltinsTest(unittest.TestCase): |
| 16 |
def test_infer_property(self): |
| 17 |
property_assign = _extract_single_node(""" |
| 18 |
class Something: |
| 19 |
def getter(): |
| 20 |
return 5 |
| 21 |
asd = property(getter) #@ |
| 22 |
""") |
| 23 |
inferred_property = next(iter(property_assign.value.infer())) |
| 24 |
self.assertTrue(isinstance(inferred_property, objects.Property)) |
| 25 |
class_parent = property_assign.scope() |
| 26 |
self.assertIsInstance(class_parent, nodes.ClassDef) |
| 27 |
self.assertFalse( |
| 28 |
any( |
| 29 |
isinstance(def_, objects.Property) |
| 30 |
for def_list in class_parent.locals.values() |
| 31 |
for def_ in def_list |
| 32 |
) |
| 33 |
) |
| 34 |
self.assertTrue(hasattr(inferred_property, "args")) |
| 35 |
|
| 36 |
|
| 37 |
class TestStringNodes: |
| 38 |
@pytest.mark.parametrize( |
| 39 |
"format_string", |
| 40 |
[ |
| 41 |
pytest.param( |
| 42 |
""""My name is {}, I'm {}".format("Daniel", 12)""", id="empty-indexes" |
| 43 |
), |
| 44 |
pytest.param( |
| 45 |
""""My name is {0}, I'm {1}".format("Daniel", 12)""", |
| 46 |
id="numbered-indexes", |
| 47 |
), |
| 48 |
pytest.param( |
| 49 |
""""My name is {fname}, I'm {age}".format(fname = "Daniel", age = 12)""", |
| 50 |
id="named-indexes", |
| 51 |
), |
| 52 |
pytest.param( |
| 53 |
""" |
| 54 |
name = "Daniel" |
| 55 |
age = 12 |
| 56 |
"My name is {0}, I'm {1}".format(name, age) |
| 57 |
""", |
| 58 |
id="numbered-indexes-from-positional", |
| 59 |
), |
| 60 |
pytest.param( |
| 61 |
""" |
| 62 |
name = "Daniel" |
| 63 |
age = 12 |
| 64 |
"My name is {fname}, I'm {age}".format(fname = name, age = age) |
| 65 |
""", |
| 66 |
id="named-indexes-from-keyword", |
| 67 |
), |
| 68 |
pytest.param( |
| 69 |
""" |
| 70 |
name = "Daniel" |
| 71 |
age = 12 |
| 72 |
"My name is {0}, I'm {age}".format(name, age = age) |
| 73 |
""", |
| 74 |
id="mixed-indexes-from-mixed", |
| 75 |
), |
| 76 |
pytest.param( |
| 77 |
""" |
| 78 |
string = "My name is {}, I'm {}" |
| 79 |
string.format("Daniel", 12) |
| 80 |
""", |
| 81 |
id="empty-indexes-on-variable", |
| 82 |
), |
| 83 |
], |
| 84 |
) |
| 85 |
def test_string_format(self, format_string: str) -> None: |
| 86 |
node: nodes.Call = _extract_single_node(format_string) |
| 87 |
inferred = next(node.infer()) |
| 88 |
assert isinstance(inferred, nodes.Const) |
| 89 |
assert inferred.value == "My name is Daniel, I'm 12" |
| 90 |
|
| 91 |
@pytest.mark.parametrize( |
| 92 |
"format_string", |
| 93 |
[ |
| 94 |
""" |
| 95 |
from missing import Unknown |
| 96 |
name = Unknown |
| 97 |
age = 12 |
| 98 |
"My name is {fname}, I'm {age}".format(fname = name, age = age) |
| 99 |
""", |
| 100 |
""" |
| 101 |
from missing import Unknown |
| 102 |
age = 12 |
| 103 |
"My name is {fname}, I'm {age}".format(fname = Unknown, age = age) |
| 104 |
""", |
| 105 |
""" |
| 106 |
from missing import Unknown |
| 107 |
"My name is {}, I'm {}".format(Unknown, 12) |
| 108 |
""", |
| 109 |
""""I am {}".format()""", |
| 110 |
""" |
| 111 |
"My name is {fname}, I'm {age}".format(fsname = "Daniel", age = 12) |
| 112 |
""", |
| 113 |
""" |
| 114 |
"My unicode character is {:c}".format(None) |
| 115 |
""", |
| 116 |
""" |
| 117 |
"My hex format is {:4x}".format('1') |
| 118 |
""", |
| 119 |
""" |
| 120 |
daniel_age = 12 |
| 121 |
"My name is {0.name}".format(daniel_age) |
| 122 |
""", |
| 123 |
pytest.param(""""{:>2000000000}".format("x")""", id="oversized-width"), |
| 124 |
pytest.param(""""{:.2000000000f}".format(1.0)""", id="oversized-precision"), |
| 125 |
pytest.param( |
| 126 |
""""{:>{}}".format("x", 2000000000)""", id="oversized-nested-width" |
| 127 |
), |
| 128 |
], |
| 129 |
) |
| 130 |
def test_string_format_uninferable(self, format_string: str) -> None: |
| 131 |
node: nodes.Call = _extract_single_node(format_string) |
| 132 |
inferred = next(node.infer()) |
| 133 |
assert inferred is util.Uninferable |
| 134 |
|
| 135 |
def test_string_format_with_specs(self) -> None: |
| 136 |
node: nodes.Call = _extract_single_node( |
| 137 |
""""My name is {}, I'm {:.2f}".format("Daniel", 12)""" |
| 138 |
) |
| 139 |
inferred = next(node.infer()) |
| 140 |
assert isinstance(inferred, nodes.Const) |
| 141 |
assert inferred.value == "My name is Daniel, I'm 12.00" |
| 142 |
|
| 143 |
def test_string_format_in_dataclass_pylint8109(self) -> None: |
| 144 |
"""https://github.com/pylint-dev/pylint/issues/8109""" |
| 145 |
function_def = extract_node(""" |
| 146 |
from dataclasses import dataclass |
| 147 |
|
| 148 |
@dataclass |
| 149 |
class Number: |
| 150 |
amount: int | float |
| 151 |
round: int = 2 |
| 152 |
|
| 153 |
def __str__(self): #@ |
| 154 |
number_format = "{:,.%sf}" % self.round |
| 155 |
return number_format.format(self.amount).rstrip("0").rstrip(".") |
| 156 |
""") |
| 157 |
inferit = function_def.infer_call_result(function_def, context=None) |
| 158 |
assert [a.name for a in inferit] == [util.Uninferable] |