test_importstring.py 888 B

123456789101112131415161718192021222324252627
  1. # Copyright (c) IPython Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. #
  4. # Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
  5. # also under the terms of the Modified BSD License.
  6. """Tests for traitlets.utils.importstring."""
  7. from __future__ import annotations
  8. import os
  9. from unittest import TestCase
  10. from traitlets.utils.importstring import import_item
  11. class TestImportItem(TestCase):
  12. def test_import_unicode(self):
  13. self.assertIs(os, import_item("os"))
  14. self.assertIs(os.path, import_item("os.path"))
  15. self.assertIs(os.path.join, import_item("os.path.join"))
  16. def test_bad_input(self):
  17. class NotAString:
  18. pass
  19. msg = "import_item accepts strings, not '%s'." % NotAString
  20. with self.assertRaisesRegex(TypeError, msg):
  21. import_item(NotAString()) # type:ignore[arg-type]