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)
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)})
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))
def assertTryCastNoneSuccess(self, tp: object) -> None: self.assertIs(None, trycast(tp, None, _FAILURE))
def assertTryCastFailure(self, tp: object, value: object) -> None: self.assertIs(None, trycast(tp, value))
def assertTryCastSuccess(self, tp: object, value: object) -> None: self.assertIs(value, trycast(tp, value))
def test_tuple_of_types(self) -> None: self.assertRaises(TypeError, lambda: trycast( (int, str), 1)) # type: ignore
def test_none(self) -> None: self.assertRaises(TypeError, lambda: trycast(None, None)) # type: ignore