| 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 inference involving constraints.""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from astroid import builder, nodes |
| 14 |
from astroid.bases import Instance |
| 15 |
from astroid.util import Uninferable |
| 16 |
|
| 17 |
|
| 18 |
def node_info(node: nodes.NodeNG) -> str: |
| 19 |
return f"Inference of {node.as_string()!r} at line {node.lineno}" |
| 20 |
|
| 21 |
|
| 22 |
def common_params(node: str) -> pytest.MarkDecorator: |
| 23 |
return pytest.mark.parametrize( |
| 24 |
("condition", "satisfy_val", "fail_val"), |
| 25 |
( |
| 26 |
(f"{node} is None", None, 3), |
| 27 |
(f"{node} is not None", 3, None), |
| 28 |
(f"{node}", 3, None), |
| 29 |
(f"not {node}", None, 3), |
| 30 |
(f"isinstance({node}, int)", 3, None), |
| 31 |
(f"isinstance({node}, (int, str))", 3, None), |
| 32 |
(f"{node} == 3", 3, None), |
| 33 |
(f"{node} != 3", None, 3), |
| 34 |
(f"3 == {node}", 3, None), |
| 35 |
(f"3 != {node}", None, 3), |
| 36 |
), |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@common_params(node="x") |
| 41 |
def test_if_single_statement( |
| 42 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 43 |
) -> None: |
| 44 |
"""Test constraint for a variable that is used in the first statement of an if body.""" |
| 45 |
node1, node2 = builder.extract_node(f""" |
| 46 |
def f1(x = {fail_val}): |
| 47 |
if {condition}: # Filters out default value |
| 48 |
return ( |
| 49 |
x #@ |
| 50 |
) |
| 51 |
|
| 52 |
def f2(x = {satisfy_val}): |
| 53 |
if {condition}: # Does not filter out default value |
| 54 |
return ( |
| 55 |
x #@ |
| 56 |
) |
| 57 |
""") |
| 58 |
|
| 59 |
inferred = node1.inferred() |
| 60 |
assert len(inferred) == 1 |
| 61 |
assert inferred[0] is Uninferable |
| 62 |
|
| 63 |
inferred = node2.inferred() |
| 64 |
assert len(inferred) == 2 |
| 65 |
assert isinstance(inferred[0], nodes.Const) |
| 66 |
assert inferred[0].value == satisfy_val |
| 67 |
|
| 68 |
assert inferred[1] is Uninferable |
| 69 |
|
| 70 |
|
| 71 |
@common_params(node="x") |
| 72 |
def test_if_multiple_statements( |
| 73 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 74 |
) -> None: |
| 75 |
"""Test constraint for a variable that is used in an if body with multiple |
| 76 |
statements. |
| 77 |
""" |
| 78 |
node1, node2 = builder.extract_node(f""" |
| 79 |
def f1(x = {fail_val}): |
| 80 |
if {condition}: # Filters out default value |
| 81 |
print(x) |
| 82 |
return ( |
| 83 |
x #@ |
| 84 |
) |
| 85 |
|
| 86 |
def f2(x = {satisfy_val}): |
| 87 |
if {condition}: # Does not filter out default value |
| 88 |
print(x) |
| 89 |
return ( |
| 90 |
x #@ |
| 91 |
) |
| 92 |
""") |
| 93 |
|
| 94 |
inferred = node1.inferred() |
| 95 |
assert len(inferred) == 1 |
| 96 |
assert inferred[0] is Uninferable |
| 97 |
|
| 98 |
inferred = node2.inferred() |
| 99 |
assert len(inferred) == 2 |
| 100 |
assert isinstance(inferred[0], nodes.Const) |
| 101 |
assert inferred[0].value == satisfy_val |
| 102 |
|
| 103 |
assert inferred[1] is Uninferable |
| 104 |
|
| 105 |
|
| 106 |
@common_params(node="x") |
| 107 |
def test_if_irrelevant_condition( |
| 108 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 109 |
) -> None: |
| 110 |
"""Test that constraint for a different variable doesn't apply.""" |
| 111 |
nodes_ = builder.extract_node(f""" |
| 112 |
def f1(x, y = {fail_val}): |
| 113 |
if {condition}: # Does not filter out fail_val |
| 114 |
return ( |
| 115 |
y #@ |
| 116 |
) |
| 117 |
|
| 118 |
def f2(x, y = {satisfy_val}): |
| 119 |
if {condition}: |
| 120 |
return ( |
| 121 |
y #@ |
| 122 |
) |
| 123 |
""") |
| 124 |
for node, val in zip(nodes_, (fail_val, satisfy_val)): |
| 125 |
inferred = node.inferred() |
| 126 |
assert len(inferred) == 2 |
| 127 |
assert isinstance(inferred[0], nodes.Const) |
| 128 |
assert inferred[0].value == val |
| 129 |
|
| 130 |
assert inferred[1] is Uninferable |
| 131 |
|
| 132 |
|
| 133 |
@common_params(node="x") |
| 134 |
def test_outside_if( |
| 135 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 136 |
) -> None: |
| 137 |
"""Test that constraint in an if condition doesn't apply outside of the if.""" |
| 138 |
nodes_ = builder.extract_node(f""" |
| 139 |
def f1(x = {fail_val}): |
| 140 |
if {condition}: |
| 141 |
pass |
| 142 |
return ( |
| 143 |
x #@ |
| 144 |
) |
| 145 |
|
| 146 |
def f2(x = {satisfy_val}): |
| 147 |
if {condition}: |
| 148 |
pass |
| 149 |
|
| 150 |
return ( |
| 151 |
x #@ |
| 152 |
) |
| 153 |
""") |
| 154 |
for node, val in zip(nodes_, (fail_val, satisfy_val)): |
| 155 |
inferred = node.inferred() |
| 156 |
assert len(inferred) == 2 |
| 157 |
assert isinstance(inferred[0], nodes.Const) |
| 158 |
assert inferred[0].value == val |
| 159 |
|
| 160 |
assert inferred[1] is Uninferable |
| 161 |
|
| 162 |
|
| 163 |
@common_params(node="x") |
| 164 |
def test_nested_if( |
| 165 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 166 |
) -> None: |
| 167 |
"""Test that constraint in an if condition applies within inner if statements.""" |
| 168 |
node1, node2 = builder.extract_node(f""" |
| 169 |
def f1(y, x = {fail_val}): |
| 170 |
if {condition}: |
| 171 |
if y is not None: |
| 172 |
return ( |
| 173 |
x #@ |
| 174 |
) |
| 175 |
|
| 176 |
def f2(y, x = {satisfy_val}): |
| 177 |
if {condition}: |
| 178 |
if y is not None: |
| 179 |
return ( |
| 180 |
x #@ |
| 181 |
) |
| 182 |
""") |
| 183 |
inferred = node1.inferred() |
| 184 |
assert len(inferred) == 1 |
| 185 |
assert inferred[0] is Uninferable |
| 186 |
|
| 187 |
inferred = node2.inferred() |
| 188 |
assert len(inferred) == 2 |
| 189 |
assert isinstance(inferred[0], nodes.Const) |
| 190 |
assert inferred[0].value == satisfy_val |
| 191 |
|
| 192 |
assert inferred[1] is Uninferable |
| 193 |
|
| 194 |
|
| 195 |
def test_if_uninferable() -> None: |
| 196 |
"""Test that when no inferred values satisfy all constraints, Uninferable is |
| 197 |
inferred. |
| 198 |
""" |
| 199 |
node1, node2 = builder.extract_node(""" |
| 200 |
def f1(): |
| 201 |
x = None |
| 202 |
if x is not None: |
| 203 |
x #@ |
| 204 |
|
| 205 |
def f2(): |
| 206 |
x = 1 |
| 207 |
if x is not None: |
| 208 |
pass |
| 209 |
else: |
| 210 |
x #@ |
| 211 |
""") |
| 212 |
inferred = node1.inferred() |
| 213 |
assert len(inferred) == 1 |
| 214 |
assert inferred[0] is Uninferable |
| 215 |
|
| 216 |
inferred = node2.inferred() |
| 217 |
assert len(inferred) == 1 |
| 218 |
assert inferred[0] is Uninferable |
| 219 |
|
| 220 |
|
| 221 |
@common_params(node="x") |
| 222 |
def test_if_reassignment_in_body( |
| 223 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 224 |
) -> None: |
| 225 |
"""Test that constraint in an if condition doesn't apply when the variable |
| 226 |
is assigned to a failing value inside the if body. |
| 227 |
""" |
| 228 |
node = builder.extract_node(f""" |
| 229 |
def f(x, y): |
| 230 |
if {condition}: |
| 231 |
if y: |
| 232 |
x = {fail_val} |
| 233 |
return ( |
| 234 |
x #@ |
| 235 |
) |
| 236 |
""") |
| 237 |
inferred = node.inferred() |
| 238 |
assert len(inferred) == 2 |
| 239 |
assert inferred[0] is Uninferable |
| 240 |
|
| 241 |
assert isinstance(inferred[1], nodes.Const) |
| 242 |
assert inferred[1].value == fail_val |
| 243 |
|
| 244 |
|
| 245 |
@common_params(node="x") |
| 246 |
def test_if_elif_else_negates( |
| 247 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 248 |
) -> None: |
| 249 |
"""Test that constraint in an if condition is negated when the variable |
| 250 |
is used in the elif and else branches. |
| 251 |
""" |
| 252 |
node1, node2, node3, node4 = builder.extract_node(f""" |
| 253 |
def f1(y, x = {fail_val}): |
| 254 |
if {condition}: |
| 255 |
pass |
| 256 |
elif y: # Does not filter out default value |
| 257 |
return ( |
| 258 |
x #@ |
| 259 |
) |
| 260 |
else: # Does not filter out default value |
| 261 |
return ( |
| 262 |
x #@ |
| 263 |
) |
| 264 |
|
| 265 |
def f2(y, x = {satisfy_val}): |
| 266 |
if {condition}: |
| 267 |
pass |
| 268 |
elif y: # Filters out default value |
| 269 |
return ( |
| 270 |
x #@ |
| 271 |
) |
| 272 |
else: # Filters out default value |
| 273 |
return ( |
| 274 |
x #@ |
| 275 |
) |
| 276 |
""") |
| 277 |
for node in (node1, node2): |
| 278 |
msg = node_info(node) |
| 279 |
inferred = node.inferred() |
| 280 |
assert len(inferred) == 2, msg |
| 281 |
assert isinstance(inferred[0], nodes.Const), msg |
| 282 |
assert inferred[0].value == fail_val, msg |
| 283 |
|
| 284 |
assert inferred[1] is Uninferable, msg |
| 285 |
|
| 286 |
for node in (node3, node4): |
| 287 |
msg = node_info(node) |
| 288 |
inferred = node.inferred() |
| 289 |
assert len(inferred) == 1, msg |
| 290 |
assert inferred[0] is Uninferable, msg |
| 291 |
|
| 292 |
|
| 293 |
@common_params(node="x") |
| 294 |
def test_if_reassignment_in_else( |
| 295 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 296 |
) -> None: |
| 297 |
"""Test that constraint in an if condition doesn't apply when the variable |
| 298 |
is assigned to a failing value inside the else branch. |
| 299 |
""" |
| 300 |
node = builder.extract_node(f""" |
| 301 |
def f(x, y): |
| 302 |
if {condition}: |
| 303 |
return x |
| 304 |
else: |
| 305 |
if y: |
| 306 |
x = {satisfy_val} |
| 307 |
return ( |
| 308 |
x #@ |
| 309 |
) |
| 310 |
""") |
| 311 |
inferred = node.inferred() |
| 312 |
assert len(inferred) == 2 |
| 313 |
assert inferred[0] is Uninferable |
| 314 |
|
| 315 |
assert isinstance(inferred[1], nodes.Const) |
| 316 |
assert inferred[1].value == satisfy_val |
| 317 |
|
| 318 |
|
| 319 |
@common_params(node="x") |
| 320 |
def test_if_comprehension_shadow( |
| 321 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 322 |
) -> None: |
| 323 |
"""Test that constraint in an if condition doesn't apply when the variable |
| 324 |
is shadowed by an inner comprehension scope. |
| 325 |
""" |
| 326 |
node = builder.extract_node(f""" |
| 327 |
def f(x): |
| 328 |
if {condition}: |
| 329 |
return [ |
| 330 |
x #@ |
| 331 |
for x in [{satisfy_val}, {fail_val}] |
| 332 |
] |
| 333 |
""") |
| 334 |
inferred = node.inferred() |
| 335 |
assert len(inferred) == 2 |
| 336 |
|
| 337 |
for actual, expected in zip(inferred, (satisfy_val, fail_val)): |
| 338 |
assert isinstance(actual, nodes.Const) |
| 339 |
assert actual.value == expected |
| 340 |
|
| 341 |
|
| 342 |
@common_params(node="x") |
| 343 |
def test_if_function_shadow( |
| 344 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 345 |
) -> None: |
| 346 |
"""Test that constraint in an if condition doesn't apply when the variable |
| 347 |
is shadowed by an inner function scope. |
| 348 |
""" |
| 349 |
node = builder.extract_node(f""" |
| 350 |
x = {satisfy_val} |
| 351 |
if {condition}: |
| 352 |
def f(x = {fail_val}): |
| 353 |
return ( |
| 354 |
x #@ |
| 355 |
) |
| 356 |
""") |
| 357 |
inferred = node.inferred() |
| 358 |
assert len(inferred) == 2 |
| 359 |
assert isinstance(inferred[0], nodes.Const) |
| 360 |
assert inferred[0].value == fail_val |
| 361 |
|
| 362 |
assert inferred[1] is Uninferable |
| 363 |
|
| 364 |
|
| 365 |
@common_params(node="x") |
| 366 |
def test_if_function_call( |
| 367 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 368 |
) -> None: |
| 369 |
"""Test that constraint in an if condition doesn't apply for a parameter |
| 370 |
a different function call, but with the same name. |
| 371 |
""" |
| 372 |
node = builder.extract_node(f""" |
| 373 |
def f(x = {satisfy_val}): |
| 374 |
if {condition}: |
| 375 |
g({fail_val}) #@ |
| 376 |
|
| 377 |
def g(x): |
| 378 |
return x |
| 379 |
""") |
| 380 |
inferred = node.inferred() |
| 381 |
assert len(inferred) == 1 |
| 382 |
assert isinstance(inferred[0], nodes.Const) |
| 383 |
assert inferred[0].value == fail_val |
| 384 |
|
| 385 |
|
| 386 |
@common_params(node="self.x") |
| 387 |
def test_if_instance_attr( |
| 388 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 389 |
) -> None: |
| 390 |
"""Test constraint for an instance attribute in an if statement.""" |
| 391 |
node1, node2 = builder.extract_node(f""" |
| 392 |
class A1: |
| 393 |
def __init__(self, x = {fail_val}): |
| 394 |
self.x = x |
| 395 |
|
| 396 |
def method(self): |
| 397 |
if {condition}: |
| 398 |
self.x #@ |
| 399 |
|
| 400 |
class A2: |
| 401 |
def __init__(self, x = {satisfy_val}): |
| 402 |
self.x = x |
| 403 |
|
| 404 |
def method(self): |
| 405 |
if {condition}: |
| 406 |
self.x #@ |
| 407 |
""") |
| 408 |
|
| 409 |
inferred = node1.inferred() |
| 410 |
assert len(inferred) == 1 |
| 411 |
assert inferred[0] is Uninferable |
| 412 |
|
| 413 |
inferred = node2.inferred() |
| 414 |
assert len(inferred) == 2 |
| 415 |
assert isinstance(inferred[0], nodes.Const) |
| 416 |
assert inferred[0].value == satisfy_val |
| 417 |
|
| 418 |
assert inferred[1] is Uninferable |
| 419 |
|
| 420 |
|
| 421 |
@common_params(node="self.x") |
| 422 |
def test_if_instance_attr_reassignment_in_body( |
| 423 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 424 |
) -> None: |
| 425 |
"""Test that constraint in an if condition doesn't apply to an instance attribute |
| 426 |
when it is assigned inside the if body. |
| 427 |
""" |
| 428 |
node1, node2 = builder.extract_node(f""" |
| 429 |
class A1: |
| 430 |
def __init__(self, x): |
| 431 |
self.x = x |
| 432 |
|
| 433 |
def method1(self): |
| 434 |
if {condition}: |
| 435 |
self.x = {satisfy_val} |
| 436 |
self.x #@ |
| 437 |
|
| 438 |
def method2(self): |
| 439 |
if {condition}: |
| 440 |
self.x = {fail_val} |
| 441 |
self.x #@ |
| 442 |
""") |
| 443 |
|
| 444 |
inferred = node1.inferred() |
| 445 |
assert len(inferred) == 2 |
| 446 |
assert inferred[0] is Uninferable |
| 447 |
|
| 448 |
assert isinstance(inferred[1], nodes.Const) |
| 449 |
assert inferred[1].value == satisfy_val |
| 450 |
|
| 451 |
inferred = node2.inferred() |
| 452 |
assert len(inferred) == 3 |
| 453 |
assert inferred[0] is Uninferable |
| 454 |
|
| 455 |
assert isinstance(inferred[1], nodes.Const) |
| 456 |
assert inferred[1].value == satisfy_val |
| 457 |
|
| 458 |
assert isinstance(inferred[2], nodes.Const) |
| 459 |
assert inferred[2].value == fail_val |
| 460 |
|
| 461 |
|
| 462 |
@common_params(node="x") |
| 463 |
def test_if_instance_attr_varname_collision1( |
| 464 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 465 |
) -> None: |
| 466 |
"""Test that constraint in an if condition doesn't apply to an instance attribute |
| 467 |
when the constraint refers to a variable with the same name. |
| 468 |
""" |
| 469 |
node1, node2 = builder.extract_node(f""" |
| 470 |
class A1: |
| 471 |
def __init__(self, x = {fail_val}): |
| 472 |
self.x = x |
| 473 |
|
| 474 |
def method(self, x = {fail_val}): |
| 475 |
if {condition}: |
| 476 |
x #@ |
| 477 |
self.x #@ |
| 478 |
""") |
| 479 |
|
| 480 |
inferred = node1.inferred() |
| 481 |
assert len(inferred) == 1 |
| 482 |
assert inferred[0] is Uninferable |
| 483 |
|
| 484 |
inferred = node2.inferred() |
| 485 |
assert len(inferred) == 2 |
| 486 |
assert isinstance(inferred[0], nodes.Const) |
| 487 |
assert inferred[0].value == fail_val |
| 488 |
|
| 489 |
assert inferred[1] is Uninferable |
| 490 |
|
| 491 |
|
| 492 |
@common_params(node="self.x") |
| 493 |
def test_if_instance_attr_varname_collision2( |
| 494 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 495 |
) -> None: |
| 496 |
"""Test that constraint in an if condition doesn't apply to a variable with the same |
| 497 |
name. |
| 498 |
""" |
| 499 |
node1, node2 = builder.extract_node(f""" |
| 500 |
class A1: |
| 501 |
def __init__(self, x = {fail_val}): |
| 502 |
self.x = x |
| 503 |
|
| 504 |
def method(self, x = {fail_val}): |
| 505 |
if {condition}: |
| 506 |
x #@ |
| 507 |
self.x #@ |
| 508 |
""") |
| 509 |
|
| 510 |
inferred = node1.inferred() |
| 511 |
assert len(inferred) == 2 |
| 512 |
assert isinstance(inferred[0], nodes.Const) |
| 513 |
assert inferred[0].value == fail_val |
| 514 |
|
| 515 |
assert inferred[1] is Uninferable |
| 516 |
|
| 517 |
inferred = node2.inferred() |
| 518 |
assert len(inferred) == 1 |
| 519 |
assert inferred[0] is Uninferable |
| 520 |
|
| 521 |
|
| 522 |
@common_params(node="self.x") |
| 523 |
def test_if_instance_attr_varname_collision3( |
| 524 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 525 |
) -> None: |
| 526 |
"""Test that constraint in an if condition doesn't apply to an instance attribute |
| 527 |
for an object of a different class. |
| 528 |
""" |
| 529 |
node = builder.extract_node(f""" |
| 530 |
class A1: |
| 531 |
def __init__(self, x = {fail_val}): |
| 532 |
self.x = x |
| 533 |
|
| 534 |
def method(self): |
| 535 |
obj = A2() |
| 536 |
if {condition}: |
| 537 |
obj.x #@ |
| 538 |
|
| 539 |
class A2: |
| 540 |
def __init__(self): |
| 541 |
self.x = {fail_val} |
| 542 |
""") |
| 543 |
|
| 544 |
inferred = node.inferred() |
| 545 |
assert len(inferred) == 1 |
| 546 |
assert isinstance(inferred[0], nodes.Const) |
| 547 |
assert inferred[0].value == fail_val |
| 548 |
|
| 549 |
|
| 550 |
@common_params(node="self.x") |
| 551 |
def test_if_instance_attr_varname_collision4( |
| 552 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 553 |
) -> None: |
| 554 |
"""Test that constraint in an if condition doesn't apply to a variable of the same name, |
| 555 |
when that variable is used to infer the value of the instance attribute. |
| 556 |
""" |
| 557 |
node = builder.extract_node(f""" |
| 558 |
class A1: |
| 559 |
def __init__(self, x): |
| 560 |
self.x = x |
| 561 |
|
| 562 |
def method(self): |
| 563 |
x = {fail_val} |
| 564 |
if {condition}: |
| 565 |
self.x = x |
| 566 |
self.x #@ |
| 567 |
""") |
| 568 |
|
| 569 |
inferred = node.inferred() |
| 570 |
assert len(inferred) == 2 |
| 571 |
assert inferred[0] is Uninferable |
| 572 |
|
| 573 |
assert isinstance(inferred[1], nodes.Const) |
| 574 |
assert inferred[1].value == fail_val |
| 575 |
|
| 576 |
|
| 577 |
@common_params(node="x") |
| 578 |
def test_if_exp_body( |
| 579 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 580 |
) -> None: |
| 581 |
"""Test constraint for a variable that is used in an if exp body.""" |
| 582 |
node1, node2 = builder.extract_node(f""" |
| 583 |
def f1(x = {fail_val}): |
| 584 |
return ( |
| 585 |
x if {condition} else None #@ |
| 586 |
) |
| 587 |
|
| 588 |
def f2(x = {satisfy_val}): |
| 589 |
return ( |
| 590 |
x if {condition} else None #@ |
| 591 |
) |
| 592 |
""") |
| 593 |
|
| 594 |
inferred = node1.body.inferred() |
| 595 |
assert len(inferred) == 1 |
| 596 |
assert inferred[0] is Uninferable |
| 597 |
|
| 598 |
inferred = node2.body.inferred() |
| 599 |
assert len(inferred) == 2 |
| 600 |
assert isinstance(inferred[0], nodes.Const) |
| 601 |
assert inferred[0].value == satisfy_val |
| 602 |
assert inferred[1] is Uninferable |
| 603 |
|
| 604 |
|
| 605 |
@common_params(node="x") |
| 606 |
def test_if_exp_else( |
| 607 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 608 |
) -> None: |
| 609 |
"""Test constraint for a variable that is used in an if exp else block.""" |
| 610 |
node1, node2 = builder.extract_node(f""" |
| 611 |
def f1(x = {satisfy_val}): |
| 612 |
return ( |
| 613 |
None if {condition} else x #@ |
| 614 |
) |
| 615 |
|
| 616 |
def f2(x = {fail_val}): |
| 617 |
return ( |
| 618 |
None if {condition} else x #@ |
| 619 |
) |
| 620 |
""") |
| 621 |
|
| 622 |
inferred = node1.orelse.inferred() |
| 623 |
assert len(inferred) == 1 |
| 624 |
assert inferred[0] is Uninferable |
| 625 |
|
| 626 |
inferred = node2.orelse.inferred() |
| 627 |
assert len(inferred) == 2 |
| 628 |
assert isinstance(inferred[0], nodes.Const) |
| 629 |
assert inferred[0].value == fail_val |
| 630 |
assert inferred[1] is Uninferable |
| 631 |
|
| 632 |
|
| 633 |
@common_params(node="x") |
| 634 |
def test_outside_if_exp( |
| 635 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 636 |
) -> None: |
| 637 |
"""Test that constraint in an if exp condition doesn't apply outside of the if exp.""" |
| 638 |
nodes_ = builder.extract_node(f""" |
| 639 |
def f1(x = {fail_val}): |
| 640 |
x if {condition} else None |
| 641 |
return ( |
| 642 |
x #@ |
| 643 |
) |
| 644 |
|
| 645 |
def f2(x = {satisfy_val}): |
| 646 |
None if {condition} else x |
| 647 |
return ( |
| 648 |
x #@ |
| 649 |
) |
| 650 |
""") |
| 651 |
for node, val in zip(nodes_, (fail_val, satisfy_val)): |
| 652 |
inferred = node.inferred() |
| 653 |
assert len(inferred) == 2 |
| 654 |
assert isinstance(inferred[0], nodes.Const) |
| 655 |
assert inferred[0].value == val |
| 656 |
assert inferred[1] is Uninferable |
| 657 |
|
| 658 |
|
| 659 |
@common_params(node="x") |
| 660 |
def test_nested_if_exp( |
| 661 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 662 |
) -> None: |
| 663 |
"""Test that constraint in an if exp condition applies within inner if exp.""" |
| 664 |
node1, node2 = builder.extract_node(f""" |
| 665 |
def f1(y, x = {fail_val}): |
| 666 |
return ( |
| 667 |
(x if y else None) if {condition} else None #@ |
| 668 |
) |
| 669 |
|
| 670 |
def f2(y, x = {satisfy_val}): |
| 671 |
return ( |
| 672 |
(x if y else None) if {condition} else None #@ |
| 673 |
) |
| 674 |
""") |
| 675 |
|
| 676 |
inferred = node1.body.body.inferred() |
| 677 |
assert len(inferred) == 1 |
| 678 |
assert inferred[0] is Uninferable |
| 679 |
|
| 680 |
inferred = node2.body.body.inferred() |
| 681 |
assert len(inferred) == 2 |
| 682 |
assert isinstance(inferred[0], nodes.Const) |
| 683 |
assert inferred[0].value == satisfy_val |
| 684 |
assert inferred[1] is Uninferable |
| 685 |
|
| 686 |
|
| 687 |
@common_params(node="self.x") |
| 688 |
def test_if_exp_instance_attr( |
| 689 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 690 |
) -> None: |
| 691 |
"""Test constraint for an instance attribute in an if exp.""" |
| 692 |
node1, node2 = builder.extract_node(f""" |
| 693 |
class A1: |
| 694 |
def __init__(self, x = {fail_val}): |
| 695 |
self.x = x |
| 696 |
|
| 697 |
def method(self): |
| 698 |
return ( |
| 699 |
self.x if {condition} else None #@ |
| 700 |
) |
| 701 |
|
| 702 |
class A2: |
| 703 |
def __init__(self, x = {satisfy_val}): |
| 704 |
self.x = x |
| 705 |
|
| 706 |
def method(self): |
| 707 |
return ( |
| 708 |
self.x if {condition} else None #@ |
| 709 |
) |
| 710 |
""") |
| 711 |
|
| 712 |
inferred = node1.body.inferred() |
| 713 |
assert len(inferred) == 1 |
| 714 |
assert inferred[0] is Uninferable |
| 715 |
|
| 716 |
inferred = node2.body.inferred() |
| 717 |
assert len(inferred) == 2 |
| 718 |
assert isinstance(inferred[0], nodes.Const) |
| 719 |
assert inferred[0].value == satisfy_val |
| 720 |
assert inferred[1].value is Uninferable |
| 721 |
|
| 722 |
|
| 723 |
@common_params(node="self.x") |
| 724 |
def test_if_exp_instance_attr_varname_collision( |
| 725 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 726 |
) -> None: |
| 727 |
"""Test that constraint in an if exp condition doesn't apply to a variable with the same name.""" |
| 728 |
node = builder.extract_node(f""" |
| 729 |
class A: |
| 730 |
def __init__(self, x = {fail_val}): |
| 731 |
self.x = x |
| 732 |
|
| 733 |
def method(self, x = {fail_val}): |
| 734 |
return ( |
| 735 |
x if {condition} else None #@ |
| 736 |
) |
| 737 |
""") |
| 738 |
|
| 739 |
inferred = node.body.inferred() |
| 740 |
assert len(inferred) == 2 |
| 741 |
assert isinstance(inferred[0], nodes.Const) |
| 742 |
assert inferred[0].value == fail_val |
| 743 |
assert inferred[1].value is Uninferable |
| 744 |
|
| 745 |
|
| 746 |
def test_isinstance_equal_types() -> None: |
| 747 |
"""Test constraint for an object whose type is equal to the checked type.""" |
| 748 |
node = builder.extract_node(""" |
| 749 |
class A: |
| 750 |
pass |
| 751 |
|
| 752 |
x = A() |
| 753 |
|
| 754 |
if isinstance(x, A): |
| 755 |
x #@ |
| 756 |
""") |
| 757 |
|
| 758 |
inferred = node.inferred() |
| 759 |
assert len(inferred) == 1 |
| 760 |
assert isinstance(inferred[0], Instance) |
| 761 |
assert isinstance(inferred[0]._proxied, nodes.ClassDef) |
| 762 |
assert inferred[0].name == "A" |
| 763 |
|
| 764 |
|
| 765 |
def test_isinstance_subtype() -> None: |
| 766 |
"""Test constraint for an object whose type is a strict subtype of the checked type.""" |
| 767 |
node = builder.extract_node(""" |
| 768 |
class A: |
| 769 |
pass |
| 770 |
|
| 771 |
class B(A): |
| 772 |
pass |
| 773 |
|
| 774 |
x = B() |
| 775 |
|
| 776 |
if isinstance(x, A): |
| 777 |
x #@ |
| 778 |
""") |
| 779 |
|
| 780 |
inferred = node.inferred() |
| 781 |
assert len(inferred) == 1 |
| 782 |
assert isinstance(inferred[0], Instance) |
| 783 |
assert isinstance(inferred[0]._proxied, nodes.ClassDef) |
| 784 |
assert inferred[0].name == "B" |
| 785 |
|
| 786 |
|
| 787 |
def test_isinstance_unrelated_types(): |
| 788 |
"""Test constraint for an object whose type is not related to the checked type.""" |
| 789 |
node = builder.extract_node(""" |
| 790 |
class A: |
| 791 |
pass |
| 792 |
|
| 793 |
class B: |
| 794 |
pass |
| 795 |
|
| 796 |
x = A() |
| 797 |
|
| 798 |
if isinstance(x, B): |
| 799 |
x #@ |
| 800 |
""") |
| 801 |
|
| 802 |
inferred = node.inferred() |
| 803 |
assert len(inferred) == 1 |
| 804 |
assert inferred[0] is Uninferable |
| 805 |
|
| 806 |
|
| 807 |
def test_isinstance_supertype(): |
| 808 |
"""Test constraint for an object whose type is a strict supertype of the checked type.""" |
| 809 |
node = builder.extract_node(""" |
| 810 |
class A: |
| 811 |
pass |
| 812 |
|
| 813 |
class B(A): |
| 814 |
pass |
| 815 |
|
| 816 |
x = A() |
| 817 |
|
| 818 |
if isinstance(x, B): |
| 819 |
x #@ |
| 820 |
""") |
| 821 |
|
| 822 |
inferred = node.inferred() |
| 823 |
assert len(inferred) == 1 |
| 824 |
assert inferred[0] is Uninferable |
| 825 |
|
| 826 |
|
| 827 |
def test_isinstance_multiple_inheritance(): |
| 828 |
"""Test constraint for an object that inherits from more than one parent class.""" |
| 829 |
n1, n2, n3 = builder.extract_node(""" |
| 830 |
class A: |
| 831 |
pass |
| 832 |
|
| 833 |
class B: |
| 834 |
pass |
| 835 |
|
| 836 |
class C(A, B): |
| 837 |
pass |
| 838 |
|
| 839 |
x = C() |
| 840 |
|
| 841 |
if isinstance(x, C): |
| 842 |
x #@ |
| 843 |
|
| 844 |
if isinstance(x, A): |
| 845 |
x #@ |
| 846 |
|
| 847 |
if isinstance(x, B): |
| 848 |
x #@ |
| 849 |
""") |
| 850 |
|
| 851 |
for node in (n1, n2, n3): |
| 852 |
msg = node_info(node) |
| 853 |
inferred = node.inferred() |
| 854 |
assert len(inferred) == 1, msg |
| 855 |
assert isinstance(inferred[0], Instance), msg |
| 856 |
assert isinstance(inferred[0]._proxied, nodes.ClassDef), msg |
| 857 |
assert inferred[0].name == "C", msg |
| 858 |
|
| 859 |
|
| 860 |
def test_isinstance_diamond_inheritance(): |
| 861 |
"""Test constraint for an object that inherits from parent classes |
| 862 |
in diamond inheritance. |
| 863 |
""" |
| 864 |
n1, n2, n3, n4 = builder.extract_node(""" |
| 865 |
class A(): |
| 866 |
pass |
| 867 |
|
| 868 |
class B(A): |
| 869 |
pass |
| 870 |
|
| 871 |
class C(A): |
| 872 |
pass |
| 873 |
|
| 874 |
class D(B, C): |
| 875 |
pass |
| 876 |
|
| 877 |
x = D() |
| 878 |
|
| 879 |
if isinstance(x, D): |
| 880 |
x #@ |
| 881 |
|
| 882 |
if isinstance(x, B): |
| 883 |
x #@ |
| 884 |
|
| 885 |
if isinstance(x, C): |
| 886 |
x #@ |
| 887 |
|
| 888 |
if isinstance(x, A): |
| 889 |
x #@ |
| 890 |
""") |
| 891 |
|
| 892 |
for node in (n1, n2, n3, n4): |
| 893 |
msg = node_info(node) |
| 894 |
inferred = node.inferred() |
| 895 |
assert len(inferred) == 1, msg |
| 896 |
assert isinstance(inferred[0], Instance), msg |
| 897 |
assert isinstance(inferred[0]._proxied, nodes.ClassDef), msg |
| 898 |
assert inferred[0].name == "D", msg |
| 899 |
|
| 900 |
|
| 901 |
def test_isinstance_keyword_arguments(): |
| 902 |
"""Test that constraint does not apply when `isinstance` is called |
| 903 |
with keyword arguments. |
| 904 |
""" |
| 905 |
n1, n2 = builder.extract_node(""" |
| 906 |
x = 3 |
| 907 |
|
| 908 |
if isinstance(object=x, classinfo=str): |
| 909 |
x #@ |
| 910 |
|
| 911 |
if isinstance(x, str, object=x, classinfo=str): |
| 912 |
x #@ |
| 913 |
""") |
| 914 |
|
| 915 |
for node in (n1, n2): |
| 916 |
msg = node_info(node) |
| 917 |
inferred = node.inferred() |
| 918 |
assert len(inferred) == 1, msg |
| 919 |
assert isinstance(inferred[0], nodes.Const), msg |
| 920 |
assert inferred[0].value == 3, msg |
| 921 |
|
| 922 |
|
| 923 |
def test_isinstance_extra_argument(): |
| 924 |
"""Test that constraint does not apply when `isinstance` is called |
| 925 |
with more than two positional arguments. |
| 926 |
""" |
| 927 |
node = builder.extract_node(""" |
| 928 |
x = 3 |
| 929 |
|
| 930 |
if isinstance(x, str, bool): |
| 931 |
x #@ |
| 932 |
""") |
| 933 |
|
| 934 |
inferred = node.inferred() |
| 935 |
assert len(inferred) == 1 |
| 936 |
assert isinstance(inferred[0], nodes.Const) |
| 937 |
assert inferred[0].value == 3 |
| 938 |
|
| 939 |
|
| 940 |
def test_isinstance_classinfo_inference_error(): |
| 941 |
"""Test that constraint is satisfied when `isinstance` is called with |
| 942 |
classinfo that raises an inference error. |
| 943 |
""" |
| 944 |
node = builder.extract_node(""" |
| 945 |
x = 3 |
| 946 |
|
| 947 |
if isinstance(x, undefined_type): |
| 948 |
x #@ |
| 949 |
""") |
| 950 |
|
| 951 |
inferred = node.inferred() |
| 952 |
assert len(inferred) == 1 |
| 953 |
assert isinstance(inferred[0], nodes.Const) |
| 954 |
assert inferred[0].value == 3 |
| 955 |
|
| 956 |
|
| 957 |
def test_isinstance_uninferable_classinfo(): |
| 958 |
"""Test that constraint is satisfied when `isinstance` is called with |
| 959 |
uninferable classinfo. |
| 960 |
""" |
| 961 |
node = builder.extract_node(""" |
| 962 |
def f(classinfo): |
| 963 |
x = 3 |
| 964 |
|
| 965 |
if isinstance(x, classinfo): |
| 966 |
x #@ |
| 967 |
""") |
| 968 |
|
| 969 |
inferred = node.inferred() |
| 970 |
assert len(inferred) == 1 |
| 971 |
assert isinstance(inferred[0], nodes.Const) |
| 972 |
assert inferred[0].value == 3 |
| 973 |
|
| 974 |
|
| 975 |
def test_isinstance_mro_error(): |
| 976 |
"""Test that constraint is satisfied when computing the object's |
| 977 |
method resolution order raises an MRO error. |
| 978 |
""" |
| 979 |
node = builder.extract_node(""" |
| 980 |
class A(): |
| 981 |
pass |
| 982 |
|
| 983 |
class B(A, A): |
| 984 |
pass |
| 985 |
|
| 986 |
x = B() |
| 987 |
|
| 988 |
if isinstance(x, A): |
| 989 |
x #@ |
| 990 |
""") |
| 991 |
|
| 992 |
inferred = node.inferred() |
| 993 |
assert len(inferred) == 1 |
| 994 |
assert isinstance(inferred[0], Instance) |
| 995 |
assert isinstance(inferred[0]._proxied, nodes.ClassDef) |
| 996 |
assert inferred[0].name == "B" |
| 997 |
|
| 998 |
|
| 999 |
def test_isinstance_uninferable(): |
| 1000 |
"""Test that constraint is satisfied when `isinstance` inference returns Uninferable.""" |
| 1001 |
node = builder.extract_node(""" |
| 1002 |
x = 3 |
| 1003 |
|
| 1004 |
if isinstance(x, str): |
| 1005 |
x #@ |
| 1006 |
""") |
| 1007 |
|
| 1008 |
with patch( |
| 1009 |
"astroid.constraint.helpers.object_isinstance", return_value=Uninferable |
| 1010 |
): |
| 1011 |
inferred = node.inferred() |
| 1012 |
assert len(inferred) == 1 |
| 1013 |
assert isinstance(inferred[0], nodes.Const) |
| 1014 |
assert inferred[0].value == 3 |
| 1015 |
|
| 1016 |
|
| 1017 |
def test_equality_callable(): |
| 1018 |
"""Test constraint for equality of callables.""" |
| 1019 |
node1, node2, node3, node4, node5, node6 = builder.extract_node(""" |
| 1020 |
class Foo: |
| 1021 |
pass |
| 1022 |
|
| 1023 |
def bar(): |
| 1024 |
pass |
| 1025 |
|
| 1026 |
baz = lambda i : i |
| 1027 |
|
| 1028 |
x, y, z = Foo, bar, baz |
| 1029 |
|
| 1030 |
if x == Foo: |
| 1031 |
x #@ |
| 1032 |
if x != Foo: |
| 1033 |
x #@ |
| 1034 |
|
| 1035 |
if y == bar: |
| 1036 |
y #@ |
| 1037 |
if y != bar: |
| 1038 |
y #@ |
| 1039 |
|
| 1040 |
if z == baz: |
| 1041 |
z #@ |
| 1042 |
if z != baz: |
| 1043 |
z #@ |
| 1044 |
""") |
| 1045 |
|
| 1046 |
inferred = node1.inferred() |
| 1047 |
assert len(inferred) == 1 |
| 1048 |
assert isinstance(inferred[0], nodes.ClassDef) |
| 1049 |
assert inferred[0].name == "Foo" |
| 1050 |
|
| 1051 |
inferred = node3.inferred() |
| 1052 |
assert len(inferred) == 1 |
| 1053 |
assert isinstance(inferred[0], nodes.FunctionDef) |
| 1054 |
assert inferred[0].name == "bar" |
| 1055 |
|
| 1056 |
inferred = node5.inferred() |
| 1057 |
assert len(inferred) == 1 |
| 1058 |
assert isinstance(inferred[0], nodes.Lambda) |
| 1059 |
|
| 1060 |
for node in (node2, node4, node6): |
| 1061 |
msg = node_info(node) |
| 1062 |
inferred = node.inferred() |
| 1063 |
assert len(inferred) == 1, msg |
| 1064 |
assert inferred[0] is Uninferable, msg |
| 1065 |
|
| 1066 |
|
| 1067 |
def test_equality_uninferable_operand(): |
| 1068 |
"""Test that equality constraint is satisfied when either operand is uninferable.""" |
| 1069 |
node1, node2, node3, node4 = builder.extract_node(""" |
| 1070 |
def f1(x): |
| 1071 |
if x == 3: |
| 1072 |
x #@ |
| 1073 |
|
| 1074 |
if x != 3: |
| 1075 |
x #@ |
| 1076 |
|
| 1077 |
def f2(y): |
| 1078 |
x = 3 |
| 1079 |
if x == y: |
| 1080 |
x #@ |
| 1081 |
|
| 1082 |
if x != y: |
| 1083 |
x #@ |
| 1084 |
""") |
| 1085 |
|
| 1086 |
for node in (node1, node2): |
| 1087 |
msg = node_info(node) |
| 1088 |
inferred = node.inferred() |
| 1089 |
assert len(inferred) == 1, msg |
| 1090 |
assert inferred[0] is Uninferable, msg |
| 1091 |
|
| 1092 |
for node in (node3, node4): |
| 1093 |
msg = node_info(node) |
| 1094 |
inferred = node.inferred() |
| 1095 |
assert len(inferred) == 1, msg |
| 1096 |
assert isinstance(inferred[0], nodes.Const), msg |
| 1097 |
assert inferred[0].value == 3, msg |
| 1098 |
|
| 1099 |
|
| 1100 |
def test_equality_ambiguous_operand(): |
| 1101 |
"""Test that equality constraint is satisfied when the compared operand has multiple inferred values.""" |
| 1102 |
node1, node2 = builder.extract_node(""" |
| 1103 |
def f(y = 1): |
| 1104 |
x = 3 |
| 1105 |
if x == y: |
| 1106 |
x #@ |
| 1107 |
|
| 1108 |
if x != y: |
| 1109 |
x #@ |
| 1110 |
""") |
| 1111 |
|
| 1112 |
for node in (node1, node2): |
| 1113 |
msg = node_info(node) |
| 1114 |
inferred = node.inferred() |
| 1115 |
assert len(inferred) == 1, msg |
| 1116 |
assert isinstance(inferred[0], nodes.Const), msg |
| 1117 |
assert inferred[0].value == 3, msg |
| 1118 |
|
| 1119 |
|
| 1120 |
def test_equality_fractions(): |
| 1121 |
"""Test that equality constraint is satisfied when both operands are fractions.""" |
| 1122 |
node1, node2, node3, node4 = builder.extract_node(""" |
| 1123 |
from fractions import Fraction |
| 1124 |
|
| 1125 |
x = Fraction(1, 3) |
| 1126 |
y = Fraction(1, 3) |
| 1127 |
|
| 1128 |
if x == y: |
| 1129 |
x #@ |
| 1130 |
y #@ |
| 1131 |
|
| 1132 |
if x != y: |
| 1133 |
x #@ |
| 1134 |
y #@ |
| 1135 |
""") |
| 1136 |
|
| 1137 |
for node in (node1, node2, node3, node4): |
| 1138 |
msg = node_info(node) |
| 1139 |
inferred = node.inferred() |
| 1140 |
assert len(inferred) == 1, msg |
| 1141 |
assert isinstance(inferred[0], Instance), msg |
| 1142 |
assert isinstance(inferred[0]._proxied, nodes.ClassDef), msg |
| 1143 |
assert inferred[0]._proxied.name == "Fraction", msg |
| 1144 |
|
| 1145 |
|
| 1146 |
@common_params(node="x") |
| 1147 |
def test_comprehension_condition( |
| 1148 |
condition: str, satisfy_val: int | None, fail_val: int | None |
| 1149 |
) -> None: |
| 1150 |
"""Test constraint for a comprehension target used in the element expression.""" |
| 1151 |
node = builder.extract_node( |
| 1152 |
f"[x for x in [{satisfy_val}, {fail_val}] if {condition}]" |
| 1153 |
) |
| 1154 |
inferred = node.elt.inferred() |
| 1155 |
assert len(inferred) == 1 |
| 1156 |
assert isinstance(inferred[0], nodes.Const) |
| 1157 |
assert inferred[0].value == satisfy_val |
| 1158 |
|
| 1159 |
|
| 1160 |
def test_comprehension_condition_all_comprehension_types() -> None: |
| 1161 |
"""Test that comprehension conditions apply in every type of comprehension.""" |
| 1162 |
list_comp, set_comp, gen_exp, dict_comp = builder.extract_node(""" |
| 1163 |
[x for x in [None, 3] if x is not None] #@ |
| 1164 |
{x for x in [None, 3] if x is not None} #@ |
| 1165 |
(x for x in [None, 3] if x is not None) #@ |
| 1166 |
{x: x for x in [None, 3] if x is not None} #@ |
| 1167 |
""") |
| 1168 |
for comp in (list_comp, set_comp, gen_exp): |
| 1169 |
msg = node_info(comp) |
| 1170 |
inferred = comp.elt.inferred() |
| 1171 |
assert len(inferred) == 1, msg |
| 1172 |
assert isinstance(inferred[0], nodes.Const), msg |
| 1173 |
assert inferred[0].value == 3, msg |
| 1174 |
|
| 1175 |
for expr in (dict_comp.key, dict_comp.value): |
| 1176 |
inferred = expr.inferred() |
| 1177 |
assert len(inferred) == 1 |
| 1178 |
assert isinstance(inferred[0], nodes.Const) |
| 1179 |
assert inferred[0].value == 3 |
| 1180 |
|
| 1181 |
|
| 1182 |
def test_comprehension_condition_unsupported_pattern() -> None: |
| 1183 |
"""Test that a condition matching no supported constraint doesn't filter.""" |
| 1184 |
node = builder.extract_node("[L for L in [[1, 2], [3, 4]] if sum(L) == 3]") |
| 1185 |
inferred = node.elt.inferred() |
| 1186 |
assert [elt.as_string() for elt in inferred] == ["[1, 2]", "[3, 4]"] |
| 1187 |
|
| 1188 |
|
| 1189 |
def test_comprehension_condition_multiple_conditions() -> None: |
| 1190 |
"""Test that all conditions apply to the element expression and that earlier |
| 1191 |
conditions also guard later ones. |
| 1192 |
""" |
| 1193 |
node = builder.extract_node("[x for x in [None, 3, 4] if x is not None if x == 3]") |
| 1194 |
inferred = node.elt.inferred() |
| 1195 |
assert len(inferred) == 1 |
| 1196 |
assert isinstance(inferred[0], nodes.Const) |
| 1197 |
assert inferred[0].value == 3 |
| 1198 |
|
| 1199 |
# ``x`` in the second condition is only guarded by the first condition. |
| 1200 |
second_condition = node.generators[0].ifs[1] |
| 1201 |
inferred = second_condition.left.inferred() |
| 1202 |
assert [const.value for const in inferred] == [3, 4] |
| 1203 |
|
| 1204 |
|
| 1205 |
def test_comprehension_condition_guards_later_generators() -> None: |
| 1206 |
"""Test that a generator's conditions guard the iterables of later generators.""" |
| 1207 |
node = builder.extract_node("[y for x in [None, [3]] if x is not None for y in x]") |
| 1208 |
inferred = node.elt.inferred() |
| 1209 |
assert len(inferred) == 1 |
| 1210 |
assert isinstance(inferred[0], nodes.Const) |
| 1211 |
assert inferred[0].value == 3 |
| 1212 |
|
| 1213 |
|
| 1214 |
def test_comprehension_condition_does_not_guard_same_generator() -> None: |
| 1215 |
"""Test that a generator's conditions don't constrain its own target, iterable |
| 1216 |
or the element of a preceding generator. |
| 1217 |
""" |
| 1218 |
node = builder.extract_node(""" |
| 1219 |
def f(y = None): |
| 1220 |
return [x for x in [y] if y is not None] #@ |
| 1221 |
""") |
| 1222 |
# ``y`` in the iterable is not constrained by the condition. |
| 1223 |
iter_elt = node.value.generators[0].iter.elts[0] |
| 1224 |
inferred = iter_elt.inferred() |
| 1225 |
assert len(inferred) == 2 |
| 1226 |
assert isinstance(inferred[0], nodes.Const) |
| 1227 |
assert inferred[0].value is None |
| 1228 |
assert inferred[1] is Uninferable |
| 1229 |
|
| 1230 |
|
| 1231 |
def test_comprehension_condition_outer_name() -> None: |
| 1232 |
"""Test that comprehension conditions constrain names from outer scopes |
| 1233 |
used in the element expression. |
| 1234 |
""" |
| 1235 |
node = builder.extract_node(""" |
| 1236 |
def f(y = None): |
| 1237 |
return [y for x in [1, 2] if y is not None] #@ |
| 1238 |
""") |
| 1239 |
inferred = node.value.elt.inferred() |
| 1240 |
assert inferred == [Uninferable] |
| 1241 |
|
| 1242 |
|
| 1243 |
def test_isinstance_multiple_inferred_values() -> None: |
| 1244 |
"""Test that an isinstance() constraint filters every inferred value. |
| 1245 |
|
| 1246 |
Checking a value must not pollute the inference context used to check |
| 1247 |
the following ones. |
| 1248 |
""" |
| 1249 |
node = builder.extract_node(""" |
| 1250 |
def f(y): |
| 1251 |
x = 2 if y else "a" |
| 1252 |
if isinstance(x, int): |
| 1253 |
return ( |
| 1254 |
x #@ |
| 1255 |
) |
| 1256 |
""") |
| 1257 |
inferred = node.inferred() |
| 1258 |
assert len(inferred) == 1 |
| 1259 |
assert isinstance(inferred[0], nodes.Const) |
| 1260 |
assert inferred[0].value == 2 |
| 1261 |
|
| 1262 |
|
| 1263 |
def test_equality_multiple_inferred_values() -> None: |
| 1264 |
"""Test that an equality constraint with a name operand filters every |
| 1265 |
inferred value. |
| 1266 |
|
| 1267 |
Checking a value must not pollute the inference context used to check |
| 1268 |
the following ones. |
| 1269 |
""" |
| 1270 |
node = builder.extract_node(""" |
| 1271 |
y = 1 |
| 1272 |
|
| 1273 |
def f(cond): |
| 1274 |
x = 1 if cond else 2 |
| 1275 |
if x == y: |
| 1276 |
return ( |
| 1277 |
x #@ |
| 1278 |
) |
| 1279 |
""") |
| 1280 |
inferred = node.inferred() |
| 1281 |
assert len(inferred) == 1 |
| 1282 |
assert isinstance(inferred[0], nodes.Const) |
| 1283 |
assert inferred[0].value == 1 |
| 1284 |
|
| 1285 |
|
| 1286 |
def test_comprehension_condition_nested_in_call() -> None: |
| 1287 |
"""Test that comprehension conditions constrain a name nested inside a call.""" |
| 1288 |
node = builder.extract_node("[f(x) for x in [None, 1] if x is not None]") |
| 1289 |
inferred = node.elt.args[0].inferred() |
| 1290 |
assert len(inferred) == 1 |
| 1291 |
assert isinstance(inferred[0], nodes.Const) |
| 1292 |
assert inferred[0].value == 1 |
| 1293 |
|
| 1294 |
node = builder.extract_node( |
| 1295 |
"[y for x in [None, 1] if x is not None for y in make_iter(x)]" |
| 1296 |
) |
| 1297 |
inferred = node.generators[1].iter.args[0].inferred() |
| 1298 |
assert len(inferred) == 1 |
| 1299 |
assert isinstance(inferred[0], nodes.Const) |
| 1300 |
assert inferred[0].value == 1 |
| 1301 |
|
| 1302 |
|
| 1303 |
def test_comprehension_condition_nested_comprehension() -> None: |
| 1304 |
"""Test that comprehension conditions constrain a name used inside an inner |
| 1305 |
comprehension. |
| 1306 |
""" |
| 1307 |
node = builder.extract_node( |
| 1308 |
"[[y for y in x] for x in [None, [1]] if x is not None]" |
| 1309 |
) |
| 1310 |
inner = node.elt |
| 1311 |
|
| 1312 |
inferred = inner.generators[0].iter.inferred() |
| 1313 |
assert [iterable.as_string() for iterable in inferred] == ["[1]"] |
| 1314 |
|
| 1315 |
inferred = inner.elt.inferred() |
| 1316 |
assert len(inferred) == 1 |
| 1317 |
assert isinstance(inferred[0], nodes.Const) |
| 1318 |
assert inferred[0].value == 1 |