diff --git a/cdd/__init__.py b/cdd/__init__.py index 5c8f3253..611b6e74 100644 --- a/cdd/__init__.py +++ b/cdd/__init__.py @@ -8,7 +8,7 @@ from logging import getLogger as get_logger __author__ = "Samuel Marks" -__version__ = "0.0.99rc8" +__version__ = "0.0.99rc9" __description__ = ( "Open API to/fro routes, models, and tests. " "Convert between docstrings, classes, methods, argparse, pydantic, and SQLalchemy." diff --git a/cdd/shared/pure_utils.py b/cdd/shared/pure_utils.py index 12764aa2..7eec3150 100644 --- a/cdd/shared/pure_utils.py +++ b/cdd/shared/pure_utils.py @@ -2,6 +2,7 @@ Pure utils for pure functions. For the same input will always produce the same input_str. """ +import string import typing from ast import Name, Str from collections import deque @@ -1057,8 +1058,8 @@ def ensure_valid_identifier(s): return "{}_".format(s) elif s[0].isdigit(): s = "_{}".format(s) - valid = frozenset("_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") - return "".join(filter(valid.__contains__, s)) + valid = frozenset("_{}{}".format(string.ascii_letters, string.digits)) + return "".join(filter(valid.__contains__, s)) or "_" def set_attr(obj, key, val): diff --git a/cdd/tests/test_shared/test_pure_utils.py b/cdd/tests/test_shared/test_pure_utils.py index 2a4d4034..c4201a52 100644 --- a/cdd/tests/test_shared/test_pure_utils.py +++ b/cdd/tests/test_shared/test_pure_utils.py @@ -14,6 +14,7 @@ blockwise, deindent, diff, + ensure_valid_identifier, find_module_filepath, get_module, identity, @@ -69,6 +70,19 @@ def test_blockwise(self) -> None: self.assertTupleEqual(tuple(blockwise("ABC")), (("A", "B"), ("C", None))) self.assertTupleEqual(tuple(blockwise("ABCD")), (("A", "B"), ("C", "D"))) + def test_ensure_valid_identifier(self) -> None: + """Tests that `ensure_valid_identifier` works""" + self.assertEqual(ensure_valid_identifier("_5"), "_5") + for ident in "foo", "bar", "can", "haz_", "_", "_5", "_a": + self.assertEqual(ensure_valid_identifier(ident), ident) + for ident in "6", "5": + self.assertEqual(ensure_valid_identifier(ident), "_{}".format(ident)) + for ident in "for", "while", "break", "continue", "def", "class": + self.assertEqual(ensure_valid_identifier(ident), "{}_".format(ident)) + for ident in "$", "-", "-%": + self.assertEqual(ensure_valid_identifier(ident), "_") + self.assertEqual(ensure_valid_identifier(""), "_") + def test_find_module_filepath(self) -> None: """tests that it can `find_module_filepath`""" self.assertEqual(