MCPcopy Index your code
hub / github.com/python/mypy / check_for_comp

Method check_for_comp

mypy/checkexpr.py:5971–6004  ·  view source on GitHub ↗

Check the for_comp part of comprehensions. That is the part from 'for': ... for x in y if z Note: This adds the type information derived from the condlists to the current binder.

(self, e: GeneratorExpr | DictionaryComprehension)

Source from the content-addressed store, hash-verified

5969 )[0]
5970
5971 def check_for_comp(self, e: GeneratorExpr | DictionaryComprehension) -> None:
5972 """Check the for_comp part of comprehensions. That is the part from 'for':
5973 ... for x in y if z
5974
5975 Note: This adds the type information derived from the condlists to the current binder.
5976 """
5977 for index, sequence, conditions, is_async in zip(
5978 e.indices, e.sequences, e.condlists, e.is_async
5979 ):
5980 if is_async:
5981 _, sequence_type = self.chk.analyze_async_iterable_item_type(sequence)
5982 else:
5983 _, sequence_type = self.chk.analyze_iterable_item_type(sequence)
5984 if (
5985 isinstance(get_proper_type(sequence_type), UninhabitedType)
5986 and isinstance(index, NameExpr)
5987 and index.name == "_"
5988 ):
5989 # To preserve backward compatibility, avoid inferring Never for "_"
5990 sequence_type = AnyType(TypeOfAny.special_form)
5991
5992 self.chk.analyze_index_variables(index, sequence_type, True, e)
5993 for condition in conditions:
5994 self.accept(condition)
5995
5996 # values are only part of the comprehension when all conditions are true
5997 true_map, false_map = self.chk.find_isinstance_check(condition)
5998 self.chk.push_type_map(true_map)
5999
6000 if codes.REDUNDANT_EXPR in self.chk.options.enabled_error_codes:
6001 if mypy.checker.is_unreachable_map(true_map):
6002 self.msg.redundant_condition_in_comprehension(False, condition)
6003 elif mypy.checker.is_unreachable_map(false_map):
6004 self.msg.redundant_condition_in_comprehension(True, condition)
6005
6006 def visit_conditional_expr(self, e: ConditionalExpr, allow_none_return: bool = False) -> Type:
6007 self.accept(e.cond)

Calls 11

acceptMethod · 0.95
get_proper_typeFunction · 0.90
AnyTypeClass · 0.90
zipFunction · 0.85
isinstanceFunction · 0.85
find_isinstance_checkMethod · 0.80
push_type_mapMethod · 0.80

Tested by

no test coverage detected