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

Conversation

mdickinson
Copy link
Member

This PR customises the trait description for the File and Directory trait types to take into account whether exists was true or not.

For the File trait, the description has changed from "a filename or object implementing the os.PathLike interface" to "a string or os.PathLike object" in the normal case and "a string or os.PathLike object referring to an existing file" for the File(exists=True) case. The changes for Directory are analogous.

Before:

>>> from traits.api import Directory, File, HasStrictTraits
>>> class A(HasStrictTraits):
...     home_dir = Directory(exists=True)
...     config_file = File(exists=True)
... 
>>> a = A()
>>> a.home_dir = 47
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 1524, in validate
    validated_value = super().validate(
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 350, in validate
    self.error(object, name, value)
  File "/Users/mdickinson/Enthought/ETS/traits/traits/base_trait_handler.py", line 74, in error
    raise TraitError(
traits.trait_errors.TraitError: The 'home_dir' trait of an A instance must be a directory name or an object implementing the os.PathLike interface, but a value of 47 <class 'int'> was specified.
>>> a.config_file = 47
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 1395, in validate
    validated_value = super().validate(object, name, value)
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 350, in validate
    self.error(object, name, value)
  File "/Users/mdickinson/Enthought/ETS/traits/traits/base_trait_handler.py", line 74, in error
    raise TraitError(
traits.trait_errors.TraitError: The 'config_file' trait of an A instance must be a filename or object implementing the os.PathLike interface, but a value of 47 <class 'int'> was specified.

After:

>>> from traits.api import Directory, File, HasStrictTraits
>>> class A(HasStrictTraits):
...     home_dir = Directory(exists=True)
...     config_file = File(exists=True)
... 
>>> a = A()
>>> a.home_dir = 47
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 1524, in validate
    validated_value = super().validate(
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 350, in validate
    self.error(object, name, value)
  File "/Users/mdickinson/Enthought/ETS/traits/traits/base_trait_handler.py", line 74, in error
    raise TraitError(
traits.trait_errors.TraitError: The 'home_dir' trait of an A instance must be a string or os.PathLike object referring to an existing directory, but a value of 47 <class 'int'> was specified.
>>> a.config_file = 47
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 1392, in validate
    validated_value = super().validate(object, name, value)
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 350, in validate
    self.error(object, name, value)
  File "/Users/mdickinson/Enthought/ETS/traits/traits/base_trait_handler.py", line 74, in error
    raise TraitError(
traits.trait_errors.TraitError: The 'config_file' trait of an A instance must be a string or os.PathLike object referring to an existing file, but a value of 47 <class 'int'> was specified.

Fixes #1439

@nicolasap-dm: Do you have bandwidth for a quick review?

Checklist

  • Tests
  • [ ] Update API reference (docs/source/traits_api_reference) N/A
  • [ ] Update User manual (docs/source/traits_user_manual) N/A
  • [ ] Update type annotation hints in traits-stubs N/A

Copy link

@nicolasap-dm nicolasap-dm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@mdickinson mdickinson merged commit 268b4d9 into master Mar 15, 2021
@mdickinson mdickinson deleted the fix/error-message-for-existing-file-or-directory-trait branch March 15, 2021 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The validation error message for a non-existing file doesn't mention that the file does not exist
2 participants