From e962df8fcca20766a56a83dab6e278bfbd8cae34 Mon Sep 17 00:00:00 2001 From: Matvey <38750524+mtovts@users.noreply.github.com> Date: Mon, 16 Aug 2021 18:54:29 +0300 Subject: [PATCH 1/3] Add `fix_reserved_words` usage --- openapi_python_client/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openapi_python_client/utils.py b/openapi_python_client/utils.py index c74598b70..4702e3341 100644 --- a/openapi_python_client/utils.py +++ b/openapi_python_client/utils.py @@ -54,7 +54,8 @@ def fix_reserved_words(value: str) -> str: def snake_case(value: str) -> str: """Converts to snake_case""" words = split_words(sanitize(value)) - return "_".join(words).lower() + value = "_".join(words).lower() + return fix_reserved_words(value) def pascal_case(value: str) -> str: From 56dae9ea6e8c24908d450ccf9c72fcd29a902977 Mon Sep 17 00:00:00 2001 From: Matvey <38750524+mtovts@users.noreply.github.com> Date: Mon, 16 Aug 2021 18:57:04 +0300 Subject: [PATCH 2/3] refactor --- openapi_python_client/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_python_client/utils.py b/openapi_python_client/utils.py index 4702e3341..35c8f5742 100644 --- a/openapi_python_client/utils.py +++ b/openapi_python_client/utils.py @@ -10,7 +10,7 @@ class PythonIdentifier(str): """A string which has been validated / transformed into a valid identifier for Python""" def __new__(cls, value: str, prefix: str) -> "PythonIdentifier": - new_value = fix_reserved_words(snake_case(sanitize(value))) + new_value = snake_case(sanitize(value)) if not new_value.isidentifier(): new_value = f"{prefix}{new_value}" From 5cdc816330ef4ec8083a5632b75c2750289a8bbc Mon Sep 17 00:00:00 2001 From: Matvey <38750524+mtovts@users.noreply.github.com> Date: Mon, 16 Aug 2021 19:42:50 +0300 Subject: [PATCH 3/3] Add `test_snake_case_fix_reserved_words` --- tests/test_utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index c50c8d0cc..26ab0f2d6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -36,6 +36,22 @@ def test_split_words(before, after): assert utils.split_words(before) == after +@pytest.mark.parametrize( + "reserved_word, expected", + [ + ("self", "self_"), + ("int", "int_"), + ("dict", "dict_"), + ("not_reserved", "not_reserved"), + ("type", "type"), + ("id", "id"), + ("None", "none"), + ], +) +def test_snake_case_fix_reserved_words(reserved_word: str, expected: str): + assert utils.snake_case(reserved_word) == expected + + def test_snake_case_uppercase_str(): assert utils.snake_case("HTTP") == "http" assert utils.snake_case("HTTP RESPONSE") == "http_response"