diff options
author | 2020-05-24 19:29:13 -0700 | |
---|---|---|
committer | 2020-05-24 19:31:45 -0700 | |
commit | 856cecbd2354d4cbdbace5a39b7eb9e3d3bf23c7 (patch) | |
tree | 52fd67b8f2272a10d45622ecbff74e517341ec88 /tests | |
parent | Improve error and error testing for increment (diff) |
Add support for Union type annotations for constants
Note that `Optional[x]` is just an alias for `Union[None, x]` so this
effectively supports `Optional` too.
This was especially troublesome because the redis password must be
unset/None in order to avoid authentication, but the test would complain
that `None` isn't a `str`. Setting to an empty string would pass the
test but then make redis authenticate and fail.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bot/test_constants.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/tests/bot/test_constants.py b/tests/bot/test_constants.py index dae7c066c..db9a9bcb0 100644 --- a/tests/bot/test_constants.py +++ b/tests/bot/test_constants.py @@ -1,4 +1,5 @@ import inspect +import typing import unittest from bot import constants @@ -8,7 +9,7 @@ class ConstantsTests(unittest.TestCase): """Tests for our constants.""" def test_section_configuration_matches_type_specification(self): - """The section annotations should match the actual types of the sections.""" + """"The section annotations should match the actual types of the sections.""" sections = ( cls @@ -19,8 +20,14 @@ class ConstantsTests(unittest.TestCase): for name, annotation in section.__annotations__.items(): with self.subTest(section=section, name=name, annotation=annotation): value = getattr(section, name) + annotation_args = typing.get_args(annotation) - if getattr(annotation, '_name', None) in ('Dict', 'List'): - self.skipTest("Cannot validate containers yet.") - - self.assertIsInstance(value, annotation) + if not annotation_args: + self.assertIsInstance(value, annotation) + else: + origin = typing.get_origin(annotation) + if origin is typing.Union: + is_instance = any(isinstance(value, arg) for arg in annotation_args) + self.assertTrue(is_instance) + else: + self.skipTest(f"Validating type {annotation} is unsupported.") |