MCPcopy Create free account
hub / github.com/python/mypy / visit_instance

Method visit_instance

mypy/applytype.py:272–303  ·  view source on GitHub ↗
(self, t: Instance)

Source from the content-addressed store, hash-verified

270 raise PolyTranslationError()
271
272 def visit_instance(self, t: Instance) -> Type:
273 if t.type.has_param_spec_type:
274 # We need this special-casing to preserve the possibility to store a
275 # generic function in an instance type. Things like
276 # forall T . Foo[[x: T], T]
277 # are not really expressible in current type system, but this looks like
278 # a useful feature, so let's keep it.
279 param_spec_index = next(
280 i for (i, tv) in enumerate(t.type.defn.type_vars) if isinstance(tv, ParamSpecType)
281 )
282 p = get_proper_type(t.args[param_spec_index])
283 if isinstance(p, Parameters):
284 found_vars = self.collect_vars(p)
285 self.bound_tvars |= set(found_vars)
286 new_args = [a.accept(self) for a in t.args]
287 self.bound_tvars -= set(found_vars)
288
289 repl = new_args[param_spec_index]
290 assert isinstance(repl, ProperType) and isinstance(repl, Parameters)
291 repl.variables = list(repl.variables) + list(found_vars)
292 return t.copy_modified(args=new_args)
293 # There is the same problem with callback protocols as with aliases
294 # (callback protocols are essentially more flexible aliases to callables).
295 if t.args and t.type.is_protocol and t.type.protocol_members == ["__call__"]:
296 if t.type in self.seen_aliases:
297 raise PolyTranslationError()
298 call = mypy.subtypes.find_member("__call__", t, t, is_operator=True)
299 assert call is not None
300 return call.accept(
301 PolyTranslator(self.poly_tvars, self.bound_tvars, self.seen_aliases | {t.type})
302 )
303 return super().visit_instance(t)

Callers

nothing calls this directly

Calls 11

collect_varsMethod · 0.95
get_proper_typeFunction · 0.90
nextFunction · 0.85
enumerateFunction · 0.85
isinstanceFunction · 0.85
setClass · 0.85
listClass · 0.85
PolyTranslatorClass · 0.85
acceptMethod · 0.45
copy_modifiedMethod · 0.45

Tested by

no test coverage detected