@@ -367,9 +367,7 @@ def _check_using_constant_test(
367
367
# Just forcing the generator to infer all elements.
368
368
# astroid.exceptions.InferenceError are false positives
369
369
# see https://github.com/pylint-dev/pylint/pull/8185
370
- if isinstance (inferred , nodes .FunctionDef ):
371
- call_inferred = list (inferred .infer_call_result (node ))
372
- elif isinstance (inferred , nodes .Lambda ):
370
+ if isinstance (inferred , (nodes .FunctionDef , nodes .Lambda )):
373
371
call_inferred = list (inferred .infer_call_result (node ))
374
372
except astroid .InferenceError :
375
373
call_inferred = None
@@ -711,37 +709,36 @@ def visit_call(self, node: nodes.Call) -> None:
711
709
# ignore the name if it's not a builtin (i.e. not defined in the
712
710
# locals nor globals scope)
713
711
if not (name in node .frame () or name in node .root ()):
714
- if name == "exec" :
715
- self .add_message ("exec-used" , node = node )
716
- elif name == "reversed" :
717
- self ._check_reversed (node )
718
- elif name == "eval" :
719
- self .add_message ("eval-used" , node = node )
712
+ match name :
713
+ case "exec" :
714
+ self .add_message ("exec-used" , node = node )
715
+ case "reversed" :
716
+ self ._check_reversed (node )
717
+ case "eval" :
718
+ self .add_message ("eval-used" , node = node )
720
719
721
720
@utils .only_required_for_messages ("assert-on-tuple" , "assert-on-string-literal" )
722
721
def visit_assert (self , node : nodes .Assert ) -> None :
723
722
"""Check whether assert is used on a tuple or string literal."""
724
- if isinstance (node .test , nodes .Tuple ) and len (node .test .elts ) > 0 :
725
- self .add_message ("assert-on-tuple" , node = node , confidence = HIGH )
726
-
727
- if isinstance (node .test , nodes .Const ) and isinstance (node .test .value , str ):
728
- if node .test .value :
729
- when = "never"
730
- else :
731
- when = "always"
732
- self .add_message ("assert-on-string-literal" , node = node , args = (when ,))
723
+ match node .test :
724
+ case nodes .Tuple (elts = elts ) if len (elts ) > 0 :
725
+ self .add_message ("assert-on-tuple" , node = node , confidence = HIGH )
726
+ case nodes .Const (value = str (val )):
727
+ when = "never" if val else "always"
728
+ self .add_message ("assert-on-string-literal" , node = node , args = (when ,))
733
729
734
730
@utils .only_required_for_messages ("duplicate-key" )
735
731
def visit_dict (self , node : nodes .Dict ) -> None :
736
732
"""Check duplicate key in dictionary."""
737
733
keys = set ()
738
734
for k , _ in node .items :
739
- if isinstance (k , nodes .Const ):
740
- key = k .value
741
- elif isinstance (k , nodes .Attribute ):
742
- key = k .as_string ()
743
- else :
744
- continue
735
+ match k :
736
+ case nodes .Const ():
737
+ key = k .value
738
+ case nodes .Attribute ():
739
+ key = k .as_string ()
740
+ case _:
741
+ continue
745
742
if key in keys :
746
743
self .add_message ("duplicate-key" , node = node , args = key )
747
744
keys .add (key )
@@ -827,41 +824,42 @@ def _check_reversed(self, node: nodes.Call) -> None:
827
824
except utils .NoSuchArgumentError :
828
825
pass
829
826
else :
830
- if isinstance (argument , util .UninferableBase ):
831
- return
832
- if argument is None :
833
- # Nothing was inferred.
834
- # Try to see if we have iter().
835
- if isinstance (node .args [0 ], nodes .Call ):
836
- try :
837
- func = next (node .args [0 ].func .infer ())
838
- except astroid .InferenceError :
839
- return
840
- if getattr (
841
- func , "name" , None
842
- ) == "iter" and utils .is_builtin_object (func ):
843
- self .add_message ("bad-reversed-sequence" , node = node )
844
- return
845
-
846
- if isinstance (argument , (nodes .List , nodes .Tuple )):
847
- return
827
+ match argument :
828
+ case util .UninferableBase ():
829
+ return
830
+ case None :
831
+ # Nothing was inferred.
832
+ # Try to see if we have iter().
833
+ if isinstance (node .args [0 ], nodes .Call ):
834
+ try :
835
+ func = next (node .args [0 ].func .infer ())
836
+ except astroid .InferenceError :
837
+ return
838
+ if getattr (
839
+ func , "name" , None
840
+ ) == "iter" and utils .is_builtin_object (func ):
841
+ self .add_message ("bad-reversed-sequence" , node = node )
842
+ return
848
843
849
- # dicts are reversible, but only from Python 3.8 onward. Prior to
850
- # that, any class based on dict must explicitly provide a
851
- # __reversed__ method
852
- if not self ._py38_plus and isinstance (argument , astroid .Instance ):
853
- if any (
854
- ancestor .name == "dict" and utils .is_builtin_object (ancestor )
855
- for ancestor in itertools .chain (
856
- (argument ._proxied ,), argument ._proxied .ancestors ()
857
- )
858
- ):
859
- try :
860
- argument .locals [REVERSED_PROTOCOL_METHOD ]
861
- except KeyError :
862
- self .add_message ("bad-reversed-sequence" , node = node )
844
+ case nodes .List () | nodes .Tuple ():
863
845
return
864
846
847
+ case astroid .Instance () if not self ._py38_plus :
848
+ # dicts are reversible, but only from Python 3.8 onward. Prior to
849
+ # that, any class based on dict must explicitly provide a
850
+ # __reversed__ method
851
+ if any (
852
+ ancestor .name == "dict" and utils .is_builtin_object (ancestor )
853
+ for ancestor in itertools .chain (
854
+ (argument ._proxied ,), argument ._proxied .ancestors ()
855
+ )
856
+ ):
857
+ try :
858
+ argument .locals [REVERSED_PROTOCOL_METHOD ]
859
+ except KeyError :
860
+ self .add_message ("bad-reversed-sequence" , node = node )
861
+ return
862
+
865
863
if hasattr (argument , "getattr" ):
866
864
# everything else is not a proper sequence for reversed()
867
865
for methods in REVERSED_METHODS :
@@ -910,15 +908,16 @@ def _check_self_assigning_variable(self, node: nodes.Assign) -> None:
910
908
# Unpacking a variable into the same name.
911
909
return
912
910
913
- if isinstance (node .value , nodes .Name ):
914
- if len (targets ) != 1 :
915
- return
916
- rhs_names = [node .value ]
917
- elif isinstance (node .value , nodes .Tuple ):
918
- rhs_count = len (node .value .elts )
919
- if len (targets ) != rhs_count or rhs_count == 1 :
920
- return
921
- rhs_names = node .value .elts
911
+ match node .value :
912
+ case nodes .Name ():
913
+ if len (targets ) != 1 :
914
+ return
915
+ rhs_names = [node .value ]
916
+ case nodes .Tuple ():
917
+ rhs_count = len (node .value .elts )
918
+ if len (targets ) != rhs_count or rhs_count == 1 :
919
+ return
920
+ rhs_names = node .value .elts
922
921
923
922
for target , lhs_name in zip (targets , rhs_names ):
924
923
if not isinstance (lhs_name , nodes .Name ):
0 commit comments