예제 #1
0
def draw_shape_endpoint(request_json: object) -> None:
    # TODO: Eliminate need to use cast() here
    shape = cast(Optional[Shape], trycast(Shape, request_json))
    if shape is not None:
        draw_shape(shape)
    else:
        shapes_drawn.append(HTTP_400_BAD_REQUEST)
예제 #2
0
파일: tests.py 프로젝트: sbdchd/trycast
    def test_alias_to_dict_with_forwardrefs(self) -> None:
        # dict with forward refs
        # TODO: Find way to auto-resolve forward references
        #       inside collection types.
        self.assertRaises(
            # TODO: Check the error message. Make it reasonable,
            #       explaining the forward references could not be resolved.
            TypeError,
            lambda: trycast(tests_forwardrefs_example.PointForLabel,
                            {'Target': dict(x=50, y=50)}))

        # dict with forward refs that have been resolved
        self.assertTryCastSuccess(
            eval_type(tests_forwardrefs_example.PointForLabel,
                      tests_forwardrefs_example.__dict__, None),
            {'Target': dict(x=50, y=50)})
예제 #3
0
파일: tests.py 프로젝트: sbdchd/trycast
    def test_alias_to_union_with_forwardrefs(self) -> None:
        # Union with forward refs
        # TODO: Find way to auto-resolve forward references
        #       inside Union types.
        self.assertRaises(
            # TODO: Check the error message. Make it reasonable,
            #       explaining the forward references could not be resolved.
            TypeError,
            lambda: trycast(
                tests_forwardrefs_example.Shape,
                dict(type='circle', center=dict(x=50, y=50), radius=25)))

        # Union with forward refs that have been resolved
        self.assertTryCastSuccess(
            eval_type(tests_forwardrefs_example.Shape,
                      tests_forwardrefs_example.__dict__, None),
            dict(type='circle', center=dict(x=50, y=50), radius=25))
예제 #4
0
파일: tests.py 프로젝트: sbdchd/trycast
 def assertTryCastNoneSuccess(self, tp: object) -> None:
     self.assertIs(None, trycast(tp, None, _FAILURE))
예제 #5
0
파일: tests.py 프로젝트: sbdchd/trycast
 def assertTryCastFailure(self, tp: object, value: object) -> None:
     self.assertIs(None, trycast(tp, value))
예제 #6
0
파일: tests.py 프로젝트: sbdchd/trycast
 def assertTryCastSuccess(self, tp: object, value: object) -> None:
     self.assertIs(value, trycast(tp, value))
예제 #7
0
파일: tests.py 프로젝트: sbdchd/trycast
 def test_tuple_of_types(self) -> None:
     self.assertRaises(TypeError, lambda: trycast(
         (int, str), 1))  # type: ignore
예제 #8
0
파일: tests.py 프로젝트: sbdchd/trycast
 def test_none(self) -> None:
     self.assertRaises(TypeError,
                       lambda: trycast(None, None))  # type: ignore