Exemplo n.º 1
0
    def build_attribute(self, parameter: Parameter) -> PycksonAttribute:
        python_name = parameter.name
        json_name = self.name_rule(parameter.name)
        optional = parameter.default is not Parameter.empty
        if parameter.annotation is Parameter.empty:
            raise TypeError('parameter {} in class {} has no type'.format(
                parameter.name, self.cls.__name__))
        if parameter.kind != Parameter.POSITIONAL_OR_KEYWORD:
            raise TypeError('pyckson only handle named parameters')
        is_optional, optional_type = inspect_optional_typing(
            parameter.annotation)
        if is_optional:
            return PycksonAttribute(
                python_name,
                json_name,
                optional_type,
                True,
                self.serializer_provider.get(optional_type, self.cls,
                                             python_name),
                self.parser_provider.get(optional_type, self.cls, python_name),
                force_default=parameter.default == Parameter.empty)

        return PycksonAttribute(
            python_name, json_name, parameter.annotation, optional,
            self.serializer_provider.get(parameter.annotation, self.cls,
                                         python_name),
            self.parser_provider.get(parameter.annotation, self.cls,
                                     python_name))
Exemplo n.º 2
0
def test_simple_type_should_not_be_optional():
    assert inspect_optional_typing(str) == (False, type(None))
    assert inspect_optional_typing(int) == (False, type(None))
Exemplo n.º 3
0
def test_other_unions_should_not_be_optional():
    assert inspect_optional_typing(Union[int, str]) == (False, int)
    assert inspect_optional_typing(Union[int, str, None]) == (False, int)
Exemplo n.º 4
0
def test_union_with_none_should_be_optional():
    assert inspect_optional_typing(Union[int, None]) == (True, int)
Exemplo n.º 5
0
def test_optional_should_be_optional():
    assert inspect_optional_typing(Optional[str]) == (True, str)
Exemplo n.º 6
0
def test_simple_typing_type_should_not_be_optional():
    assert inspect_optional_typing(List[int]) == (False, type(None))