def __init__(self, ctx): # Make sure we have typing available as a dependency member_map = typing_overlay.copy() ast = ctx.loader.typing for cls in ast.classes: _, name = cls.name.rsplit(".", 1) if name not in member_map and pytd.IsContainer( cls) and cls.template: member_map[name] = overlay.build(name, TypingContainer) super().__init__(ctx, "typing", member_map, ast)
def __init__(self, vm): # Make sure we have typing available as a dependency if vm.python_version < (3, 5) and not vm.loader.can_see("typing"): vm.errorlog.import_error(vm.frames, "typing") member_map = typing_overlay.copy() ast = vm.loader.typing for cls in ast.classes: _, name = cls.name.rsplit(".", 1) if name not in member_map and pytd.IsContainer(cls) and cls.template: member_map[name] = overlay.build(name, TypingContainer) super().__init__(vm, "typing", member_map, ast)
def __init__(self, vm): member_map = { "Annotated": typing_overlay.typing_overlay["Annotated"], "Literal": typing_overlay.typing_overlay["Literal"], "Protocol": build_protocol, "runtime": build_runtime, # alias for runtime_checkable } ast = vm.loader.import_name("typing_extensions") for pyval in ast.aliases + ast.classes + ast.constants + ast.functions: # Any public typing_extensions members that are not explicitly implemented # are unsupported. _, name = pyval.name.rsplit(".", 1) if name.startswith("_"): continue if f"typing.{name}" not in vm.loader.typing: if name not in member_map: member_map[name] = overlay.build( name, typing_overlay.not_supported_yet) super().__init__(vm, "typing_extensions", member_map, ast)
def build_typechecking(ctx): return ctx.convert.true def build_cast(ctx): return Cast.make("cast", ctx, "typing") def build_final_decorator(ctx): return FinalDecorator.make("final", ctx, "typing") typing_overlay = { "Annotated": overlay.build("Annotated", Annotated), "Any": build_any, "Callable": overlay.build("Callable", Callable), "final": build_final_decorator, "Final": overlay.build("Final", Final), "Generic": overlay.build("Generic", Generic), "Literal": overlay.build("Literal", Literal), "NamedTuple": build_namedtuple, "NewType": build_newtype, "NoReturn": build_noreturn, "Optional": overlay.build("Optional", Optional), "Tuple": overlay.build("Tuple", Tuple), "TypeVar": build_typevar, "TypedDict": build_typeddict, "Union": Union, "TYPE_CHECKING": build_typechecking,
def build_typevar(vm): return TypeVar.make("TypeVar", vm, "typing", pyval_name="_typevar_new") def build_typechecking(vm): return vm.convert.true def build_cast(vm): return Cast.make("cast", vm, "typing") typing_overlay = { "Any": build_any, "Callable": overlay.build("Callable", Callable), "Generic": overlay.build("Generic", Generic), "Literal": overlay.build("Literal", Literal), "NamedTuple": build_namedtuple, "NewType": build_newtype, "NoReturn": build_noreturn, "Optional": overlay.build("Optional", Optional), "Tuple": overlay.build("Tuple", Tuple), "TypeVar": build_typevar, "Union": Union, "TYPE_CHECKING": build_typechecking, "cast": build_cast, "overload": build_overload, }
def build_typechecking(vm): return vm.convert.true def build_cast(vm): return Cast.make("cast", vm, "typing") def build_final(vm): vm.errorlog.not_supported_yet(vm.frames, "typing.final") return vm.convert.name_to_value("typing.final") typing_overlay = { "Annotated": overlay.build("Annotated", Annotated), "Any": build_any, "Callable": overlay.build("Callable", Callable), "final": build_final, "Generic": overlay.build("Generic", Generic), "Literal": overlay.build("Literal", Literal), "NamedTuple": build_namedtuple, "NewType": build_newtype, "NoReturn": build_noreturn, "Optional": overlay.build("Optional", Optional), "Tuple": overlay.build("Tuple", Tuple), "TypeVar": build_typevar, "TypedDict": overlay.build("TypedDict", not_supported_yet), "Union": Union, "TYPE_CHECKING": build_typechecking, "cast": build_cast,
name = escape.pack_namedtuple(name, field_names) ast = namedtuple_ast(name, field_names, python_version=self.vm.python_version) mapping = self._get_known_types_mapping() # A truly well-formed pyi for the namedtuple will have references to the new # namedtuple class itself for all `self` params and as the return type for # methods like __new__, _replace and _make. In order to do that, we need a # ClassType. cls_type = pytd.ClassType(name) mapping[name] = cls_type cls = ast.Lookup(name).Visit(visitors.ReplaceTypes(mapping)) cls_type.cls = cls # Make sure all NamedType nodes have been replaced by ClassType nodes with # filled cls pointers. cls.Visit(visitors.VerifyLookup()) # We can't build the PyTDClass directly, and instead must run it through # convert.constant_to_value first, for caching. instance = self.vm.convert.constant_to_value(cls, {}, self.vm.root_node) self.vm.trace_namedtuple(instance) return node, instance.to_variable(node) collections_overlay = { "namedtuple": overlay.build("namedtuple", NamedTupleBuilder.make), }