| 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 |
from astroid import bases, builder, nodes, objects, util |
| 10 |
from astroid.exceptions import AttributeInferenceError, InferenceError, SuperError |
| 11 |
from astroid.objects import Super |
| 12 |
|
| 13 |
|
| 14 |
class ObjectsTest(unittest.TestCase): |
| 15 |
def test_frozenset(self) -> None: |
| 16 |
node = builder.extract_node(""" |
| 17 |
frozenset({1: 2, 2: 3}) #@ |
| 18 |
""") |
| 19 |
inferred = next(node.infer()) |
| 20 |
self.assertIsInstance(inferred, objects.FrozenSet) |
| 21 |
|
| 22 |
self.assertEqual(inferred.pytype(), "builtins.frozenset") |
| 23 |
|
| 24 |
itered = inferred.itered() |
| 25 |
self.assertEqual(len(itered), 2) |
| 26 |
self.assertIsInstance(itered[0], nodes.Const) |
| 27 |
self.assertEqual([const.value for const in itered], [1, 2]) |
| 28 |
|
| 29 |
proxied = inferred._proxied |
| 30 |
self.assertEqual(inferred.qname(), "builtins.frozenset") |
| 31 |
self.assertIsInstance(proxied, nodes.ClassDef) |
| 32 |
|
| 33 |
def test_lookup_regression_slots(self) -> None: |
| 34 |
"""Regression test for attr__new__ of ObjectModel. |
| 35 |
|
| 36 |
ObjectModel._instance is not always an bases.Instance, so we can't |
| 37 |
rely on the ._proxied attribute of an Instance. |
| 38 |
""" |
| 39 |
|
| 40 |
node = builder.extract_node(""" |
| 41 |
class ClassHavingUnknownAncestors(Unknown): |
| 42 |
__slots__ = ["yo"] |
| 43 |
|
| 44 |
def test(self): |
| 45 |
self.not_yo = 42 |
| 46 |
""") |
| 47 |
assert node.getattr("__new__") |
| 48 |
|
| 49 |
|
| 50 |
class SuperTests(unittest.TestCase): |
| 51 |
def test_inferring_super_outside_methods(self) -> None: |
| 52 |
ast_nodes = builder.extract_node(""" |
| 53 |
class Module(object): |
| 54 |
pass |
| 55 |
class StaticMethod(object): |
| 56 |
@staticmethod |
| 57 |
def static(): |
| 58 |
# valid, but we don't bother with it. |
| 59 |
return super(StaticMethod, StaticMethod) #@ |
| 60 |
# super outside methods aren't inferred |
| 61 |
super(Module, Module) #@ |
| 62 |
# no argument super is not recognised outside methods as well. |
| 63 |
super() #@ |
| 64 |
""") |
| 65 |
assert isinstance(ast_nodes, list) |
| 66 |
in_static = next(ast_nodes[0].value.infer()) |
| 67 |
self.assertIsInstance(in_static, bases.Instance) |
| 68 |
self.assertEqual(in_static.qname(), "builtins.super") |
| 69 |
|
| 70 |
module_level = next(ast_nodes[1].infer()) |
| 71 |
self.assertIsInstance(module_level, bases.Instance) |
| 72 |
self.assertEqual(in_static.qname(), "builtins.super") |
| 73 |
|
| 74 |
no_arguments = next(ast_nodes[2].infer()) |
| 75 |
self.assertIsInstance(no_arguments, bases.Instance) |
| 76 |
self.assertEqual(no_arguments.qname(), "builtins.super") |
| 77 |
|
| 78 |
def test_inferring_unbound_super_doesnt_work(self) -> None: |
| 79 |
node = builder.extract_node(""" |
| 80 |
class Test(object): |
| 81 |
def __init__(self): |
| 82 |
super(Test) #@ |
| 83 |
""") |
| 84 |
unbounded = next(node.infer()) |
| 85 |
self.assertIsInstance(unbounded, bases.Instance) |
| 86 |
self.assertEqual(unbounded.qname(), "builtins.super") |
| 87 |
|
| 88 |
def test_use_default_inference_on_not_inferring_args(self) -> None: |
| 89 |
ast_nodes = builder.extract_node(""" |
| 90 |
class Test(object): |
| 91 |
def __init__(self): |
| 92 |
super(Lala, self) #@ |
| 93 |
super(Test, lala) #@ |
| 94 |
""") |
| 95 |
assert isinstance(ast_nodes, list) |
| 96 |
first = next(ast_nodes[0].infer()) |
| 97 |
self.assertIsInstance(first, bases.Instance) |
| 98 |
self.assertEqual(first.qname(), "builtins.super") |
| 99 |
|
| 100 |
second = next(ast_nodes[1].infer()) |
| 101 |
self.assertIsInstance(second, bases.Instance) |
| 102 |
self.assertEqual(second.qname(), "builtins.super") |
| 103 |
|
| 104 |
def test_no_arguments_super(self) -> None: |
| 105 |
ast_nodes = builder.extract_node(""" |
| 106 |
class First(object): pass |
| 107 |
class Second(First): |
| 108 |
def test(self): |
| 109 |
super() #@ |
| 110 |
@classmethod |
| 111 |
def test_classmethod(cls): |
| 112 |
super() #@ |
| 113 |
""") |
| 114 |
assert isinstance(ast_nodes, list) |
| 115 |
first = next(ast_nodes[0].infer()) |
| 116 |
self.assertIsInstance(first, objects.Super) |
| 117 |
self.assertIsInstance(first.type, bases.Instance) |
| 118 |
self.assertEqual(first.type.name, "Second") |
| 119 |
self.assertIsInstance(first.mro_pointer, nodes.ClassDef) |
| 120 |
self.assertEqual(first.mro_pointer.name, "Second") |
| 121 |
|
| 122 |
second = next(ast_nodes[1].infer()) |
| 123 |
self.assertIsInstance(second, objects.Super) |
| 124 |
self.assertIsInstance(second.type, nodes.ClassDef) |
| 125 |
self.assertEqual(second.type.name, "Second") |
| 126 |
self.assertIsInstance(second.mro_pointer, nodes.ClassDef) |
| 127 |
self.assertEqual(second.mro_pointer.name, "Second") |
| 128 |
|
| 129 |
def test_super_simple_cases(self) -> None: |
| 130 |
ast_nodes = builder.extract_node(""" |
| 131 |
class First(object): pass |
| 132 |
class Second(First): pass |
| 133 |
class Third(First): |
| 134 |
def test(self): |
| 135 |
super(Third, self) #@ |
| 136 |
super(Second, self) #@ |
| 137 |
|
| 138 |
# mro position and the type |
| 139 |
super(Third, Third) #@ |
| 140 |
super(Third, Second) #@ |
| 141 |
super(Fourth, Fourth) #@ |
| 142 |
|
| 143 |
class Fourth(Third): |
| 144 |
pass |
| 145 |
""") |
| 146 |
|
| 147 |
# .type is the object which provides the mro. |
| 148 |
# .mro_pointer is the position in the mro from where |
| 149 |
# the lookup should be done. |
| 150 |
|
| 151 |
# super(Third, self) |
| 152 |
assert isinstance(ast_nodes, list) |
| 153 |
first = next(ast_nodes[0].infer()) |
| 154 |
self.assertIsInstance(first, objects.Super) |
| 155 |
self.assertIsInstance(first.type, bases.Instance) |
| 156 |
self.assertEqual(first.type.name, "Third") |
| 157 |
self.assertIsInstance(first.mro_pointer, nodes.ClassDef) |
| 158 |
self.assertEqual(first.mro_pointer.name, "Third") |
| 159 |
|
| 160 |
# super(Second, self) |
| 161 |
second = next(ast_nodes[1].infer()) |
| 162 |
self.assertIsInstance(second, objects.Super) |
| 163 |
self.assertIsInstance(second.type, bases.Instance) |
| 164 |
self.assertEqual(second.type.name, "Third") |
| 165 |
self.assertIsInstance(first.mro_pointer, nodes.ClassDef) |
| 166 |
self.assertEqual(second.mro_pointer.name, "Second") |
| 167 |
|
| 168 |
# super(Third, Third) |
| 169 |
third = next(ast_nodes[2].infer()) |
| 170 |
self.assertIsInstance(third, objects.Super) |
| 171 |
self.assertIsInstance(third.type, nodes.ClassDef) |
| 172 |
self.assertEqual(third.type.name, "Third") |
| 173 |
self.assertIsInstance(third.mro_pointer, nodes.ClassDef) |
| 174 |
self.assertEqual(third.mro_pointer.name, "Third") |
| 175 |
|
| 176 |
# super(Third, second) |
| 177 |
fourth = next(ast_nodes[3].infer()) |
| 178 |
self.assertIsInstance(fourth, objects.Super) |
| 179 |
self.assertIsInstance(fourth.type, nodes.ClassDef) |
| 180 |
self.assertEqual(fourth.type.name, "Second") |
| 181 |
self.assertIsInstance(fourth.mro_pointer, nodes.ClassDef) |
| 182 |
self.assertEqual(fourth.mro_pointer.name, "Third") |
| 183 |
|
| 184 |
# Super(Fourth, Fourth) |
| 185 |
fifth = next(ast_nodes[4].infer()) |
| 186 |
self.assertIsInstance(fifth, objects.Super) |
| 187 |
self.assertIsInstance(fifth.type, nodes.ClassDef) |
| 188 |
self.assertEqual(fifth.type.name, "Fourth") |
| 189 |
self.assertIsInstance(fifth.mro_pointer, nodes.ClassDef) |
| 190 |
self.assertEqual(fifth.mro_pointer.name, "Fourth") |
| 191 |
|
| 192 |
def test_super_infer(self) -> None: |
| 193 |
node = builder.extract_node(""" |
| 194 |
class Super(object): |
| 195 |
def __init__(self): |
| 196 |
super(Super, self) #@ |
| 197 |
""") |
| 198 |
inferred = next(node.infer()) |
| 199 |
self.assertIsInstance(inferred, objects.Super) |
| 200 |
reinferred = next(inferred.infer()) |
| 201 |
self.assertIsInstance(reinferred, objects.Super) |
| 202 |
self.assertIs(inferred, reinferred) |
| 203 |
|
| 204 |
def test_inferring_invalid_supers(self) -> None: |
| 205 |
ast_nodes = builder.extract_node(""" |
| 206 |
class Super(object): |
| 207 |
def __init__(self): |
| 208 |
# MRO pointer is not a type |
| 209 |
super(1, self) #@ |
| 210 |
# MRO type is not a subtype |
| 211 |
super(Super, 1) #@ |
| 212 |
# self is not a subtype of Bupper |
| 213 |
super(Bupper, self) #@ |
| 214 |
class Bupper(Super): |
| 215 |
pass |
| 216 |
""") |
| 217 |
assert isinstance(ast_nodes, list) |
| 218 |
first = next(ast_nodes[0].infer()) |
| 219 |
self.assertIsInstance(first, objects.Super) |
| 220 |
with self.assertRaises(SuperError) as cm: |
| 221 |
first.super_mro() |
| 222 |
self.assertIsInstance(cm.exception.super_.mro_pointer, nodes.Const) |
| 223 |
self.assertEqual(cm.exception.super_.mro_pointer.value, 1) |
| 224 |
for node, invalid_type in zip(ast_nodes[1:], (nodes.Const, bases.Instance)): |
| 225 |
inferred = next(node.infer()) |
| 226 |
self.assertIsInstance(inferred, objects.Super, node) |
| 227 |
with self.assertRaises(SuperError) as cm: |
| 228 |
inferred.super_mro() |
| 229 |
self.assertIsInstance(cm.exception.super_.type, invalid_type) |
| 230 |
|
| 231 |
def test_proxied(self) -> None: |
| 232 |
node = builder.extract_node(""" |
| 233 |
class Super(object): |
| 234 |
def __init__(self): |
| 235 |
super(Super, self) #@ |
| 236 |
""") |
| 237 |
inferred = next(node.infer()) |
| 238 |
proxied = inferred._proxied |
| 239 |
self.assertEqual(proxied.qname(), "builtins.super") |
| 240 |
self.assertIsInstance(proxied, nodes.ClassDef) |
| 241 |
|
| 242 |
def test_super_bound_model(self) -> None: |
| 243 |
ast_nodes = builder.extract_node(""" |
| 244 |
class First(object): |
| 245 |
def method(self): |
| 246 |
pass |
| 247 |
@classmethod |
| 248 |
def class_method(cls): |
| 249 |
pass |
| 250 |
class Super_Type_Type(First): |
| 251 |
def method(self): |
| 252 |
super(Super_Type_Type, Super_Type_Type).method #@ |
| 253 |
super(Super_Type_Type, Super_Type_Type).class_method #@ |
| 254 |
@classmethod |
| 255 |
def class_method(cls): |
| 256 |
super(Super_Type_Type, Super_Type_Type).method #@ |
| 257 |
super(Super_Type_Type, Super_Type_Type).class_method #@ |
| 258 |
|
| 259 |
class Super_Type_Object(First): |
| 260 |
def method(self): |
| 261 |
super(Super_Type_Object, self).method #@ |
| 262 |
super(Super_Type_Object, self).class_method #@ |
| 263 |
""") |
| 264 |
# Super(type, type) is the same for both functions and classmethods. |
| 265 |
assert isinstance(ast_nodes, list) |
| 266 |
first = next(ast_nodes[0].infer()) |
| 267 |
self.assertIsInstance(first, nodes.FunctionDef) |
| 268 |
self.assertEqual(first.name, "method") |
| 269 |
|
| 270 |
second = next(ast_nodes[1].infer()) |
| 271 |
self.assertIsInstance(second, bases.BoundMethod) |
| 272 |
self.assertEqual(second.bound.name, "First") |
| 273 |
self.assertEqual(second.type, "classmethod") |
| 274 |
|
| 275 |
third = next(ast_nodes[2].infer()) |
| 276 |
self.assertIsInstance(third, nodes.FunctionDef) |
| 277 |
self.assertEqual(third.name, "method") |
| 278 |
|
| 279 |
fourth = next(ast_nodes[3].infer()) |
| 280 |
self.assertIsInstance(fourth, bases.BoundMethod) |
| 281 |
self.assertEqual(fourth.bound.name, "First") |
| 282 |
self.assertEqual(fourth.type, "classmethod") |
| 283 |
|
| 284 |
# Super(type, obj) can lead to different attribute bindings |
| 285 |
# depending on the type of the place where super was called. |
| 286 |
fifth = next(ast_nodes[4].infer()) |
| 287 |
self.assertIsInstance(fifth, bases.BoundMethod) |
| 288 |
self.assertEqual(fifth.bound.name, "First") |
| 289 |
self.assertEqual(fifth.type, "method") |
| 290 |
|
| 291 |
sixth = next(ast_nodes[5].infer()) |
| 292 |
self.assertIsInstance(sixth, bases.BoundMethod) |
| 293 |
self.assertEqual(sixth.bound.name, "First") |
| 294 |
self.assertEqual(sixth.type, "classmethod") |
| 295 |
|
| 296 |
def test_super_getattr_single_inheritance(self) -> None: |
| 297 |
ast_nodes = builder.extract_node(""" |
| 298 |
class First(object): |
| 299 |
def test(self): pass |
| 300 |
class Second(First): |
| 301 |
def test2(self): pass |
| 302 |
class Third(Second): |
| 303 |
test3 = 42 |
| 304 |
def __init__(self): |
| 305 |
super(Third, self).test2 #@ |
| 306 |
super(Third, self).test #@ |
| 307 |
# test3 is local, no MRO lookup is done. |
| 308 |
super(Third, self).test3 #@ |
| 309 |
super(Third, self) #@ |
| 310 |
|
| 311 |
# Unbounds. |
| 312 |
super(Third, Third).test2 #@ |
| 313 |
super(Third, Third).test #@ |
| 314 |
|
| 315 |
""") |
| 316 |
assert isinstance(ast_nodes, list) |
| 317 |
first = next(ast_nodes[0].infer()) |
| 318 |
self.assertIsInstance(first, bases.BoundMethod) |
| 319 |
self.assertEqual(first.bound.name, "Second") |
| 320 |
|
| 321 |
second = next(ast_nodes[1].infer()) |
| 322 |
self.assertIsInstance(second, bases.BoundMethod) |
| 323 |
self.assertEqual(second.bound.name, "First") |
| 324 |
|
| 325 |
with self.assertRaises(InferenceError): |
| 326 |
next(ast_nodes[2].infer()) |
| 327 |
fourth = next(ast_nodes[3].infer()) |
| 328 |
with self.assertRaises(AttributeInferenceError): |
| 329 |
fourth.getattr("test3") |
| 330 |
with self.assertRaises(AttributeInferenceError): |
| 331 |
next(fourth.igetattr("test3")) |
| 332 |
|
| 333 |
first_unbound = next(ast_nodes[4].infer()) |
| 334 |
self.assertIsInstance(first_unbound, nodes.FunctionDef) |
| 335 |
self.assertEqual(first_unbound.name, "test2") |
| 336 |
self.assertEqual(first_unbound.parent.name, "Second") |
| 337 |
|
| 338 |
second_unbound = next(ast_nodes[5].infer()) |
| 339 |
self.assertIsInstance(second_unbound, nodes.FunctionDef) |
| 340 |
self.assertEqual(second_unbound.name, "test") |
| 341 |
self.assertEqual(second_unbound.parent.name, "First") |
| 342 |
|
| 343 |
def test_super_invalid_mro(self) -> None: |
| 344 |
node = builder.extract_node(""" |
| 345 |
class A(object): |
| 346 |
test = 42 |
| 347 |
class Super(A, A): |
| 348 |
def __init__(self): |
| 349 |
super(Super, self) #@ |
| 350 |
""") |
| 351 |
inferred = next(node.infer()) |
| 352 |
with self.assertRaises(AttributeInferenceError): |
| 353 |
next(inferred.getattr("test")) |
| 354 |
|
| 355 |
def test_super_complex_mro(self) -> None: |
| 356 |
ast_nodes = builder.extract_node(""" |
| 357 |
class A(object): |
| 358 |
def spam(self): return "A" |
| 359 |
def foo(self): return "A" |
| 360 |
@staticmethod |
| 361 |
def static(self): pass |
| 362 |
class B(A): |
| 363 |
def boo(self): return "B" |
| 364 |
def spam(self): return "B" |
| 365 |
class C(A): |
| 366 |
def boo(self): return "C" |
| 367 |
class E(C, B): |
| 368 |
def __init__(self): |
| 369 |
super(E, self).boo #@ |
| 370 |
super(C, self).boo #@ |
| 371 |
super(E, self).spam #@ |
| 372 |
super(E, self).foo #@ |
| 373 |
super(E, self).static #@ |
| 374 |
""") |
| 375 |
assert isinstance(ast_nodes, list) |
| 376 |
first = next(ast_nodes[0].infer()) |
| 377 |
self.assertIsInstance(first, bases.BoundMethod) |
| 378 |
self.assertEqual(first.bound.name, "C") |
| 379 |
second = next(ast_nodes[1].infer()) |
| 380 |
self.assertIsInstance(second, bases.BoundMethod) |
| 381 |
self.assertEqual(second.bound.name, "B") |
| 382 |
third = next(ast_nodes[2].infer()) |
| 383 |
self.assertIsInstance(third, bases.BoundMethod) |
| 384 |
self.assertEqual(third.bound.name, "B") |
| 385 |
fourth = next(ast_nodes[3].infer()) |
| 386 |
self.assertEqual(fourth.bound.name, "A") |
| 387 |
static = next(ast_nodes[4].infer()) |
| 388 |
self.assertIsInstance(static, nodes.FunctionDef) |
| 389 |
self.assertEqual(static.parent.scope().name, "A") |
| 390 |
|
| 391 |
def test_super_data_model(self) -> None: |
| 392 |
ast_nodes = builder.extract_node(""" |
| 393 |
class X(object): pass |
| 394 |
class A(X): |
| 395 |
def __init__(self): |
| 396 |
super(A, self) #@ |
| 397 |
super(A, A) #@ |
| 398 |
super(X, A) #@ |
| 399 |
""") |
| 400 |
assert isinstance(ast_nodes, list) |
| 401 |
first = next(ast_nodes[0].infer()) |
| 402 |
thisclass = first.getattr("__thisclass__")[0] |
| 403 |
self.assertIsInstance(thisclass, nodes.ClassDef) |
| 404 |
self.assertEqual(thisclass.name, "A") |
| 405 |
selfclass = first.getattr("__self_class__")[0] |
| 406 |
self.assertIsInstance(selfclass, nodes.ClassDef) |
| 407 |
self.assertEqual(selfclass.name, "A") |
| 408 |
self_ = first.getattr("__self__")[0] |
| 409 |
self.assertIsInstance(self_, bases.Instance) |
| 410 |
self.assertEqual(self_.name, "A") |
| 411 |
cls = first.getattr("__class__")[0] |
| 412 |
self.assertEqual(cls, first._proxied) |
| 413 |
|
| 414 |
second = next(ast_nodes[1].infer()) |
| 415 |
thisclass = second.getattr("__thisclass__")[0] |
| 416 |
self.assertEqual(thisclass.name, "A") |
| 417 |
self_ = second.getattr("__self__")[0] |
| 418 |
self.assertIsInstance(self_, nodes.ClassDef) |
| 419 |
self.assertEqual(self_.name, "A") |
| 420 |
|
| 421 |
third = next(ast_nodes[2].infer()) |
| 422 |
thisclass = third.getattr("__thisclass__")[0] |
| 423 |
self.assertEqual(thisclass.name, "X") |
| 424 |
selfclass = third.getattr("__self_class__")[0] |
| 425 |
self.assertEqual(selfclass.name, "A") |
| 426 |
|
| 427 |
def assertEqualMro(self, klass: Super, expected_mro: list[str]) -> None: |
| 428 |
self.assertEqual([member.name for member in klass.super_mro()], expected_mro) |
| 429 |
|
| 430 |
def test_super_mro(self) -> None: |
| 431 |
ast_nodes = builder.extract_node(""" |
| 432 |
class A(object): pass |
| 433 |
class B(A): pass |
| 434 |
class C(A): pass |
| 435 |
class E(C, B): |
| 436 |
def __init__(self): |
| 437 |
super(E, self) #@ |
| 438 |
super(C, self) #@ |
| 439 |
super(B, self) #@ |
| 440 |
|
| 441 |
super(B, 1) #@ |
| 442 |
super(1, B) #@ |
| 443 |
""") |
| 444 |
assert isinstance(ast_nodes, list) |
| 445 |
first = next(ast_nodes[0].infer()) |
| 446 |
self.assertEqualMro(first, ["C", "B", "A", "object"]) |
| 447 |
second = next(ast_nodes[1].infer()) |
| 448 |
self.assertEqualMro(second, ["B", "A", "object"]) |
| 449 |
third = next(ast_nodes[2].infer()) |
| 450 |
self.assertEqualMro(third, ["A", "object"]) |
| 451 |
|
| 452 |
fourth = next(ast_nodes[3].infer()) |
| 453 |
with self.assertRaises(SuperError): |
| 454 |
fourth.super_mro() |
| 455 |
fifth = next(ast_nodes[4].infer()) |
| 456 |
with self.assertRaises(SuperError): |
| 457 |
fifth.super_mro() |
| 458 |
|
| 459 |
def test_super_yes_objects(self) -> None: |
| 460 |
ast_nodes = builder.extract_node(""" |
| 461 |
from collections import Missing |
| 462 |
class A(object): |
| 463 |
def __init__(self): |
| 464 |
super(Missing, self) #@ |
| 465 |
super(A, Missing) #@ |
| 466 |
""") |
| 467 |
assert isinstance(ast_nodes, list) |
| 468 |
first = next(ast_nodes[0].infer()) |
| 469 |
self.assertIsInstance(first, bases.Instance) |
| 470 |
second = next(ast_nodes[1].infer()) |
| 471 |
self.assertIsInstance(second, bases.Instance) |
| 472 |
|
| 473 |
def test_super_method_chaining_return_type(self) -> None: |
| 474 |
"""Test that super().method() correctly infers child type. |
| 475 |
|
| 476 |
Regression test for: https://github.com/pylint-dev/astroid/issues/457 |
| 477 |
""" |
| 478 |
node = builder.extract_node(""" |
| 479 |
class Parent: |
| 480 |
def method(self) -> "Parent": |
| 481 |
return self |
| 482 |
|
| 483 |
class Child(Parent): |
| 484 |
def method(self) -> "Child": |
| 485 |
return super().method() |
| 486 |
|
| 487 |
def extra(self) -> "Child": |
| 488 |
return self |
| 489 |
|
| 490 |
Child().method().extra() #@ |
| 491 |
""") |
| 492 |
inferred = next(node.infer()) |
| 493 |
# Should infer Child instance, not Parent |
| 494 |
self.assertIsInstance(inferred, bases.Instance) |
| 495 |
self.assertEqual(inferred._proxied.name, "Child") |
| 496 |
|
| 497 |
def test_super_classmethod_chaining_return_type(self) -> None: |
| 498 |
"""Test that super().classmethod() correctly infers child type. |
| 499 |
|
| 500 |
Regression test for: https://github.com/pylint-dev/astroid/issues/457 |
| 501 |
""" |
| 502 |
node = builder.extract_node(""" |
| 503 |
class Parent: |
| 504 |
@classmethod |
| 505 |
def create(cls) -> "Parent": |
| 506 |
return cls() |
| 507 |
|
| 508 |
class Child(Parent): |
| 509 |
@classmethod |
| 510 |
def create(cls) -> "Child": |
| 511 |
return super().create() |
| 512 |
|
| 513 |
def extra(self) -> "Child": |
| 514 |
return self |
| 515 |
|
| 516 |
Child.create().extra() #@ |
| 517 |
""") |
| 518 |
inferred = next(node.infer()) |
| 519 |
# Should infer Child instance, not Parent |
| 520 |
self.assertIsInstance(inferred, bases.Instance) |
| 521 |
self.assertEqual(inferred._proxied.name, "Child") |
| 522 |
|
| 523 |
def test_super_method_three_level_chain_return_type(self) -> None: |
| 524 |
"""Test that super().method() infers the leaf type in a 3-level chain. |
| 525 |
|
| 526 |
Regression test for: |
| 527 |
https://github.com/pylint-dev/pylint/issues/10807 |
| 528 |
https://github.com/pylint-dev/astroid/issues/2852 |
| 529 |
""" |
| 530 |
node = builder.extract_node(""" |
| 531 |
import typing |
| 532 |
|
| 533 |
class A: |
| 534 |
def method(self) -> typing.Self: |
| 535 |
return self |
| 536 |
|
| 537 |
class B(A): |
| 538 |
def method(self) -> typing.Self: |
| 539 |
return super().method() |
| 540 |
|
| 541 |
class C(B): |
| 542 |
clsattr = 1 |
| 543 |
|
| 544 |
C().method() #@ |
| 545 |
""") |
| 546 |
inferred = next(node.infer()) |
| 547 |
self.assertIsInstance(inferred, bases.Instance) |
| 548 |
self.assertEqual(inferred._proxied.name, "C") |
| 549 |
|
| 550 |
def test_super_classmethod_three_level_chain_return_type(self) -> None: |
| 551 |
"""Test that super().classmethod() infers the leaf type in a 3-level chain. |
| 552 |
|
| 553 |
Regression test for: |
| 554 |
https://github.com/pylint-dev/pylint/issues/9159 |
| 555 |
https://github.com/pylint-dev/astroid/issues/2852 |
| 556 |
""" |
| 557 |
node = builder.extract_node(""" |
| 558 |
import typing |
| 559 |
|
| 560 |
class C: |
| 561 |
@classmethod |
| 562 |
def from_nothing(cls) -> typing.Self: |
| 563 |
return cls() |
| 564 |
|
| 565 |
class D(C): |
| 566 |
@classmethod |
| 567 |
def from_nothing(cls) -> typing.Self: |
| 568 |
return super().from_nothing() |
| 569 |
|
| 570 |
class E(D): |
| 571 |
clsattr = 1 |
| 572 |
|
| 573 |
E.from_nothing() #@ |
| 574 |
""") |
| 575 |
inferred = next(node.infer()) |
| 576 |
self.assertIsInstance(inferred, bases.Instance) |
| 577 |
self.assertEqual(inferred._proxied.name, "E") |
| 578 |
|
| 579 |
def test_super_in_a_mixin_uses_the_mro_of_the_class_using_it(self) -> None: |
| 580 |
"""A mixin has no base of its own to delegate to. |
| 581 |
|
| 582 |
``Mixin.sound`` only has something to call because ``Leaf`` puts |
| 583 |
``Base`` after ``Mixin`` in its mro. |
| 584 |
""" |
| 585 |
node = builder.extract_node(""" |
| 586 |
class Base: |
| 587 |
def sound(self): |
| 588 |
return "moo" |
| 589 |
|
| 590 |
class Mixin: |
| 591 |
def sound(self): |
| 592 |
return super().sound() |
| 593 |
|
| 594 |
class Leaf(Mixin, Base): |
| 595 |
pass |
| 596 |
|
| 597 |
Leaf().sound() #@ |
| 598 |
""") |
| 599 |
inferred = next(node.infer()) |
| 600 |
self.assertIsInstance(inferred, bases.Instance) |
| 601 |
self.assertEqual(inferred._proxied.name, "str") |
| 602 |
|
| 603 |
def test_super_follows_the_mro_of_the_calling_class(self) -> None: |
| 604 |
"""``super()`` in ``Left`` reaches ``Right``, which is not an ancestor of it. |
| 605 |
|
| 606 |
``Right`` comes after ``Left`` and before ``Base`` in ``Leaf``'s mro, so |
| 607 |
that is what ``Left.pick`` delegates to when called on a ``Leaf``. |
| 608 |
""" |
| 609 |
node = builder.extract_node(""" |
| 610 |
class Base: |
| 611 |
def pick(self): |
| 612 |
return "apple" |
| 613 |
|
| 614 |
class Left(Base): |
| 615 |
def pick(self): |
| 616 |
return super().pick() |
| 617 |
|
| 618 |
class Right(Base): |
| 619 |
def pick(self): |
| 620 |
return "banana" |
| 621 |
|
| 622 |
class Leaf(Left, Right): |
| 623 |
pass |
| 624 |
|
| 625 |
Leaf().pick() #@ |
| 626 |
""") |
| 627 |
# ``Base.pick`` is still inferred as a second, stale possibility, but the |
| 628 |
# value the call really produces comes first. |
| 629 |
inferred = next(node.infer()) |
| 630 |
self.assertIsInstance(inferred, nodes.Const) |
| 631 |
self.assertEqual(inferred.value, "banana") |
| 632 |
|
| 633 |
def test_super_when_the_calling_class_has_no_usable_mro(self) -> None: |
| 634 |
"""A class whose mro cannot be computed falls back, it does not raise.""" |
| 635 |
node = builder.extract_node(""" |
| 636 |
class Base: |
| 637 |
def eat(self): |
| 638 |
return "yum" |
| 639 |
|
| 640 |
class Mixin: |
| 641 |
def eat(self): |
| 642 |
return super().eat() |
| 643 |
|
| 644 |
class Broken(Mixin, Base, Mixin): |
| 645 |
pass |
| 646 |
|
| 647 |
Broken().eat() #@ |
| 648 |
""") |
| 649 |
self.assertEqual(list(node.infer()), [util.Uninferable]) |
| 650 |
|
| 651 |
def test_super_invalid_types(self) -> None: |
| 652 |
node = builder.extract_node(""" |
| 653 |
import collections |
| 654 |
class A(object): |
| 655 |
def __init__(self): |
| 656 |
super(A, collections) #@ |
| 657 |
""") |
| 658 |
inferred = next(node.infer()) |
| 659 |
with self.assertRaises(SuperError): |
| 660 |
inferred.super_mro() |
| 661 |
with self.assertRaises(SuperError): |
| 662 |
inferred.super_mro() |
| 663 |
|
| 664 |
def test_super_properties(self) -> None: |
| 665 |
node = builder.extract_node(""" |
| 666 |
class Foo(object): |
| 667 |
@property |
| 668 |
def dict(self): |
| 669 |
return 42 |
| 670 |
|
| 671 |
class Bar(Foo): |
| 672 |
@property |
| 673 |
def dict(self): |
| 674 |
return super(Bar, self).dict |
| 675 |
|
| 676 |
Bar().dict |
| 677 |
""") |
| 678 |
inferred = next(node.infer()) |
| 679 |
self.assertIsInstance(inferred, nodes.Const) |
| 680 |
self.assertEqual(inferred.value, 42) |
| 681 |
|
| 682 |
def test_super_qname(self) -> None: |
| 683 |
"""Make sure a Super object generates a qname |
| 684 |
equivalent to super.__qname__. |
| 685 |
""" |
| 686 |
# See issue 533 |
| 687 |
code = """ |
| 688 |
class C: |
| 689 |
def foo(self): return super() |
| 690 |
C().foo() #@ |
| 691 |
""" |
| 692 |
super_obj = next(builder.extract_node(code).infer()) |
| 693 |
self.assertEqual(super_obj.qname(), "super") |
| 694 |
|
| 695 |
def test_super_new_call(self) -> None: |
| 696 |
"""Test that __new__ returns an object or node and not a (Un)BoundMethod.""" |
| 697 |
new_call_result: nodes.Name = builder.extract_node(""" |
| 698 |
import enum |
| 699 |
class ChoicesMeta(enum.EnumMeta): |
| 700 |
def __new__(metacls, classname, bases, classdict, **kwds): |
| 701 |
cls = super().__new__(metacls, "str", (enum.Enum,), enum._EnumDict(), **kwargs) |
| 702 |
cls #@ |
| 703 |
""") |
| 704 |
inferred = list(new_call_result.infer()) |
| 705 |
assert all( |
| 706 |
isinstance(i, (nodes.NodeNG, type(util.Uninferable))) for i in inferred |
| 707 |
) |
| 708 |
|
| 709 |
def test_super_init_call(self) -> None: |
| 710 |
"""Test that __init__ is still callable.""" |
| 711 |
init_node: nodes.Attribute = builder.extract_node(""" |
| 712 |
class SuperUsingClass: |
| 713 |
@staticmethod |
| 714 |
def test(): |
| 715 |
super(object, 1).__new__ #@ |
| 716 |
super(object, 1).__init__ #@ |
| 717 |
class A: |
| 718 |
pass |
| 719 |
A().__new__ #@ |
| 720 |
A().__init__ #@ |
| 721 |
""") |
| 722 |
assert isinstance(next(init_node[0].infer()), bases.BoundMethod) |
| 723 |
assert isinstance(next(init_node[1].infer()), bases.BoundMethod) |
| 724 |
assert isinstance(next(init_node[2].infer()), bases.BoundMethod) |
| 725 |
assert isinstance(next(init_node[3].infer()), bases.BoundMethod) |