Skip to content

Commit dc923b7

Browse files
committed
Code review
1 parent c738ed9 commit dc923b7

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

pylint/checkers/typecheck.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,10 +1817,9 @@ def _check_invalid_slice_index(self, node: nodes.Slice) -> None:
18171817
match index_type := safe_infer(index):
18181818
case _ if not index_type:
18191819
continue
1820-
case nodes.Const():
1820+
case nodes.Const(value=int() | None):
18211821
# Constants must be of type int or None
1822-
if isinstance(index_type.value, (int, type(None))):
1823-
continue
1822+
continue
18241823
case astroid.Instance():
18251824
# Instance values must be of type int, None or an object
18261825
# with __index__

pylint/checkers/variables.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -650,29 +650,30 @@ def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG] | None:
650650
def _inferred_to_define_name_raise_or_return(
651651
self,
652652
name: str,
653-
node: nodes.NodeNG,
653+
node: nodes.Try | nodes.With | nodes.For | nodes.While | nodes.Match | nodes.If,
654654
) -> bool:
655655
"""Return True if there is a path under this `if_node`
656656
that is inferred to define `name`, raise, or return.
657657
"""
658-
# Handle try and with
659-
if isinstance(node, nodes.Try):
660-
# Allow either a path through try/else/finally OR a path through ALL except handlers
661-
try_except_node = node
662-
if node.finalbody:
663-
try_except_node = next(
664-
(child for child in node.nodes_of_class(nodes.Try)),
665-
None,
658+
match node:
659+
case nodes.Try():
660+
# Allow either a path through try/else/finally OR a path through ALL except handlers
661+
try_except_node = node
662+
if node.finalbody:
663+
try_except_node = next(
664+
(child for child in node.nodes_of_class(nodes.Try)),
665+
None,
666+
)
667+
handlers = try_except_node.handlers if try_except_node else []
668+
return NamesConsumer._defines_name_raises_or_returns_recursive(
669+
name, node
670+
) or all(
671+
NamesConsumer._defines_name_raises_or_returns_recursive(
672+
name, handler
673+
)
674+
for handler in handlers
666675
)
667-
handlers = try_except_node.handlers if try_except_node else []
668-
return NamesConsumer._defines_name_raises_or_returns_recursive(
669-
name, node
670-
) or all(
671-
NamesConsumer._defines_name_raises_or_returns_recursive(name, handler)
672-
for handler in handlers
673-
)
674676

675-
match node:
676677
case nodes.With() | nodes.For() | nodes.While():
677678
return NamesConsumer._defines_name_raises_or_returns_recursive(
678679
name, node
@@ -684,12 +685,17 @@ def _inferred_to_define_name_raise_or_return(
684685
for case in node.cases
685686
)
686687
case nodes.If():
687-
pass
688+
return self._inferred_to_define_name_raise_or_return_for_if_node(
689+
name, node
690+
)
688691
case _: # pragma: no cover
689692
# The function is only called for Try, With, For, While, Match and
690693
# If nodes. All of which are being handled above.
691694
raise AssertionError
692695

696+
def _inferred_to_define_name_raise_or_return_for_if_node(
697+
self, name: str, node: nodes.If
698+
) -> bool:
693699
# Be permissive if there is a break or a continue
694700
if any(node.nodes_of_class(nodes.Break, nodes.Continue)):
695701
return True

0 commit comments

Comments
 (0)