diff --git a/traits/tests/test_directory.py b/traits/tests/test_directory.py index 0192d4e56..516798556 100644 --- a/traits/tests/test_directory.py +++ b/traits/tests/test_directory.py @@ -19,6 +19,8 @@ class ExampleModel(HasTraits): path = Directory(exists=True) + new_path = Directory(exists=False) + class ExistsBaseDirectory(HasTraits): path = BaseDirectory(value=pathlib.Path(gettempdir()), exists=True) @@ -122,3 +124,16 @@ def test_simple_accepts_any_pathlib(self): foo.path = pathlib.Path("!!!") self.assertIsInstance(foo.path, str) + + def test_info_text(self): + example_model = ExampleModel() + with self.assertRaises(TraitError) as exc_cm: + example_model.path = 47 + self.assertIn("a string or os.PathLike object", str(exc_cm.exception)) + self.assertIn( + "referring to an existing directory", str(exc_cm.exception)) + + with self.assertRaises(TraitError) as exc_cm: + example_model.new_path = 47 + self.assertIn("a string or os.PathLike object", str(exc_cm.exception)) + self.assertNotIn("exist", str(exc_cm.exception)) diff --git a/traits/tests/test_file.py b/traits/tests/test_file.py index 037d52f5d..b8f3c01dd 100644 --- a/traits/tests/test_file.py +++ b/traits/tests/test_file.py @@ -19,6 +19,8 @@ class ExampleModel(HasTraits): file_name = File(exists=True) + new_file_name = File(exists=False) + class FastExampleModel(HasTraits): file_name = File() @@ -66,6 +68,18 @@ def test_fast(self): example_model = FastExampleModel(file_name=__file__) example_model.path = "." + def test_info_text(self): + example_model = ExampleModel() + with self.assertRaises(TraitError) as exc_cm: + example_model.file_name = 47 + self.assertIn("a string or os.PathLike object", str(exc_cm.exception)) + self.assertIn("referring to an existing file", str(exc_cm.exception)) + + with self.assertRaises(TraitError) as exc_cm: + example_model.new_file_name = 47 + self.assertIn("a string or os.PathLike object", str(exc_cm.exception)) + self.assertNotIn("exist", str(exc_cm.exception)) + class TestCreateEditor(unittest.TestCase): diff --git a/traits/trait_types.py b/traits/trait_types.py index 32de9b357..cf302c65d 100644 --- a/traits/trait_types.py +++ b/traits/trait_types.py @@ -1360,9 +1360,6 @@ class BaseFile(BaseStr): not. """ - #: A description of the type of value this trait accepts: - info_text = "a filename or object implementing the os.PathLike interface" - def __init__( self, value="", @@ -1400,6 +1397,13 @@ def validate(self, object, name, value): self.error(object, name, value) + def info(self): + """ Return a description of the type of value this trait accepts. """ + description = "a string or os.PathLike object" + if self.exists: + description += " referring to an existing file" + return description + def create_editor(self): from traitsui.editors.file_editor import FileEditor @@ -1498,10 +1502,6 @@ class BaseDirectory(BaseStr): not. """ - #: A description of the type of value this trait accepts: - info_text = ("a directory name or an object implementing " - "the os.PathLike interface") - def __init__( self, value="", auto_set=False, entries=0, exists=False, **metadata ): @@ -1531,6 +1531,13 @@ def validate(self, object, name, value): self.error(object, name, value) + def info(self): + """ Return a description of the type of value this trait accepts. """ + description = "a string or os.PathLike object" + if self.exists: + description += " referring to an existing directory" + return description + def create_editor(self): from traitsui.editors.directory_editor import DirectoryEditor