示例#1
0
 def _typecheck(self, attr, value):
     expected_type = typecheck.get_arg_type_from_constructor_annotation(
         type(self), attr
     )
     if expected_type is None:
         return  # no type info :(
     typecheck.check_type(attr, value, expected_type)
示例#2
0
 def _typecheck(self, attr, value):
     expected_type = typecheck.get_arg_type_from_constructor_annotation(
         type(self), attr
     )
     if expected_type is None:
         return  # no type info :(
     typecheck.check_type(attr, value, expected_type)
示例#3
0
def test_check_tuple():
    with pytest.raises(TypeError):
        typecheck.check_type("foo", None, typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", (), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", (42, 42), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", ("42", 42), typing.Tuple[int, str])
    typecheck.check_type("foo", (42, "42"), typing.Tuple[int, str])
示例#4
0
def test_check_tuple():
    with pytest.raises(TypeError):
        typecheck.check_type("foo", None, typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", (), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", (42, 42), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", ("42", 42), typing.Tuple[int, str])
    typecheck.check_type("foo", (42, "42"), typing.Tuple[int, str])
示例#5
0
 def __init__(self, name: str, typespec: type, default: typing.Any,
              help: str,
              choices: typing.Optional[typing.Sequence[str]]) -> None:
     typecheck.check_type(name, default, typespec)
     self.name = name
     self.typespec = typespec
     self._default = default
     self.value = unset
     self.help = textwrap.dedent(help).strip().replace("\n", " ")
     self.choices = choices
示例#6
0
def test_check_type():
    typecheck.check_type("foo", 42, int)
    with pytest.raises(TypeError):
        typecheck.check_type("foo", 42, str)
    with pytest.raises(TypeError):
        typecheck.check_type("foo", None, str)
    with pytest.raises(TypeError):
        typecheck.check_type("foo", b"foo", str)
示例#7
0
def test_check_sequence():
    typecheck.check_type("foo", [10], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", ["foo"], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [10, "foo"], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [b"foo"], typing.Sequence[str])
示例#8
0
def test_check_type():
    typecheck.check_type("foo", 42, int)
    with pytest.raises(TypeError):
        typecheck.check_type("foo", 42, str)
    with pytest.raises(TypeError):
        typecheck.check_type("foo", None, str)
    with pytest.raises(TypeError):
        typecheck.check_type("foo", b"foo", str)
示例#9
0
 def __init__(
     self,
     name: str,
     typespec: type,
     default: typing.Any,
     help: str,
     choices: typing.Optional[typing.Sequence[str]]
 ) -> None:
     typecheck.check_type(name, default, typespec)
     self.name = name
     self.typespec = typespec
     self._default = default
     self.value = unset
     self.help = help
     self.choices = choices
示例#10
0
def test_check_union():
    typecheck.check_type("foo", 42, typing.Union[int, str])
    typecheck.check_type("foo", "42", typing.Union[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [], typing.Union[int, str])

    # Python 3.5 only defines __union_params__
    m = mock.Mock()
    m.__str__ = lambda self: "typing.Union"
    m.__union_params__ = (int,)
    typecheck.check_type("foo", 42, m)
示例#11
0
def test_check_io():
    typecheck.check_type("foo", io.StringIO(), typing.IO[str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", "foo", typing.IO[str])
示例#12
0
def test_check_sequence():
    typecheck.check_type("foo", [10], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", ["foo"], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [10, "foo"], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [b"foo"], typing.Sequence[str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", "foo", typing.Sequence[str])

    # Python 3.5.0 only defines __parameters__
    m = mock.Mock()
    m.__str__ = lambda self: "typing.Sequence"
    m.__parameters__ = (int,)
    typecheck.check_type("foo", [10], m)
示例#13
0
def test_check_io():
    typecheck.check_type("foo", io.StringIO(), typing.IO[str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", "foo", typing.IO[str])
示例#14
0
def test_check_tuple():
    typecheck.check_type("foo", (42, "42"), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", None, typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", (), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", (42, 42), typing.Tuple[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", ("42", 42), typing.Tuple[int, str])

    # Python 3.5 only defines __tuple_params__
    m = mock.Mock()
    m.__str__ = lambda self: "typing.Tuple"
    m.__tuple_params__ = (int, str)
    typecheck.check_type("foo", (42, "42"), m)
示例#15
0
 def set(self, value: typing.Any) -> None:
     typecheck.check_type(self.name, value, self.typespec)
     self.value = value
示例#16
0
def test_check_union():
    typecheck.check_type("foo", 42, typing.Union[int, str])
    typecheck.check_type("foo", "42", typing.Union[int, str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [], typing.Union[int, str])
示例#17
0
def test_check_sequence():
    typecheck.check_type("foo", [10], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", ["foo"], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [10, "foo"], typing.Sequence[int])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", [b"foo"], typing.Sequence[str])
    with pytest.raises(TypeError):
        typecheck.check_type("foo", "foo", typing.Sequence[str])

    # Python 3.5 only defines __parameters__
    m = mock.Mock()
    m.__str__ = lambda self: "typing.Sequence"
    m.__parameters__ = (int,)
    typecheck.check_type("foo", [10], m)