Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change File and Directory trait descriptions to take into account the 'exists' flag. #1440

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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