Skip to content

Commit b4fe93a

Browse files
committed
Use UninferableBase instead of Uninferable
1 parent 9c4fbed commit b4fe93a

17 files changed

+87
-81
lines changed

pylint/checkers/async.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import TYPE_CHECKING
1111

1212
import astroid
13-
from astroid import nodes
13+
from astroid import nodes, util
1414

1515
from pylint import checkers
1616
from pylint.checkers import utils as checker_utils
@@ -55,7 +55,7 @@ def visit_asyncfunctiondef(self, node: nodes.AsyncFunctionDef) -> None:
5555
def visit_asyncwith(self, node: nodes.AsyncWith) -> None:
5656
for ctx_mgr, _ in node.items:
5757
inferred = checker_utils.safe_infer(ctx_mgr)
58-
if inferred is None or inferred is astroid.Uninferable:
58+
if inferred is None or isinstance(inferred, util.UninferableBase):
5959
continue
6060

6161
if isinstance(inferred, nodes.AsyncFunctionDef):

pylint/checkers/base/basic_checker.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import TYPE_CHECKING, cast
1414

1515
import astroid
16-
from astroid import nodes, objects
16+
from astroid import nodes, objects, util
1717

1818
from pylint import utils as lint_utils
1919
from pylint.checkers import BaseChecker, utils
@@ -333,7 +333,9 @@ def _check_using_constant_test(
333333
maybe_generator_call = None
334334
if not isinstance(test, except_nodes):
335335
inferred = utils.safe_infer(test)
336-
if inferred is astroid.Uninferable and isinstance(test, nodes.Name):
336+
if isinstance(inferred, util.UninferableBase) and isinstance(
337+
test, nodes.Name
338+
):
337339
emit, maybe_generator_call = BasicChecker._name_holds_generator(test)
338340

339341
# Emit if calling a function that only returns GeneratorExp (always tests True)
@@ -677,7 +679,7 @@ def _check_misplaced_format_function(self, call_node: nodes.Call) -> None:
677679
return
678680

679681
expr = utils.safe_infer(call_node.func.expr)
680-
if expr is astroid.Uninferable:
682+
if isinstance(expr, util.UninferableBase):
681683
return
682684
if not expr:
683685
# we are doubtful on inferred type of node, so here just check if format
@@ -822,7 +824,7 @@ def _check_reversed(self, node: nodes.Call) -> None:
822824
except utils.NoSuchArgumentError:
823825
pass
824826
else:
825-
if argument is astroid.Uninferable:
827+
if isinstance(argument, util.UninferableBase):
826828
return
827829
if argument is None:
828830
# Nothing was inferred.

pylint/checkers/classes/class_checker.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import TYPE_CHECKING, Any, Union
1616

1717
import astroid
18-
from astroid import bases, nodes
18+
from astroid import bases, nodes, util
1919
from astroid.nodes import LocalsDictNodeNG
2020
from astroid.typing import SuccessfulInferenceResult
2121

@@ -459,7 +459,7 @@ def _is_attribute_property(name: str, klass: nodes.ClassDef) -> bool:
459459
return False
460460
property_name = "builtins.property"
461461
for attr in attributes:
462-
if attr is astroid.Uninferable:
462+
if isinstance(attr, util.UninferableBase):
463463
continue
464464
try:
465465
inferred = next(attr.infer())
@@ -1453,7 +1453,7 @@ def _check_slots(self, node: nodes.ClassDef) -> None:
14531453

14541454
for slots in node.ilookup("__slots__"):
14551455
# check if __slots__ is a valid type
1456-
if slots is astroid.Uninferable:
1456+
if isinstance(slots, util.UninferableBase):
14571457
continue
14581458
if not is_iterable(slots) and not is_comprehension(slots):
14591459
self.add_message("invalid-slots", node=node)
@@ -1471,7 +1471,7 @@ def _check_slots(self, node: nodes.ClassDef) -> None:
14711471
values = [item[0] for item in slots.items]
14721472
else:
14731473
values = slots.itered()
1474-
if values is astroid.Uninferable:
1474+
if isinstance(values, util.UninferableBase):
14751475
continue
14761476
for elt in values:
14771477
try:
@@ -1518,7 +1518,7 @@ def _check_slots_elt(
15181518
self, elt: SuccessfulInferenceResult, node: nodes.ClassDef
15191519
) -> None:
15201520
for inferred in elt.infer():
1521-
if inferred is astroid.Uninferable:
1521+
if isinstance(inferred, util.UninferableBase):
15221522
continue
15231523
if not isinstance(inferred, nodes.Const) or not isinstance(
15241524
inferred.value, str
@@ -1623,8 +1623,7 @@ def _check_invalid_class_object(self, node: nodes.AssignAttr) -> None:
16231623
else:
16241624
inferred = safe_infer(node.parent.value)
16251625
if (
1626-
isinstance(inferred, nodes.ClassDef)
1627-
or inferred is astroid.Uninferable
1626+
isinstance(inferred, (nodes.ClassDef, util.UninferableBase))
16281627
or inferred is None
16291628
):
16301629
# If is uninferable, we allow it to prevent false positives
@@ -2133,7 +2132,7 @@ def _check_init(self, node: nodes.FunctionDef, klass_node: nodes.ClassDef) -> No
21332132
# pylint: disable = too-many-try-statements
21342133
try:
21352134
for klass in expr.expr.infer():
2136-
if klass is astroid.Uninferable:
2135+
if isinstance(klass, util.UninferableBase):
21372136
continue
21382137
# The inferred klass can be super(), which was
21392138
# assigned to a variable and the `__init__`

pylint/checkers/classes/special_methods_checker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections.abc import Callable
1010

1111
import astroid
12-
from astroid import bases, nodes
12+
from astroid import bases, nodes, util
1313
from astroid.context import InferenceContext
1414
from astroid.typing import InferenceResult
1515

@@ -394,7 +394,7 @@ def _check_getnewargs_ex(
394394
if isinstance(arg, nodes.Call):
395395
arg = safe_infer(arg)
396396

397-
if arg and arg is not astroid.Uninferable:
397+
if arg and not isinstance(arg, util.UninferableBase):
398398
if not check(arg):
399399
found_error = True
400400
break

pylint/checkers/dunder_methods.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from typing import TYPE_CHECKING
88

9-
from astroid import Instance, Uninferable, nodes
9+
from astroid import Instance, nodes
10+
from astroid.util import UninferableBase
1011

1112
from pylint.checkers import BaseChecker
1213
from pylint.checkers.utils import safe_infer
@@ -77,7 +78,9 @@ def visit_call(self, node: nodes.Call) -> None:
7778
)
7879
):
7980
inf_expr = safe_infer(node.func.expr)
80-
if not (inf_expr in {None, Uninferable} or isinstance(inf_expr, Instance)):
81+
if not (
82+
inf_expr is None or isinstance(inf_expr, (Instance, UninferableBase))
83+
):
8184
# Skip dunder calls to non instantiated classes.
8285
return
8386

pylint/checkers/exceptions.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import TYPE_CHECKING, Any
1414

1515
import astroid
16-
from astroid import nodes, objects
16+
from astroid import nodes, objects, util
1717
from astroid.context import InferenceContext
1818
from astroid.typing import InferenceResult, SuccessfulInferenceResult
1919

@@ -46,11 +46,11 @@ def _annotated_unpack_infer(
4646
if isinstance(stmt, (nodes.List, nodes.Tuple)):
4747
for elt in stmt.elts:
4848
inferred = utils.safe_infer(elt)
49-
if inferred and inferred is not astroid.Uninferable:
49+
if inferred and not isinstance(inferred, util.UninferableBase):
5050
yield elt, inferred
5151
return
5252
for inferred in stmt.infer(context):
53-
if inferred is astroid.Uninferable:
53+
if isinstance(inferred, util.UninferableBase):
5454
continue
5555
yield stmt, inferred
5656

@@ -344,7 +344,7 @@ def visit_raise(self, node: nodes.Raise) -> None:
344344
ExceptionRaiseRefVisitor(self, node).visit(expr)
345345

346346
inferred = utils.safe_infer(expr)
347-
if inferred is None or inferred is astroid.Uninferable:
347+
if inferred is None or isinstance(inferred, util.UninferableBase):
348348
return
349349
ExceptionRaiseLeafVisitor(self, node).visit(inferred)
350350

@@ -375,7 +375,7 @@ def _check_bad_exception_cause(self, node: nodes.Raise) -> None:
375375
An exception cause can be only `None` or an exception.
376376
"""
377377
cause = utils.safe_infer(node.cause)
378-
if cause in (astroid.Uninferable, None):
378+
if cause is None or isinstance(cause, util.UninferableBase):
379379
return
380380

381381
if isinstance(cause, nodes.Const):
@@ -441,7 +441,7 @@ def _check_catching_non_exception(
441441
if isinstance(exc, nodes.Tuple):
442442
# Check if it is a tuple of exceptions.
443443
inferred = [utils.safe_infer(elt) for elt in exc.elts]
444-
if any(node is astroid.Uninferable for node in inferred):
444+
if any(isinstance(node, util.UninferableBase) for node in inferred):
445445
# Don't emit if we don't know every component.
446446
return
447447
if all(

pylint/checkers/refactoring/implicit_booleaness_checker.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import annotations
66

77
import astroid
8-
from astroid import bases, nodes
8+
from astroid import bases, nodes, util
99

1010
from pylint import checkers
1111
from pylint.checkers import utils
@@ -224,7 +224,9 @@ def _implicit_booleaness_message_args(
224224
return original_comparison, suggestion, description
225225

226226
@staticmethod
227-
def base_names_of_instance(node: bases.Uninferable | bases.Instance) -> list[str]:
227+
def base_names_of_instance(
228+
node: util.UninferableBase | bases.Instance,
229+
) -> list[str]:
228230
"""Return all names inherited by a class instance or those returned by a
229231
function.
230232

pylint/checkers/refactoring/refactoring_checker.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import astroid
1818
from astroid import bases, nodes
19-
from astroid.util import Uninferable
19+
from astroid.util import UninferableBase
2020

2121
from pylint import checkers
2222
from pylint.checkers import utils
@@ -1542,7 +1542,9 @@ def visit_return(self, node: nodes.Return | nodes.Assign) -> None:
15421542
return
15431543

15441544
inferred_truth_value = utils.safe_infer(truth_value, compare_constants=True)
1545-
if inferred_truth_value is None or inferred_truth_value == astroid.Uninferable:
1545+
if inferred_truth_value is None or isinstance(
1546+
inferred_truth_value, UninferableBase
1547+
):
15461548
return
15471549
truth_boolean_value = inferred_truth_value.bool_value()
15481550

@@ -1569,7 +1571,7 @@ def _append_context_managers_to_stack(self, node: nodes.Assign) -> None:
15691571
else:
15701572
assignees = [node.targets[0]]
15711573
values = [node.value]
1572-
if Uninferable in (assignees, values):
1574+
if any(isinstance(n, UninferableBase) for n in (assignees, values)):
15731575
return
15741576
for assignee, value in zip(assignees, values):
15751577
if not isinstance(value, nodes.Call):
@@ -1921,7 +1923,11 @@ def _is_raise_node_return_ended(self, node: nodes.Raise) -> bool:
19211923
# to infer it.
19221924
return True
19231925
exc = utils.safe_infer(node.exc)
1924-
if exc is None or exc is astroid.Uninferable or not hasattr(exc, "pytype"):
1926+
if (
1927+
exc is None
1928+
or isinstance(exc, UninferableBase)
1929+
or not hasattr(exc, "pytype")
1930+
):
19251931
return False
19261932
exc_name = exc.pytype().split(".")[-1]
19271933
handlers = utils.get_exception_handlers(node, exc_name)

pylint/checkers/stdlib.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import TYPE_CHECKING, Any, Dict, Set, Tuple
1212

1313
import astroid
14-
from astroid import nodes
14+
from astroid import nodes, util
1515
from astroid.typing import InferenceResult
1616

1717
from pylint import interfaces
@@ -540,7 +540,7 @@ def visit_call(self, node: nodes.Call) -> None:
540540
"""Visit a Call node."""
541541
self.check_deprecated_class_in_call(node)
542542
for inferred in utils.infer_all(node.func):
543-
if inferred is astroid.Uninferable:
543+
if isinstance(inferred, util.UninferableBase):
544544
continue
545545
if inferred.root().name in OPEN_MODULE:
546546
open_func_name: str | None = None
@@ -805,7 +805,7 @@ def _check_invalid_envvar_value(
805805
call_arg: InferenceResult | None,
806806
allow_none: bool,
807807
) -> None:
808-
if call_arg in (astroid.Uninferable, None):
808+
if call_arg is None or isinstance(call_arg, util.UninferableBase):
809809
return
810810

811811
name = infer.qname()
@@ -818,7 +818,7 @@ def _check_invalid_envvar_value(
818818
if emit:
819819
self.add_message(message, node=node, args=(name, call_arg.pytype()))
820820
else:
821-
self.add_message(message, node=node, args=(name, call_arg.pytype())) # type: ignore[union-attr]
821+
self.add_message(message, node=node, args=(name, call_arg.pytype()))
822822

823823
def deprecated_methods(self) -> set[str]:
824824
return self._deprecated_methods

pylint/checkers/strings.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import TYPE_CHECKING
1616

1717
import astroid
18-
from astroid import bases, nodes
18+
from astroid import bases, nodes, util
1919
from astroid.typing import SuccessfulInferenceResult
2020

2121
from pylint.checkers import BaseChecker, BaseRawFileChecker, BaseTokenChecker, utils
@@ -337,7 +337,7 @@ def visit_binop(self, node: nodes.BinOp) -> None:
337337
if (
338338
format_type is not None
339339
and arg_type
340-
and arg_type != astroid.Uninferable
340+
and not isinstance(arg_type, util.UninferableBase)
341341
and not arg_matches_format_type(arg_type, format_type)
342342
):
343343
self.add_message(
@@ -395,7 +395,7 @@ def visit_binop(self, node: nodes.BinOp) -> None:
395395
arg_type = utils.safe_infer(arg)
396396
if (
397397
arg_type
398-
and arg_type != astroid.Uninferable
398+
and not isinstance(arg_type, util.UninferableBase)
399399
and not arg_matches_format_type(arg_type, format_type)
400400
):
401401
self.add_message(
@@ -561,7 +561,7 @@ def _check_new_format_specifiers(
561561
if key not in named:
562562
continue
563563
argname = named[key]
564-
if argname in (astroid.Uninferable, None):
564+
if argname is None or isinstance(argname, util.UninferableBase):
565565
continue
566566
try:
567567
argument = utils.safe_infer(argname)
@@ -578,7 +578,7 @@ def _check_new_format_specifiers(
578578
previous = argument
579579
parsed: list[tuple[bool, str]] = []
580580
for is_attribute, specifier in specifiers:
581-
if previous is astroid.Uninferable:
581+
if isinstance(previous, util.UninferableBase):
582582
break
583583
parsed.append((is_attribute, specifier))
584584
if is_attribute:
@@ -611,7 +611,7 @@ def _check_new_format_specifiers(
611611
warn_error = True
612612
except astroid.InferenceError:
613613
break
614-
if previous is astroid.Uninferable:
614+
if isinstance(previous, util.UninferableBase):
615615
break
616616
else:
617617
try:

0 commit comments

Comments
 (0)