Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.
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
11 changes: 9 additions & 2 deletions conman/nav_tree/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ def test_split_path_with_dots(self):


class TestImportFromDottedPath(TestCase):
def assert_error_message(self, exception):
"""Check the exception's message is correct."""
message = 'An import path with two or more components is required.'
self.assertEqual(exception.args[0], message)

def test_empty(self):
"""An empty path cannot be imported."""
with self.assertRaises(ValueError):
with self.assertRaises(ValueError) as cm:
utils.import_from_dotted_path('')
self.assert_error_message(cm.exception)

def test_too_short(self):
"""A path with only one component cannot be imported."""
with self.assertRaises(ValueError):
with self.assertRaises(ValueError) as cm:
utils.import_from_dotted_path('antigravity')
self.assert_error_message(cm.exception)

def test_import_module(self):
"""A module can be imported by dotted path."""
Expand Down
7 changes: 6 additions & 1 deletion conman/nav_tree/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def import_from_dotted_path(path):

The path must have at least one dot.
"""
module_path, attr = path.rsplit('.', 1)
try:
module_path, attr = path.rsplit('.', 1)
except ValueError:
message = 'An import path with two or more components is required.'
raise ValueError(message) from None
Copy link
Owner

Choose a reason for hiding this comment

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

Oooh python 3 shiny! I've not used this feature yet... what is the advantage of using from None here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It hides the fact that this exception was caused by rsplit raising ValueError - the end user doesn't need to know that implementation detail.

See Pep 409 for more detail.

Copy link
Owner

Choose a reason for hiding this comment

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

I take it that it does still show the stacktrace, though?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

>>> try:
...  1/0
... except ZeroDivisionError:
...  raise TypeError
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
TypeError

>>> try:
...  1/0
... except ZeroDivisionError:
...  raise TypeError from None
... 
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
TypeError

Copy link
Owner

Choose a reason for hiding this comment

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

👍


module = importlib.import_module(module_path)
return getattr(module, attr)