Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def _parse_rtype(self, input_string: str) -> Optional[FakeAnnotation]:
        if ":rtype: " not in input_string:
            return None

        TypeDocGrammar.reset()
        rtype_string = input_string[input_string.index(":rtype: ") :].split("\n", 1)[0]
        try:
            match = TypeDocGrammar.rtype_definition.parseString(rtype_string)
        except ParseException as e:
            self.logger.warning(f"Cannot parse rtype for {self.prefix}: {e}")
            return None

        type_name = match.asDict()["type_name"]
        return get_type_from_docstring(type_name)
Ejemplo n.º 4
0
    def _fix_keys_typed_dict(
        self, typed_dict: TypeTypedDict, argument_line: TypeDocLine,
    ) -> None:
        for line in argument_line.indented:
            if not line.name:
                continue

            attribute = typed_dict.get_attribute(line.name)
            attribute.required = line.required
            if attribute.type_annotation is Type.Any:
                attribute.type_annotation = get_type_from_docstring(line.type_name)
            if not line.indented:
                continue

            self._fix_keys(attribute.type_annotation, line)
Ejemplo n.º 5
0
    def test_get_type_from_docstring(self) -> None:
        self.assertTrue(get_type_from_docstring("bytes"))

        with self.assertRaises(ValueError):
            get_type_from_docstring("unknown")
    def test_get_type_from_docstring(self) -> None:
        assert get_type_from_docstring("bytes")

        with pytest.raises(ValueError):
            get_type_from_docstring("unknown")