| 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 |
from __future__ import annotations |
| 6 |
|
| 7 |
import unittest |
| 8 |
|
| 9 |
import astroid |
| 10 |
from astroid import exceptions, nodes |
| 11 |
|
| 12 |
try: |
| 13 |
import attr # type: ignore[import] # pylint: disable=unused-import |
| 14 |
|
| 15 |
HAS_ATTR = True |
| 16 |
except ImportError: |
| 17 |
HAS_ATTR = False |
| 18 |
|
| 19 |
|
| 20 |
@unittest.skipUnless(HAS_ATTR, "These tests require the attr library") |
| 21 |
class AttrsTest(unittest.TestCase): |
| 22 |
def test_attr_transform(self) -> None: |
| 23 |
module = astroid.parse(""" |
| 24 |
import attr |
| 25 |
from attr import attrs, attrib, field |
| 26 |
|
| 27 |
@attr.s |
| 28 |
class Foo: |
| 29 |
|
| 30 |
d = attr.ib(attr.Factory(dict)) |
| 31 |
|
| 32 |
f = Foo() |
| 33 |
f.d['answer'] = 42 |
| 34 |
|
| 35 |
@attr.s(slots=True) |
| 36 |
class Bar: |
| 37 |
d = attr.ib(attr.Factory(dict)) |
| 38 |
|
| 39 |
g = Bar() |
| 40 |
g.d['answer'] = 42 |
| 41 |
|
| 42 |
@attrs |
| 43 |
class Bah: |
| 44 |
d = attrib(attr.Factory(dict)) |
| 45 |
|
| 46 |
h = Bah() |
| 47 |
h.d['answer'] = 42 |
| 48 |
|
| 49 |
@attr.attrs |
| 50 |
class Bai: |
| 51 |
d = attr.attrib(attr.Factory(dict)) |
| 52 |
|
| 53 |
i = Bai() |
| 54 |
i.d['answer'] = 42 |
| 55 |
|
| 56 |
@attr.define |
| 57 |
class Spam: |
| 58 |
d = field(default=attr.Factory(dict)) |
| 59 |
|
| 60 |
j = Spam(d=1) |
| 61 |
j.d['answer'] = 42 |
| 62 |
|
| 63 |
@attr.mutable |
| 64 |
class Eggs: |
| 65 |
d = attr.field(default=attr.Factory(dict)) |
| 66 |
|
| 67 |
k = Eggs(d=1) |
| 68 |
k.d['answer'] = 42 |
| 69 |
|
| 70 |
@attr.frozen |
| 71 |
class Eggs: |
| 72 |
d = attr.field(default=attr.Factory(dict)) |
| 73 |
|
| 74 |
l = Eggs(d=1) |
| 75 |
l.d['answer'] = 42 |
| 76 |
|
| 77 |
@attr.attrs(auto_attribs=True) |
| 78 |
class Eggs: |
| 79 |
d: int = attr.Factory(lambda: 3) |
| 80 |
|
| 81 |
m = Eggs(d=1) |
| 82 |
""") |
| 83 |
|
| 84 |
for name in ("f", "g", "h", "i", "j", "k", "l", "m"): |
| 85 |
should_be_unknown = next(module.getattr(name)[0].infer()).getattr("d")[0] |
| 86 |
self.assertIsInstance(should_be_unknown, nodes.Unknown) |
| 87 |
|
| 88 |
def test_attrs_transform(self) -> None: |
| 89 |
"""Test brain for decorators of the 'attrs' package. |
| 90 |
|
| 91 |
Package added support for 'attrs' alongside 'attr' in v21.3.0. |
| 92 |
See: https://github.com/python-attrs/attrs/releases/tag/21.3.0 |
| 93 |
""" |
| 94 |
module = astroid.parse(""" |
| 95 |
import attrs |
| 96 |
from attrs import field, mutable, frozen, define |
| 97 |
from attrs import mutable as my_mutable |
| 98 |
|
| 99 |
@attrs.define |
| 100 |
class Foo: |
| 101 |
|
| 102 |
d = attrs.field(attrs.Factory(dict)) |
| 103 |
|
| 104 |
f = Foo() |
| 105 |
f.d['answer'] = 42 |
| 106 |
|
| 107 |
@attrs.define(slots=True) |
| 108 |
class Bar: |
| 109 |
d = field(attrs.Factory(dict)) |
| 110 |
|
| 111 |
g = Bar() |
| 112 |
g.d['answer'] = 42 |
| 113 |
|
| 114 |
@attrs.mutable |
| 115 |
class Bah: |
| 116 |
d = field(attrs.Factory(dict)) |
| 117 |
|
| 118 |
h = Bah() |
| 119 |
h.d['answer'] = 42 |
| 120 |
|
| 121 |
@attrs.frozen |
| 122 |
class Bai: |
| 123 |
d = attrs.field(attrs.Factory(dict)) |
| 124 |
|
| 125 |
i = Bai() |
| 126 |
i.d['answer'] = 42 |
| 127 |
|
| 128 |
@attrs.define |
| 129 |
class Spam: |
| 130 |
d = field(default=attrs.Factory(dict)) |
| 131 |
|
| 132 |
j = Spam(d=1) |
| 133 |
j.d['answer'] = 42 |
| 134 |
|
| 135 |
@attrs.mutable |
| 136 |
class Eggs: |
| 137 |
d = attrs.field(default=attrs.Factory(dict)) |
| 138 |
|
| 139 |
k = Eggs(d=1) |
| 140 |
k.d['answer'] = 42 |
| 141 |
|
| 142 |
@attrs.frozen |
| 143 |
class Eggs: |
| 144 |
d = attrs.field(default=attrs.Factory(dict)) |
| 145 |
|
| 146 |
l = Eggs(d=1) |
| 147 |
l.d['answer'] = 42 |
| 148 |
|
| 149 |
|
| 150 |
@frozen |
| 151 |
class Legs: |
| 152 |
d = attrs.field(default=attrs.Factory(dict)) |
| 153 |
""") |
| 154 |
|
| 155 |
for name in ("f", "g", "h", "i", "j", "k", "l"): |
| 156 |
should_be_unknown = next(module.getattr(name)[0].infer()).getattr("d")[0] |
| 157 |
self.assertIsInstance(should_be_unknown, nodes.Unknown, name) |
| 158 |
|
| 159 |
def test_special_attributes(self) -> None: |
| 160 |
"""Make sure special attrs attributes exist""" |
| 161 |
|
| 162 |
code = """ |
| 163 |
import attr |
| 164 |
|
| 165 |
@attr.s |
| 166 |
class Foo: |
| 167 |
pass |
| 168 |
Foo() |
| 169 |
""" |
| 170 |
foo_inst = next(astroid.extract_node(code).infer()) |
| 171 |
[attr_node] = foo_inst.getattr("__attrs_attrs__") |
| 172 |
# Prevents https://github.com/pylint-dev/pylint/issues/1884 |
| 173 |
assert isinstance(attr_node, nodes.Unknown) |
| 174 |
|
| 175 |
def test_dont_consider_assignments_but_without_attrs(self) -> None: |
| 176 |
code = """ |
| 177 |
import attr |
| 178 |
|
| 179 |
class Cls: pass |
| 180 |
@attr.s |
| 181 |
class Foo: |
| 182 |
temp = Cls() |
| 183 |
temp.prop = 5 |
| 184 |
bar_thing = attr.ib(default=temp) |
| 185 |
Foo() |
| 186 |
""" |
| 187 |
next(astroid.extract_node(code).infer()) |
| 188 |
|
| 189 |
def test_attrs_with_annotation(self) -> None: |
| 190 |
code = """ |
| 191 |
import attr |
| 192 |
|
| 193 |
@attr.s |
| 194 |
class Foo: |
| 195 |
bar: int = attr.ib(default=5) |
| 196 |
Foo() |
| 197 |
""" |
| 198 |
should_be_unknown = next(astroid.extract_node(code).infer()).getattr("bar")[0] |
| 199 |
self.assertIsInstance(should_be_unknown, nodes.Unknown) |
| 200 |
|
| 201 |
def test_attr_with_only_annotation_fails(self) -> None: |
| 202 |
code = """ |
| 203 |
import attr |
| 204 |
|
| 205 |
@attr.s |
| 206 |
class Foo: |
| 207 |
bar: int |
| 208 |
Foo() |
| 209 |
""" |
| 210 |
with self.assertRaises(exceptions.AttributeInferenceError): |
| 211 |
next(astroid.extract_node(code).infer()).getattr("bar") |
| 212 |
|
| 213 |
def test_attrs_with_only_annotation_works(self) -> None: |
| 214 |
code = """ |
| 215 |
import attrs |
| 216 |
|
| 217 |
@attrs.define |
| 218 |
class Foo: |
| 219 |
bar: int |
| 220 |
baz: str = "hello" |
| 221 |
Foo(1) |
| 222 |
""" |
| 223 |
for attr_name in ("bar", "baz"): |
| 224 |
should_be_unknown = next(astroid.extract_node(code).infer()).getattr( |
| 225 |
attr_name |
| 226 |
)[0] |
| 227 |
self.assertIsInstance(should_be_unknown, nodes.Unknown) |
| 228 |
|
| 229 |
def test_attrs_with_class_var_annotation(self) -> None: |
| 230 |
cases = { |
| 231 |
"with-subscript": """ |
| 232 |
import attrs |
| 233 |
from typing import ClassVar |
| 234 |
|
| 235 |
@attrs.define |
| 236 |
class Foo: |
| 237 |
bar: ClassVar[int] = 1 |
| 238 |
Foo() |
| 239 |
""", |
| 240 |
"no-subscript": """ |
| 241 |
import attrs |
| 242 |
from typing import ClassVar |
| 243 |
|
| 244 |
@attrs.define |
| 245 |
class Foo: |
| 246 |
bar: ClassVar = 1 |
| 247 |
Foo() |
| 248 |
""", |
| 249 |
} |
| 250 |
|
| 251 |
for name, code in cases.items(): |
| 252 |
with self.subTest(case=name): |
| 253 |
instance = next(astroid.extract_node(code).infer()) |
| 254 |
self.assertIsInstance(instance.getattr("bar")[0], nodes.AssignName) |
| 255 |
self.assertNotIn("bar", instance.instance_attrs) |
| 256 |
|
| 257 |
def test_attrs_without_class_var_annotation(self) -> None: |
| 258 |
cases = { |
| 259 |
"wrong-name": """ |
| 260 |
import attrs |
| 261 |
from typing import Final |
| 262 |
|
| 263 |
@attrs.define |
| 264 |
class Foo: |
| 265 |
bar: Final[int] = 1 |
| 266 |
Foo() |
| 267 |
""", |
| 268 |
"classvar-not-outermost": """ |
| 269 |
import attrs |
| 270 |
from typing import ClassVar |
| 271 |
|
| 272 |
@attrs.define |
| 273 |
class Foo: |
| 274 |
bar: list[ClassVar[int]] = [] |
| 275 |
Foo() |
| 276 |
""", |
| 277 |
} |
| 278 |
|
| 279 |
for name, code in cases.items(): |
| 280 |
with self.subTest(case=name): |
| 281 |
instance = next(astroid.extract_node(code).infer()) |
| 282 |
self.assertIsInstance(instance.getattr("bar")[0], nodes.Unknown) |
| 283 |
self.assertIn("bar", instance.instance_attrs) |