@@ -231,25 +231,26 @@ def _fix_dot_imports(
231231 if not isinstance (stmt , (nodes .ImportFrom , nodes .Import )):
232232 continue
233233 for imports in stmt .names :
234- second_name = None
235234 import_module_name = imports [0 ]
236- if import_module_name == "*" :
235+ second_name = (
237236 # In case of wildcard imports,
238237 # pick the name from inside the imported module.
239- second_name = name
240- else :
241- name_matches_dotted_import = False
242- if (
243- import_module_name .startswith (name )
244- and import_module_name .find ("." ) > - 1
245- ):
246- name_matches_dotted_import = True
247-
248- if name_matches_dotted_import or name in imports :
249- # Most likely something like 'xml.etree',
250- # which will appear in the .locals as 'xml'.
251- # Only pick the name if it wasn't consumed.
252- second_name = import_module_name
238+ name
239+ if import_module_name == "*"
240+ else
241+ # Most likely something like 'xml.etree',
242+ # which will appear in the .locals as 'xml'.
243+ # Only pick the name if it wasn't consumed.
244+ (
245+ import_module_name
246+ if (
247+ import_module_name .startswith (name )
248+ and import_module_name .find ("." ) > - 1
249+ )
250+ or name in imports
251+ else None
252+ )
253+ )
253254 if second_name and second_name not in names :
254255 names [second_name ] = stmt
255256 return sorted (names .items (), key = lambda a : a [1 ].fromlineno )
@@ -264,8 +265,7 @@ def _find_frame_imports(name: str, frame: nodes.LocalsDictNodeNG) -> bool:
264265 if name in _flattened_scope_names (frame .nodes_of_class (nodes .Global )):
265266 return False
266267
267- imports = frame .nodes_of_class ((nodes .Import , nodes .ImportFrom ))
268- for import_node in imports :
268+ for import_node in frame .nodes_of_class ((nodes .Import , nodes .ImportFrom )):
269269 for import_name , import_alias in import_node .names :
270270 # If the import uses an alias, check only that.
271271 # Otherwise, check only the import name.
@@ -689,8 +689,9 @@ def _inferred_to_define_name_raise_or_return(
689689 if _defines_name_raises_or_returns (name , node ):
690690 return True
691691
692- test = node .test .value if isinstance (node .test , nodes .NamedExpr ) else node .test
693- all_inferred = utils .infer_all (test )
692+ all_inferred = utils .infer_all (
693+ node .test .value if isinstance (node .test , nodes .NamedExpr ) else node .test
694+ )
694695 only_search_if = False
695696 only_search_else = True
696697
@@ -1591,22 +1592,25 @@ def _report_unfound_name_definition(
15911592 ):
15921593 return
15931594
1594- confidence = HIGH
1595- if node .name in current_consumer .names_under_always_false_test :
1596- confidence = INFERENCE
1597- elif node .name in current_consumer .consumed_uncertain :
1598- confidence = CONTROL_FLOW
1599-
1600- if node .name in current_consumer .names_defined_under_one_branch_only :
1601- msg = "possibly-used-before-assignment"
1602- else :
1603- msg = "used-before-assignment"
1595+ msg = (
1596+ "possibly-used-before-assignment"
1597+ if node .name in current_consumer .names_defined_under_one_branch_only
1598+ else "used-before-assignment"
1599+ )
16041600
16051601 self .add_message (
16061602 msg ,
16071603 args = node .name ,
16081604 node = node ,
1609- confidence = confidence ,
1605+ confidence = (
1606+ INFERENCE
1607+ if node .name in current_consumer .names_under_always_false_test
1608+ else (
1609+ CONTROL_FLOW
1610+ if node .name in current_consumer .consumed_uncertain
1611+ else HIGH
1612+ )
1613+ ),
16101614 )
16111615
16121616 def _filter_type_checking_import_from_consumption (
@@ -1778,10 +1782,11 @@ class D(Tp):
17781782 node , frame
17791783 )
17801784 in_ancestor_list = utils .is_ancestor_name (frame , node )
1781- if in_annotation_or_default_or_decorator or in_ancestor_list :
1782- frame_locals = frame .parent .scope ().locals
1783- else :
1784- frame_locals = frame .locals
1785+ frame_locals = (
1786+ frame .parent .scope ().locals
1787+ if in_annotation_or_default_or_decorator or in_ancestor_list
1788+ else frame .locals
1789+ )
17851790 return not (
17861791 (isinstance (frame , nodes .ClassDef ) or in_annotation_or_default_or_decorator )
17871792 and not _in_lambda_or_comprehension_body (node , frame )
@@ -1808,21 +1813,22 @@ def _loopvar_name(self, node: astroid.Name) -> None:
18081813 # scope lookup rules would need to be changed to return the initial
18091814 # assignment (which does not exist in code per se) as well as any later
18101815 # modifications.
1811- if (
1812- not astmts # pylint: disable=too-many-boolean-expressions
1813- or (
1814- astmts [0 ].parent == astmts [0 ].root ()
1815- and astmts [0 ].parent .parent_of (node )
1816- )
1817- or (
1818- astmts [0 ].is_statement
1819- or not isinstance (astmts [0 ].parent , nodes .Module )
1820- and astmts [0 ].statement ().parent_of (node )
1816+ _astmts = (
1817+ []
1818+ if (
1819+ not astmts
1820+ or (
1821+ astmts [0 ].parent == astmts [0 ].root ()
1822+ and astmts [0 ].parent .parent_of (node )
1823+ )
1824+ or (
1825+ astmts [0 ].is_statement
1826+ or not isinstance (astmts [0 ].parent , nodes .Module )
1827+ and astmts [0 ].statement ().parent_of (node )
1828+ )
18211829 )
1822- ):
1823- _astmts = []
1824- else :
1825- _astmts = astmts [:1 ]
1830+ else astmts [:1 ]
1831+ )
18261832 for i , stmt in enumerate (astmts [1 :]):
18271833 try :
18281834 astmt_statement = astmts [i ].statement ()
@@ -1971,9 +1977,8 @@ def _check_is_unused(
19711977 if name in self ._type_annotation_names :
19721978 return
19731979
1974- argnames = node .argnames ()
19751980 # Care about functions with unknown argument (builtins)
1976- if name in argnames :
1981+ if name in ( argnames := node . argnames ()) :
19771982 if node .name == "__new__" :
19781983 is_init_def = False
19791984 # Look for the `__init__` method in all the methods of the same class.
@@ -2009,18 +2014,26 @@ def _check_is_unused(
20092014 message_name = "possibly-unused-variable"
20102015 else :
20112016 if isinstance (stmt , nodes .Import ):
2012- if asname is not None :
2013- msg = f"{ qname } imported as { asname } "
2014- else :
2015- msg = f"import { name } "
2016- self .add_message ("unused-import" , args = msg , node = stmt )
2017+ self .add_message (
2018+ "unused-import" ,
2019+ node = stmt ,
2020+ args = (
2021+ f"import { name } "
2022+ if asname is None
2023+ else f"{ qname } imported as { asname } "
2024+ ),
2025+ )
20172026 return
20182027 if isinstance (stmt , nodes .ImportFrom ):
2019- if asname is not None :
2020- msg = f"{ qname } imported from { stmt .modname } as { asname } "
2021- else :
2022- msg = f"{ name } imported from { stmt .modname } "
2023- self .add_message ("unused-import" , args = msg , node = stmt )
2028+ self .add_message (
2029+ "unused-import" ,
2030+ node = stmt ,
2031+ args = (
2032+ f"{ name } imported from { stmt .modname } "
2033+ if asname is None
2034+ else f"{ qname } imported from { stmt .modname } as { asname } "
2035+ ),
2036+ )
20242037 return
20252038 message_name = "unused-variable"
20262039
@@ -2044,15 +2057,16 @@ def _is_name_ignored(
20442057 stmt : nodes .NodeNG ,
20452058 name : str ,
20462059 ) -> re .Pattern [str ] | re .Match [str ] | None :
2047- authorized_rgx = self .linter .config .dummy_variables_rgx
2048- if (
2049- isinstance (stmt , nodes .AssignName )
2050- and isinstance (stmt .parent , nodes .Arguments )
2051- or isinstance (stmt , nodes .Arguments )
2052- ):
2053- regex : re .Pattern [str ] = self .linter .config .ignored_argument_names
2054- else :
2055- regex = authorized_rgx
2060+ regex : re .Pattern [str ] = (
2061+ self .linter .config .ignored_argument_names
2062+ if (
2063+ isinstance (stmt , nodes .AssignName )
2064+ and isinstance (stmt .parent , nodes .Arguments )
2065+ or isinstance (stmt , nodes .Arguments )
2066+ )
2067+ else self .linter .config .dummy_variables_rgx
2068+ )
2069+
20562070 # See https://stackoverflow.com/a/47007761/2519059 to
20572071 # understand what this function return. Please do NOT use
20582072 # this elsewhere, this is confusing for no benefit
@@ -2068,12 +2082,9 @@ def _check_unused_arguments(
20682082 ) -> None :
20692083 is_method = node .is_method ()
20702084 klass = node .parent .frame ()
2071- if is_method and isinstance (klass , nodes .ClassDef ):
2072- confidence = (
2073- INFERENCE if utils .has_known_bases (klass ) else INFERENCE_FAILURE
2074- )
2075- else :
2076- confidence = HIGH
2085+ has_known_bases = isinstance (klass , nodes .ClassDef ) and utils .has_known_bases (
2086+ klass
2087+ )
20772088
20782089 if is_method :
20792090 # Don't warn for the first argument of a (non static) method
@@ -2109,7 +2120,16 @@ def _check_unused_arguments(
21092120 if name in nonlocal_names :
21102121 return
21112122
2112- self .add_message ("unused-argument" , args = name , node = stmt , confidence = confidence )
2123+ self .add_message (
2124+ "unused-argument" ,
2125+ args = name ,
2126+ node = stmt ,
2127+ confidence = (
2128+ HIGH
2129+ if not is_method or not has_known_bases
2130+ else (INFERENCE if has_known_bases else INFERENCE_FAILURE )
2131+ ),
2132+ )
21132133
21142134 def _check_late_binding_closure (self , node : nodes .Name ) -> None :
21152135 """Check whether node is a cell var that is assigned within a containing loop.
@@ -2198,10 +2218,9 @@ def _store_type_annotation_names(
21982218 self ,
21992219 node : nodes .For | nodes .Assign | nodes .With ,
22002220 ) -> None :
2201- type_annotation = node .type_annotation
2202- if not type_annotation :
2221+ if not (type_annotation := node .type_annotation ):
22032222 return
2204- self ._store_type_annotation_node (node . type_annotation )
2223+ self ._store_type_annotation_node (type_annotation )
22052224
22062225 def _check_self_cls_assign (self , node : nodes .Assign ) -> None :
22072226 """Check that self/cls don't get assigned."""
@@ -2257,10 +2276,9 @@ def _check_unpacking(
22572276 return
22582277
22592278 # Attempt to check unpacking is properly balanced
2260- values = _nodes_to_unpack (inferred )
22612279 details = _get_unpacking_extra_info (node , inferred )
22622280
2263- if values is not None :
2281+ if ( values := _nodes_to_unpack ( inferred )) is not None :
22642282 if len (targets ) != len (values ):
22652283 self ._report_unbalanced_unpacking (
22662284 node , inferred , targets , len (values ), details
@@ -2485,12 +2503,11 @@ def _check_imports(self, not_consumed: Consumption) -> None:
24852503
24862504 # Construct string for unused-wildcard-import message
24872505 for module , unused_list in unused_wildcard_imports .items ():
2488- if len (unused_list ) == 1 :
2489- arg_string = unused_list [0 ]
2490- else :
2491- arg_string = (
2492- f"{ ', ' .join (i for i in unused_list [:- 1 ])} and { unused_list [- 1 ]} "
2493- )
2506+ arg_string = (
2507+ unused_list [0 ]
2508+ if len (unused_list ) == 1
2509+ else (f"{ ', ' .join (i for i in unused_list [:- 1 ])} and { unused_list [- 1 ]} " )
2510+ )
24942511 self .add_message (
24952512 "unused-wildcard-import" , args = (arg_string , module [0 ]), node = module [1 ]
24962513 )
@@ -2616,8 +2633,7 @@ def visit_const(self, node: nodes.Const) -> None:
26162633 return
26172634
26182635 try :
2619- annotation = extract_node (node .value )
2620- self ._store_type_annotation_node (annotation )
2636+ self ._store_type_annotation_node (extract_node (node .value ))
26212637 except ValueError :
26222638 # e.g. node.value is white space
26232639 pass
@@ -2918,9 +2934,8 @@ def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks(
29182934 if closest_except_handler is None :
29192935 return uncertain_nodes
29202936 for other_node in found_nodes :
2921- other_node_statement = other_node .statement ()
29222937 # If the other statement is the except handler guarding `node`, it executes
2923- if other_node_statement is closest_except_handler :
2938+ if ( other_node_statement := other_node . statement ()) is closest_except_handler :
29242939 continue
29252940 # Ensure other_node is in a try block
29262941 (
@@ -2966,12 +2981,11 @@ def _uncertain_nodes_in_try_blocks_when_evaluating_finally_blocks(
29662981 ):
29672982 return uncertain_nodes
29682983 for other_node in found_nodes :
2969- other_node_statement = other_node .statement ()
29702984 (
29712985 other_node_try_finally_ancestor ,
29722986 child_of_other_node_try_finally_ancestor ,
29732987 ) = utils .get_node_first_ancestor_of_type_and_its_child (
2974- other_node_statement , nodes .Try
2988+ other_node . statement () , nodes .Try
29752989 )
29762990 if other_node_try_finally_ancestor is None :
29772991 continue
@@ -3009,9 +3023,10 @@ def _defined_in_function_definition(
30093023 node : nodes .NodeNG ,
30103024 frame : nodes .NodeNG ,
30113025) -> bool :
3012- in_annotation_or_default_or_decorator = False
3013- if isinstance (frame , nodes .FunctionDef ) and node .statement () is frame :
3014- in_annotation_or_default_or_decorator = (
3026+ return (
3027+ isinstance (frame , nodes .FunctionDef )
3028+ and node .statement () is frame
3029+ and (
30153030 (
30163031 node in frame .args .annotations
30173032 or node in frame .args .posonlyargs_annotations
@@ -3026,7 +3041,7 @@ def _defined_in_function_definition(
30263041 and (node is frame .returns or frame .returns .parent_of (node ))
30273042 )
30283043 )
3029- return in_annotation_or_default_or_decorator
3044+ )
30303045
30313046
30323047def _in_lambda_or_comprehension_body (
@@ -3303,9 +3318,11 @@ def _is_first_level_self_reference(
33033318 # Check if used as type annotation
33043319 # Break if postponed evaluation is enabled
33053320 if utils .is_node_in_type_annotation_context (node ):
3306- if not utils .is_postponed_evaluation_enabled (node ):
3307- return (ConsumerAction .CONTINUE , None )
3308- return (ConsumerAction .RETURN , None )
3321+ return (
3322+ ConsumerAction .RETURN
3323+ if utils .is_postponed_evaluation_enabled (node )
3324+ else ConsumerAction .CONTINUE
3325+ ), None
33093326 # Check if used as default value by calling the class
33103327 if isinstance (node .parent , nodes .Call ) and isinstance (
33113328 node .parent .parent , nodes .Arguments
@@ -3353,8 +3370,7 @@ def _comprehension_between_frame_and_node(node: nodes.Name) -> bool:
33533370
33543371
33553372def _get_value_length (value_node : nodes .NodeNG ) -> int :
3356- value_subnodes = _nodes_to_unpack (value_node )
3357- if value_subnodes is not None :
3373+ if (value_subnodes := _nodes_to_unpack (value_node )) is not None :
33583374 return len (value_subnodes )
33593375 if isinstance (value_node , nodes .Const ) and isinstance (
33603376 value_node .value , (str , bytes )
0 commit comments