def test_get_method_type_stub(self) -> None:
     assert get_method_type_stub(ServiceNameCatalog.s3, "Client",
                                 "copy_object", "CopySource")
     assert get_method_type_stub(ServiceNameCatalog.ec2, "Client",
                                 "create_tags", "Resources")
     assert (get_method_type_stub(ServiceNameCatalog.s3, "Client",
                                  "copy_object", "Unknown") is None)
     assert (get_method_type_stub(ServiceNameCatalog.logs, "Client",
                                  "copy_object", "Unknown") is None)
Пример #2
0
 def test_get_method_type_stub(self) -> None:
     self.assertTrue(
         get_method_type_stub(ServiceNameCatalog.s3, "Client",
                              "copy_object", "CopySource"))
     self.assertTrue(
         get_method_type_stub(ServiceNameCatalog.ec2, "Any", "create_tags",
                              "Resources"))
     self.assertIsNone(
         get_method_type_stub(ServiceNameCatalog.s3, "Client",
                              "copy_object", "Unknown"))
     self.assertIsNone(
         get_method_type_stub(ServiceNameCatalog.logs, "Client",
                              "copy_object", "Unknown"))
Пример #3
0
    def _parse_arguments(
        self,
        class_name: str,
        method_name: str,
        operation_name: str,
        shape: StructureShape,
    ) -> List[Argument]:
        result: List[Argument] = []
        required = shape.required_members
        for argument_name, argument_shape in shape.members.items():
            argument_alias = self._get_argument_alias(operation_name, argument_name)
            if argument_alias == "None":
                continue

            argument_type_stub = get_method_type_stub(
                self.service_name, class_name, method_name, argument_name
            )
            if argument_type_stub is not None:
                argument_type = argument_type_stub
            else:
                argument_type = self._parse_shape(argument_shape)
            argument = Argument(argument_alias, argument_type)
            if argument_name not in required:
                argument.default = Type.none
            result.append(argument)

        result.sort(key=lambda x: not x.required)
        return result
def parse_attributes(
    service_name: ServiceName, resource_name: str, resource: Boto3ServiceResource
) -> List[Attribute]:
    """
    Extract attributes from boto3 resource.

    Arguments:
        resource -- boto3 service resource.

    Returns:
        A list of Attribute structures.
    """
    result: List[Attribute] = []
    if not resource.meta.client:
        return result
    if not resource.meta.resource_model:
        return result

    service_model = resource.meta.client.meta.service_model
    if resource.meta.resource_model.shape:
        shape = service_model.shape_for(resource.meta.resource_model.shape)
        attributes = resource.meta.resource_model.get_attributes(shape)
        for name, attribute in attributes.items():
            argument_type = get_method_type_stub(service_name, resource_name, "_attributes", name)
            if argument_type is None:
                argument_type = get_type_from_docstring(attribute[1].type_name)
            result.append(Attribute(name, argument_type))

    return result
    def _parse_types(self, input_string: str) -> None:
        if ":type " not in input_string:
            return

        type_strings = [
            i for i in input_string.split("\n") if i.startswith(":type ")
        ]
        TypeDocGrammar.reset()
        for type_string in type_strings:
            try:
                match = TypeDocGrammar.type_definition.parseString(type_string)
            except ParseException as e:
                self.logger.warning(
                    f"Cannot parse type definition {type_string} for {self.prefix}"
                )
                self.logger.debug(e)
                continue

            match_dict = match.asDict()
            argument_name = match_dict["name"]
            type_str = match_dict["type_name"]
            argument = self._find_argument_or_append(argument_name)
            type_stub = get_method_type_stub(self.service_name,
                                             self.class_name, self.method_name,
                                             argument_name)
            if type_stub:
                argument.type = type_stub
            else:
                argument.type = get_type_from_docstring(type_str)
Пример #6
0
    def get_return_type(self, class_name: str,
                        method_name: str) -> Optional[FakeAnnotation]:
        type_stub = get_method_type_stub(self.service_name, class_name,
                                         method_name, "return")
        if type_stub:
            return type_stub

        return None
Пример #7
0
    def get_return_type(self, class_name: str, method_name: str) -> FakeAnnotation | None:
        """
        Get `class_name.method_name` return type annotation.
        """
        type_stub = get_method_type_stub(self.service_name, class_name, method_name, "return")
        if type_stub:
            return type_stub

        return None
Пример #8
0
    def _parse_return_type(self, class_name: str, method_name: str,
                           shape: Shape | None) -> FakeAnnotation:
        argument_type_stub = get_method_type_stub(self.service_name,
                                                  class_name, method_name,
                                                  "return")
        if argument_type_stub is not None:
            return argument_type_stub

        if shape:
            return self.parse_shape(shape, output=True)

        return Type.none
Пример #9
0
    def _parse_return_type(self, class_name: str, method_name: str,
                           shape: Optional[Shape]) -> FakeAnnotation:
        argument_type_stub = get_method_type_stub(self.service_name,
                                                  class_name, method_name,
                                                  "return")
        if argument_type_stub is not None:
            return argument_type_stub

        if shape:
            return self._parse_shape(shape)

        return Type.none
Пример #10
0
    def get_arguments(self, class_name: str, method_name: str,
                      func: FunctionType) -> List[Argument]:
        arguments = self._get_arguments_from_argspec(func)

        for argument in arguments:
            if argument.type_annotation is not Type.Any:
                continue

            type_stub = get_method_type_stub(self.service_name, class_name,
                                             method_name, argument.name)
            if type_stub is not None:
                argument.type_annotation = type_stub

        return arguments
Пример #11
0
    def get_arguments(self, class_name: str, method_name: str, func: MethodType) -> list[Argument]:
        """
        Get arguments from `class_name.method_name` method `func`.
        """
        arguments = self._get_arguments_from_argspec(func)

        for argument in arguments:
            if argument.type_annotation is not Type.Any:
                continue

            type_stub = get_method_type_stub(
                self.service_name, class_name, method_name, argument.name
            )
            if type_stub is not None:
                argument.type_annotation = type_stub

        return arguments
Пример #12
0
    def _parse_arguments(
        self,
        class_name: str,
        method_name: str,
        operation_name: str,
        shape: StructureShape,
        exclude_names: Iterable[str] = tuple(),
        optional_only: bool = False,
    ) -> list[Argument]:
        result: list[Argument] = []
        required = shape.required_members
        for argument_name, argument_shape in shape.members.items():
            if argument_name in exclude_names:
                continue
            argument_alias = self._get_argument_alias(operation_name,
                                                      argument_name)
            if argument_alias == "None":
                continue

            argument_type_stub = get_method_type_stub(self.service_name,
                                                      class_name, method_name,
                                                      argument_name)
            if argument_type_stub is Type.RemoveArgument:
                continue
            if argument_type_stub is not None:
                argument_type = argument_type_stub
            else:
                argument_type = self.parse_shape(argument_shape)
            argument = Argument(argument_alias, argument_type)
            if argument_name not in required:
                argument.default = Type.Ellipsis
            if optional_only and argument.required:
                continue

            # FIXME: https://github.com/boto/boto3/issues/2813
            # if not argument.required and argument.type_annotation:
            #     argument.type_annotation = Type.get_optional(argument.type_annotation)

            result.append(argument)

        result.sort(key=lambda x: not x.required)
        return result
Пример #13
0
def parse_attributes(
    service_name: ServiceName,
    resource_name: str,
    resource: Boto3ServiceResource,
    shape_parser: ShapeParser,
) -> list[Attribute]:
    """
    Extract attributes from boto3 resource.

    Arguments:
        resource -- boto3 service resource.

    Returns:
        A list of Attribute structures.
    """
    result: list[Attribute] = []
    if not resource.meta.client:
        return result
    if not resource.meta.resource_model:
        return result
    if not resource.meta.resource_model.shape:
        return result

    service_model = resource.meta.client.meta.service_model

    shape = service_model.shape_for(resource.meta.resource_model.shape)
    attributes = resource.meta.resource_model.get_attributes(shape)
    for name, attribute in attributes.items():
        attribute_type = get_method_type_stub(service_name, resource_name,
                                              "_attributes", name)
        if attribute_type is None:
            attribute_shape = attribute[1]
            attribute_type = shape_parser.parse_shape(attribute_shape,
                                                      output=True)
        result.append(Attribute(name, attribute_type))

    return result
    def _parse_arguments(
        self,
        class_name: str,
        method_name: str,
        operation_name: str,
        shape: StructureShape,
        exclude_names: Iterable[str] = tuple(),
        optional_only: bool = False,
    ) -> List[Argument]:
        result: List[Argument] = []
        required = shape.required_members
        for argument_name, argument_shape in shape.members.items():
            if argument_name in exclude_names:
                continue
            argument_alias = self._get_argument_alias(operation_name,
                                                      argument_name)
            if argument_alias == "None":
                continue

            argument_type_stub = get_method_type_stub(self.service_name,
                                                      class_name, method_name,
                                                      argument_name)
            if argument_type_stub is Type.RemoveArgument:
                continue
            if argument_type_stub is not None:
                argument_type = argument_type_stub
            else:
                argument_type = self._parse_shape(argument_shape)
            argument = Argument(argument_alias, argument_type)
            if argument_name not in required:
                argument.default = Type.none
            if optional_only and argument.required:
                continue
            result.append(argument)

        result.sort(key=lambda x: not x.required)
        return result