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

Function apply_poly

mypy/applytype.py:182–199  ·  view source on GitHub ↗

Make free type variables generic in the type if possible. This will translate the type `tp` while trying to create valid bindings for type variables `poly_tvars` while traversing the type. This follows the same rules as we do during semantic analysis phase, examples: * Callable[Ca

(tp: CallableType, poly_tvars: Sequence[TypeVarLikeType])

Source from the content-addressed store, hash-verified

180
181
182def apply_poly(tp: CallableType, poly_tvars: Sequence[TypeVarLikeType]) -> CallableType | None:
183 """Make free type variables generic in the type if possible.
184
185 This will translate the type `tp` while trying to create valid bindings for
186 type variables `poly_tvars` while traversing the type. This follows the same rules
187 as we do during semantic analysis phase, examples:
188 * Callable[Callable[[T], T], T] -> def [T] (def (T) -> T) -> T
189 * Callable[[], Callable[[T], T]] -> def () -> def [T] (T -> T)
190 * List[T] -> None (not possible)
191 """
192 try:
193 return tp.copy_modified(
194 arg_types=[t.accept(PolyTranslator(poly_tvars)) for t in tp.arg_types],
195 ret_type=tp.ret_type.accept(PolyTranslator(poly_tvars)),
196 variables=[],
197 )
198 except PolyTranslationError:
199 return None
200
201
202class PolyTranslationError(Exception):

Callers

nothing calls this directly

Calls 3

PolyTranslatorClass · 0.85
copy_modifiedMethod · 0.45
acceptMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…