Skip to content

Don't depend on stringcase (closes #369) #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ strict_equality = True
[mypy-importlib_metadata]
ignore_missing_imports = True

[mypy-stringcase]
ignore_missing_imports = True

[mypy-typer]
ignore_missing_imports = True

32 changes: 20 additions & 12 deletions openapi_python_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import builtins
import re
from keyword import iskeyword
from typing import List

import stringcase
delimiters = " _-"


def sanitize(value: str) -> str:
""" Removes every character that isn't 0-9, A-Z, a-z, ' ', -, or _ """
return re.sub(r"[^\w _\-]+", "", value)
""" Removes every character that isn't 0-9, A-Z, a-z, or a known delimiter """
return re.sub(rf"[^\w{delimiters}]+", "", value)


def split_words(value: str) -> List[str]:
""" Split a string on non-capital letters and known delimiters """
value = " ".join(re.split("([A-Z]?[a-z0-9]+)", value))
return re.findall(rf"[^{delimiters}]+", value)


def fix_keywords(value: str) -> str:
Expand All @@ -25,22 +32,23 @@ def fix_reserved_words(value: str) -> str:
return value


def group_title(value: str) -> str:
value = re.sub(r"([A-Z]{2,})([A-Z][a-z]|[ \-_]|$)", lambda m: m.group(1).title() + m.group(2), value.strip())
value = re.sub(r"(^|[ _-])([A-Z])", lambda m: m.group(1) + m.group(2).lower(), value)
return value


def snake_case(value: str) -> str:
return fix_keywords(stringcase.snakecase(group_title(sanitize(value))))
words = split_words(sanitize(value))
value = "_".join(words).lower()
return fix_keywords(value)


def pascal_case(value: str) -> str:
return fix_keywords(stringcase.pascalcase(sanitize(value.replace(" ", ""))))
words = split_words(sanitize(value))
words = [word.capitalize() if not word.isupper() else word for word in words]
value = "".join(words)
Copy link
Collaborator

Choose a reason for hiding this comment

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

A side-effect of this is that a leading delimiter gets stripped. So _MyClass and __MyClass both become MyClass. This may not be a problem, but it is a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I wonder if we should stay consistent or not. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

After thinking about it a bit, I think it's better to remove the leading underscores since that has the semantics in Python of being private. Nothing in the generated interfaces should be private so... this new behavior seems better.

return fix_keywords(value)


def kebab_case(value: str) -> str:
return fix_keywords(stringcase.spinalcase(group_title(sanitize(value))))
words = split_words(sanitize(value))
value = "-".join(words).lower()
return fix_keywords(value)


def remove_string_escapes(value: str) -> str:
Expand Down
11 changes: 0 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ include = ["CHANGELOG.md", "openapi_python_client/py.typed"]
[tool.poetry.dependencies]
python = "^3.6"
jinja2 = "^2.11.1"
stringcase = "^1.2.0"
typer = "^0.3"
colorama = {version = "^0.4.3", markers = "sys_platform == 'win32'"}
shellingham = "^1.3.2"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_snake_case_from_pascal_with_acronyms():
assert utils.snake_case("HTTPResponse") == "http_response"
assert utils.snake_case("APIClientHTTPResponse") == "api_client_http_response"
assert utils.snake_case("OAuthClientHTTPResponse") == "o_auth_client_http_response"
assert utils.snake_case("S3Config") == "s3_config"


def test_snake_case_from_pascal():
Expand All @@ -20,6 +21,7 @@ def test_snake_case_from_pascal():

def test_snake_case_from_camel():
assert utils.snake_case("httpResponseLowerCamel") == "http_response_lower_camel"
assert utils.snake_case("connectionID") == "connection_id"


def test_kebab_case():
Expand Down Expand Up @@ -59,6 +61,7 @@ def test_to_valid_python_identifier():
("snake_case", "SnakeCase"),
("TLAClass", "TLAClass"),
("Title Case", "TitleCase"),
("s3_config", "S3Config"),
],
)
def test_pascalcase(before, after):
Expand Down