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

Method visit_dictionary_comprehension

mypy/checkexpr.py:5935–5969  ·  view source on GitHub ↗

Type check a dictionary comprehension.

(self, e: DictionaryComprehension)

Source from the content-addressed store, hash-verified

5933 return self.check_call(constructor, [gen.left_expr], [nodes.ARG_POS], gen)[0]
5934
5935 def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type:
5936 """Type check a dictionary comprehension."""
5937 with self.chk.binder.frame_context(can_skip=True, fall_through=0):
5938 self.check_for_comp(e)
5939
5940 # Infer the type of the list comprehension by using a synthetic generic
5941 # callable type.
5942 ktdef = TypeVarType(
5943 "KT",
5944 "KT",
5945 id=TypeVarId(-1, namespace="<dict>"),
5946 values=[],
5947 upper_bound=self.object_type(),
5948 default=AnyType(TypeOfAny.from_omitted_generics),
5949 )
5950 vtdef = TypeVarType(
5951 "VT",
5952 "VT",
5953 id=TypeVarId(-2, namespace="<dict>"),
5954 values=[],
5955 upper_bound=self.object_type(),
5956 default=AnyType(TypeOfAny.from_omitted_generics),
5957 )
5958 constructor = CallableType(
5959 [ktdef, vtdef],
5960 [nodes.ARG_POS, nodes.ARG_POS],
5961 [None, None],
5962 self.chk.named_generic_type("builtins.dict", [ktdef, vtdef]),
5963 self.chk.named_type("builtins.function"),
5964 name="<dictionary-comprehension>",
5965 variables=[ktdef, vtdef],
5966 )
5967 return self.check_call(
5968 constructor, [e.key, e.value], [nodes.ARG_POS, nodes.ARG_POS], e
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':

Callers

nothing calls this directly

Calls 10

check_for_compMethod · 0.95
object_typeMethod · 0.95
check_callMethod · 0.95
TypeVarTypeClass · 0.90
TypeVarIdClass · 0.90
AnyTypeClass · 0.90
CallableTypeClass · 0.90
frame_contextMethod · 0.80
named_generic_typeMethod · 0.45
named_typeMethod · 0.45

Tested by

no test coverage detected