Пример #1
0
    def validate_types_shallow(self) -> None:
        """
        Compares the type annotations on a node's fields with those field's actual
        values at runtime. Raises a TypeError is a mismatch is found.

        Only validates the current node, not any of it's children. For a recursive
        version, see :func:`validate_types_deep`.

        If you're using a static type checker (highly recommended), this is useless.
        However, if your code doesn't use a static type checker, or if you're unable to
        statically type your code for some reason, you can use this method to help
        validate your tree.

        Some (non-typing) validation is done unconditionally during the construction of
        a node. That validation does not overlap with the work that
        :func:`validate_types_deep` does.
        """
        for f in fields(self):
            value = getattr(self, f.name)
            if not is_value_of_type(value, f.type):
                raise TypeError(
                    f"Expected an instance of {f.type!r} on "
                    + f"{type(self).__name__}'s '{f.name}' field, but instead got "
                    + f"an instance of {type(value)!r}"
                )
Пример #2
0
 def test_not_implemented(self) -> None:
     with self.assertRaises(NotImplementedError):
         # pyre-ignore Pyre doesn't like the params to AsyncGenerator
         is_value_of_type("something", AsyncGenerator[None, None])
Пример #3
0
 def test_basic_pass(self, value, expected_type) -> None:
     self.assertTrue(
         is_value_of_type(value, expected_type),
         f"value {value!r} was supposed to be of type {expected_type!r}",
     )
Пример #4
0
 def test_basic_fail(self, value: object,
                     expected_type: Type[object]) -> None:
     self.assertFalse(is_value_of_type(value, expected_type))
Пример #5
0
 def test_not_implemented(self) -> None:
     with self.assertRaises(NotImplementedError):
         is_value_of_type("something", AsyncGenerator[None, None])