[air] Raise error on path-like access for Checkpoints (#26970)

Calling e.g. `os.path.exists(checkpoint)` currently raises an TypeError, but we should make it more explicit and guide users towards the correct API.

Signed-off-by: Kai Fricke <kai@anyscale.com>
This commit is contained in:
Kai Fricke 2022-07-25 21:45:31 +01:00 committed by GitHub
parent 5315f1e643
commit df217d15e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View file

@ -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."""

View file

@ -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