Exemplo n.º 1
0
    def get_type_defs_required_import_records(self) -> List[ImportRecord]:
        if not self.typed_dicts:
            return []

        import_records: Set[ImportRecord] = set()
        import_records.add(ImportRecord(ImportString("sys")))
        import_records.add(
            ImportRecord(
                ImportString("typing"),
                "TypedDict",
                min_version=(3, 8),
                fallback=ImportRecord(ImportString("typing_extensions"),
                                      "TypedDict"),
            ))
        for types_dict in self.typed_dicts:
            for type_annotation in types_dict.get_children_types():
                import_record = type_annotation.get_import_record()
                if not import_record or import_record.is_builtins():
                    continue
                if import_record.is_type_defs():
                    continue
                import_records.add(
                    import_record.get_external(self.service_name.module_name))

        return list(sorted(import_records))
 def test_is_type_defs(self) -> None:
     assert ImportRecord(ImportString("type_defs")).is_type_defs()
     assert ImportRecord(ImportString("service",
                                      "type_defs")).is_type_defs()
     assert not ImportRecord(ImportString("builtins")).is_type_defs()
     assert not ImportRecord(ImportString("other")).is_type_defs()
     assert not ImportRecord(ImportString("boto3")).is_builtins()
 def __init__(self,
              service_module_name: ServiceModuleName,
              name: str = "",
              alias: str = ""):
     self._local_source = ImportString(service_module_name.name)
     source = ImportString.parent() + self._local_source
     super().__init__(source, name=name, alias=alias)
Exemplo n.º 4
0
 def test_is_standalone(self) -> None:
     assert not ImportRecord(ImportString("test"), name="my").is_standalone()
     assert ImportRecord(ImportString("test")).is_standalone()
     assert ImportRecord(
         ImportString("test"),
         name="my",
         fallback=ImportRecord(ImportString("test2")),
     ).is_standalone()
 def setup_method(self) -> None:
     self.result = ImportRecordGroup(
         ImportString("typing"),
         [
             ImportRecord(ImportString("typing"), "Any"),
             ImportRecord(ImportString("typing"), "Text", "string"),
         ],
     )
Exemplo n.º 6
0
 def _get_client_import(self, service_name: ServiceName) -> ExternalImport:
     client_import = ExternalImport(
         source=ImportString.parent() +
         ImportString(ServiceModuleName.client.value),
         name=Client.get_class_name(service_name),
     )
     client_import.import_record = InternalImportRecord(
         ServiceModuleName.client, client_import.name)
     return client_import
Exemplo n.º 7
0
 def test_operations(self) -> None:
     assert ImportString("my") + ImportString("test") == "my.test"
     assert ImportString("my") < ImportString("test")
     assert ImportString("my", "test") == "my.test"
     assert ImportString("my", "test")
     assert not ImportString.empty()
     assert hash(ImportString("my")) != hash(ImportString.empty())
Exemplo n.º 8
0
 def test_operations(self) -> None:
     self.assertEqual(ImportString("my") + ImportString("test"), "my.test")
     self.assertTrue(ImportString("my") < ImportString("test"))
     self.assertTrue(ImportString("my", "test") == "my.test")
     self.assertTrue(ImportString("my", "test"))
     self.assertFalse(ImportString.empty())
     self.assertTrue(hash(ImportString("my")) != hash(ImportString.empty()))
Exemplo n.º 9
0
    def get_helpers_import_record_groups(self) -> List[ImportRecordGroup]:
        import_records: Set[ImportRecord] = set()
        import_records.add(ImportRecord(ImportString("boto3")))
        import_records.add(ImportRecord(ImportString("typing"), "Dict"))
        import_records.add(ImportRecord(ImportString("typing"), "Any"))
        for helper_function in self.helper_functions:
            for type_annotation in helper_function.get_types():
                import_record = type_annotation.get_import_record()
                import_records.add(import_record)

        return ImportRecordGroup.from_import_records(import_records)
Exemplo n.º 10
0
 def get_import_record(self) -> ImportRecord:
     """
     Get import record required for using type annotation.
     """
     return ImportRecord(
         ImportString("typing"),
         "Literal",
         min_version=(3, 8),
         fallback=ImportRecord(ImportString("typing_extensions"),
                               "Literal"),
     )
Exemplo n.º 11
0
    def get_typing_import_record() -> ImportRecord:
        """
        Get import record required for using TypedDict.

        Fallback to typing_extensions for py38-.
        """
        return ImportRecord(
            ImportString("typing"),
            "TypedDict",
            min_version=(3, 9),
            fallback=ImportRecord(ImportString("typing_extensions"), "TypedDict"),
        )
Exemplo n.º 12
0
 def get_import_record(self) -> ImportRecord:
     """
     Create a safe Import Record for annotation.
     """
     if self.has_fallback():
         return ImportRecord(
             source=ImportString("typing"),
             name=self.get_import_name(),
             fallback=ImportRecord(source=ImportString("typing_extensions"),
                                   name=self.get_import_name()),
         )
     return ImportRecord(source=ImportString("typing"),
                         name=self.get_import_name())
Exemplo n.º 13
0
    def get_import_record(self) -> ImportRecord:
        """
        Get import record required for using type annotation.
        """
        if self.inline:
            return ImportRecord(
                ImportString("typing"),
                "Literal",
                min_version=(3, 8),
                fallback=ImportRecord(ImportString("typing_extensions"), "Literal"),
            )

        return InternalImportRecord(ServiceModuleName.literals, name=self.name)
    def get_init_import_records(self) -> List[ImportRecord]:
        """
        Get import records for `__init__.py[i]`.
        """
        import_records: Set[ImportRecord] = set()
        import_records.add(
            ImportRecord(
                ImportString.parent() + ImportString(ServiceModuleName.client.name),
                self.client.name,
            )
        )
        if self.service_resource:
            import_records.add(
                ImportRecord(
                    ImportString.parent() + ImportString(ServiceModuleName.service_resource.name),
                    self.service_resource.name,
                )
            )
        for waiter in self.waiters:
            import_records.add(
                ImportRecord(
                    ImportString.parent() + ImportString(ServiceModuleName.waiter.name),
                    waiter.name,
                )
            )
        for paginator in self.paginators:
            import_records.add(
                ImportRecord(
                    ImportString.parent() + ImportString(ServiceModuleName.paginator.name),
                    paginator.name,
                )
            )

        return list(sorted(import_records))
    def get_client_required_import_records(self) -> List[ImportRecord]:
        """
        Get import records for `client.py[i]`.
        """
        import_records: Set[ImportRecord] = set()
        for import_record in self.client.get_required_import_records():
            import_records.add(import_record.get_external(self.service_name.module_name))
            if import_record.fallback:
                import_records.add(ImportRecord(ImportString("sys")))
        for import_record in self.client.exceptions_class.get_required_import_records():
            import_records.add(import_record.get_external(self.service_name.module_name))
            if import_record.fallback:
                import_records.add(ImportRecord(ImportString("sys")))

        return list(sorted(import_records))
 def get_literals_required_import_records(self) -> List[ImportRecord]:
     """
     Get import records for `literals.py[i]`.
     """
     import_records: Set[ImportRecord] = set()
     import_records.add(ImportRecord(ImportString("sys")))
     import_records.add(
         ImportRecord(
             ImportString("typing"),
             "Literal",
             min_version=(3, 8),
             fallback=ImportRecord(ImportString("typing_extensions"), "Literal"),
         )
     )
     return list(sorted(import_records))
Exemplo n.º 17
0
 def test_init(self) -> None:
     service_name_mock = MagicMock()
     service_name_mock.name = "service_name"
     result = InternalImportRecord(service_name_mock, "name", "alias")
     assert result.source == ImportString("service_name")
     assert result.name == "name"
     assert result.alias == "alias"
Exemplo n.º 18
0
 def get_required_import_records(self) -> set[ImportRecord]:
     """
     Extract import records from required type annotations.
     """
     result = super().get_required_import_records()
     result.add(ImportRecord(ImportString("typing"), "Dict"))
     return result
Exemplo n.º 19
0
 def __init__(
     self,
     name: str,
     service_name: ServiceName,
     boto3_service_resource: Boto3ServiceResource,
 ):
     self.resource_meta_class = self._get_resource_meta_class(service_name)
     super().__init__(
         name=name,
         bases=[
             ExternalImport(
                 source=ImportString("boto3", "resources", "base"),
                 name="ServiceResource",
                 alias="Boto3ServiceResource",
             )
         ],
         attributes=[
             Attribute(
                 "meta",
                 InternalImport(
                     self.resource_meta_class.name,
                     service_name,
                     ServiceModuleName.service_resource,
                 ),
             )
         ],
     )
     self.service_name = service_name
     self.boto3_service_resource = boto3_service_resource
     self.collections: list[Collection] = []
     self.sub_resources: list[Resource] = []
    def from_import_records(
            cls, import_records: Iterable[ImportRecord]
    ) -> List["ImportRecordGroup"]:
        """
        Get groups from `ImportRecord` list.

        Arguments:
            import_records -- Import records list.

        Returns:
            A list of generated `ImportRecordGroup`.
        """
        result: List[ImportRecordGroup] = []
        all_import_records: Set[ImportRecord] = set(import_records)

        for import_record in import_records:
            if import_record.fallback:
                all_import_records.add(ImportRecord(ImportString("sys")))

        for import_record in sorted(all_import_records):
            if not import_record:
                continue
            if import_record.is_builtins():
                continue
            if (not result or result[-1].source != import_record.source
                    or not result[-1].import_records[0].name
                    or import_record.is_standalone()
                    or result[-1].import_records[0].is_standalone()):
                result.append(
                    ImportRecordGroup(import_record.source, [import_record]))
            else:
                result[-1].import_records.append(import_record)

        return result
Exemplo n.º 21
0
class ImportRecord:
    def __init__(self, source: str, name: str = "", alias: str = "") -> None:
        self.source = ImportString(source)
        self.name = name
        self.alias = alias

    def __str__(self) -> str:
        if self.name and self.alias:
            return f"from {self.source} import {self.name} as {self.alias}"
        if self.name:
            return f"from {self.source} import {self.name}"
        if self.alias:
            return f"import {self.source} as {self.alias}"
        return f"import {self.source}"

    def __hash__(self) -> int:
        return hash(str(self))

    def __eq__(self, other: Any) -> bool:
        if not isinstance(other, ImportRecord):
            raise ValueError(f"Cannot compare ImportString with {other}")

        return str(self) == str(other)

    def __gt__(self, other: ImportRecord) -> bool:
        if self.source == other.source:
            return self.name > other.name

        return self.source > other.source

    def render(self) -> str:
        return str(self)

    def get_local_name(self) -> str:
        return self.alias or self.name or self.source.render()
Exemplo n.º 22
0
 def __init__(
         self,
         local_modules: Iterable[str],
         common_import_records: Iterable[ImportRecord] = (),
 ) -> None:
     self.common_import_records = common_import_records
     self.local_import_strings = [ImportString(i) for i in local_modules]
Exemplo n.º 23
0
 def __init__(self,
              service_module_name: ServiceModuleName,
              name: str = "",
              alias: str = ""):
     super().__init__(ImportString(service_module_name.name),
                      name=name,
                      alias=alias)
Exemplo n.º 24
0
    def _get_type_func_call(self) -> FakeAnnotation:
        if not self.func_call:
            raise ValueError(f"Value is not a func call: {self.raw}")

        if self.func_call["name"] == "datetime":
            return ExternalImport(ImportString("datetime"), "datetime")

        if self.func_call["name"] == "StreamingBody":
            return ExternalImport(ImportString("botocore", "response"),
                                  "StreamingBody")

        if self.func_call["name"] == "EventStream":
            return ExternalImport(ImportString("botocore", "eventstream"),
                                  "EventStream")

        self.logger.warning(f"Unknown function: {self.raw}, fallback to Any")
        return Type.Any
 def _make_async_client(self) -> None:
     self.package.client.bases = [
         ExternalImport(ImportString("aiobotocore", "client"), "AioBaseClient")
     ]
     for method in self.package.client.methods:
         if method.name in ["exceptions", "get_waiter", "get_paginator", "can_paginate"]:
             continue
         method.is_async = True
 def _make_async_paginators(self) -> None:
     for paginator in self.package.paginators:
         paginator.bases = [
             ExternalImport(ImportString("aiobotocore", "paginate"), "AioPaginator")
         ]
         paginate_method = paginator.get_method("paginate")
         assert isinstance(paginate_method.return_type, TypeSubscript)
         paginate_method.return_type.parent = Type.AsyncIterator
Exemplo n.º 27
0
 def test_comparison(self) -> None:
     local_source = ImportString("mypy_boto3")
     third_party_source = ImportString("boto3")
     other_source = ImportString("other")
     assert ImportRecord(local_source, "test") > ImportRecord(local_source, "name")
     assert not (ImportRecord(third_party_source, "test") > ImportRecord(local_source, "name"))
     assert not (ImportRecord(other_source, "test") > ImportRecord(third_party_source, "name"))
     assert ImportRecord(local_source, "name") > ImportRecord(third_party_source, "test")
     assert ImportRecord(local_source, "name") > ImportRecord(other_source, "test")
     assert ImportRecord(third_party_source, "name") > ImportRecord(other_source, "test")
     assert ImportRecord(ImportString("zzz")) > ImportRecord(ImportString("aaa"))
     assert ImportRecord(
         local_source, "test", fallback=ImportRecord(local_source, "test2")
     ) > ImportRecord(local_source, "name")
     assert not (
         ImportRecord(local_source, "name")
         > ImportRecord(local_source, "test", fallback=ImportRecord(local_source, "test2"))
     )
Exemplo n.º 28
0
 def test_is_third_party(self) -> None:
     assert not ImportRecord(ImportString("type_defs")).is_third_party()
     assert not ImportRecord(ImportString("builtins")).is_third_party()
     assert not ImportRecord(ImportString("other")).is_third_party()
     assert ImportRecord(ImportString("boto3")).is_third_party()
     assert ImportRecord(ImportString("boto3", "test")).is_third_party()
     assert ImportRecord(ImportString("botocore")).is_third_party()
     assert ImportRecord(ImportString("botocore", "test")).is_third_party()
Exemplo n.º 29
0
    def get_waiter_required_import_records(self) -> List[ImportRecord]:
        import_records: Set[ImportRecord] = set()
        for waiter in self.waiters:
            for import_record in waiter.get_required_import_records():
                import_records.add(
                    import_record.get_external(self.service_name.module_name))
                if import_record.fallback:
                    import_records.add(ImportRecord(ImportString("sys")))

        return list(sorted(import_records))
Exemplo n.º 30
0
 def get_client_method(self) -> Method:
     """
     Get `get_waiter` method for `Client`.
     """
     return Method(
         name="get_waiter",
         decorators=[Type.overload],
         docstring=self.docstring,
         arguments=[
             Argument("self", None),
             Argument("waiter_name",
                      TypeLiteral(f"{self.name}Name", [self.waiter_name])),
         ],
         return_type=ExternalImport(
             source=ImportString.parent() +
             ImportString(ServiceModuleName.waiter.value),
             name=self.name,
         ),
     )