def test_runtime(self): class DP(IterDataPipe[Tuple[int, T_co]]): def __init__(self, datasource): self.ds = datasource @runtime_validation def __iter__(self) -> Iterator[Tuple[int, T_co]]: for d in self.ds: yield d dss = ([(1, '1'), (2, '2')], [(1, 1), (2, '2')]) for ds in dss: dp = DP(ds) # type: ignore[var-annotated] self.assertEqual(list(d for d in dp), ds) # Reset __iter__ self.assertEqual(list(d for d in dp), ds) dss = ([(1, 1), ('2', 2)], # type: ignore[assignment, list-item] [[1, '1'], [2, '2']], # type: ignore[list-item] [1, '1', 2, '2']) for ds in dss: dp = DP(ds) with self.assertRaisesRegex(RuntimeError, r"Expected an instance of subtype"): list(d for d in dp) with runtime_validation_disabled(): self.assertEqual(list(d for d in dp), ds) with runtime_validation_disabled(): self.assertEqual(list(d for d in dp), ds) with self.assertRaisesRegex(RuntimeError, r"Expected an instance of subtype"): list(d for d in dp)
def test_reinforce(self): T = TypeVar('T', int, str) class DP(IterDataPipe[T]): def __init__(self, ds): self.ds = ds @runtime_validation def __iter__(self) -> Iterator[T]: for d in self.ds: yield d ds = list(range(10)) # Valid type reinforcement dp = DP(ds).reinforce_type(int) self.assertTrue(dp.type, int) self.assertEqual(list(dp), ds) # Invalid type with self.assertRaisesRegex(TypeError, r"'expected_type' must be a type"): dp = DP(ds).reinforce_type(1) # Type is not subtype with self.assertRaisesRegex(TypeError, r"Expected 'expected_type' as subtype of"): dp = DP(ds).reinforce_type(float) # Invalid data at runtime dp = DP(ds).reinforce_type(str) with self.assertRaisesRegex(RuntimeError, r"Expected an instance as subtype"): list(dp) # Context Manager to disable the runtime validation with runtime_validation_disabled(): self.assertEqual(list(d for d in dp), ds)