Skip to content

Commit

Permalink
Change File and Directory trait descriptions to take into account the…
Browse files Browse the repository at this point in the history
… 'exists' flag (#1440)
  • Loading branch information
mdickinson authored Mar 15, 2021
1 parent 13b5c3e commit 268b4d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
15 changes: 15 additions & 0 deletions traits/tests/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
14 changes: 14 additions & 0 deletions traits/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class ExampleModel(HasTraits):
file_name = File(exists=True)

new_file_name = File(exists=False)


class FastExampleModel(HasTraits):
file_name = File()
Expand Down Expand Up @@ -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):

Expand Down
21 changes: 14 additions & 7 deletions traits/trait_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="",
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 268b4d9

Please sign in to comment.