diff --git a/python/ray/air/checkpoint.py b/python/ray/air/checkpoint.py index 061a9d199..00f3a5716 100644 --- a/python/ray/air/checkpoint.py +++ b/python/ray/air/checkpoint.py @@ -627,6 +627,12 @@ class Checkpoint: def __setstate__(self, state): self.__dict__.update(state) + def __fspath__(self): + raise TypeError( + "You cannot use `air.Checkpoint` objects directly as paths. " + "Use `Checkpoint.to_directory()` or `Checkpoint.as_directory()` instead." + ) + def get_preprocessor(self) -> Optional["Preprocessor"]: """Return the saved preprocessor, if one exists.""" diff --git a/python/ray/air/tests/test_checkpoints.py b/python/ray/air/tests/test_checkpoints.py index 49c940bce..c00b3cc8a 100644 --- a/python/ray/air/tests/test_checkpoints.py +++ b/python/ray/air/tests/test_checkpoints.py @@ -544,6 +544,12 @@ class PreprocessorCheckpointTest(unittest.TestCase): preprocessor = checkpoint.get_preprocessor() assert preprocessor.multiplier == 1 + def testAttrPath(self): + with tempfile.TemporaryDirectory() as tmpdir: + checkpoint = Checkpoint.from_directory(tmpdir) + with self.assertRaises(TypeError): + os.path.exists(checkpoint) + if __name__ == "__main__": import pytest