Skip to content
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

Bump black version #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
]
dependencies = [
"autoflake==2.3.1",
"black==23.12.1",
"black==24.10.0",
"common-libs[client]@git+https://github.com/yugokato/common-libs",
"isort==5.13.2",
"inflect==7.0.0",
Expand Down
15 changes: 9 additions & 6 deletions src/openapi_test_client/libraries/api/api_client_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ def update_endpoint_functions(
# endpoint path and endpoint options
rf"(\n{tab}{{2}})?\"(?P<path>.+?)\"(?P<ep_options>,.+?)?(\n{tab})?\)\n"
# function def
rf"(?P<func_def>{tab}def (?P<func_name>.+?)\((?P<signature>.+?){tab}?\) -> {RestResponse.__name__}:\n)"
rf"(?P<func_def>{tab}def (?P<func_name>.+?)\((?P<signature>.+?){tab}?\) -> {RestResponse.__name__}:\n?)"
# docstring
rf"({tab}{{2}}(?P<docstring>\"{{3}}.*?\"{{3}})\n)?"
# function body
rf"(?P<func_body>\n*{tab}{{2}}(?:[^@]+|\.{{3}})\n)?$",
rf"(?P<func_body>(?:\n*{tab}{{2}}(?:[^@]+|\.{{3}})| \.{{3}})\n)?$",
flags=re.MULTILINE | re.DOTALL,
)

Expand Down Expand Up @@ -324,12 +324,12 @@ def update_existing_endpoints(target_api_class: type[APIClassType] = api_class):
endpoint_str = f"{method.upper()} {path}"
defined_endpoints.append((method, path))

# For troubleshooting
# # For troubleshooting
# print(
# f"{method.upper()} {path}:\n"
# f" - matched: {repr(matched.group(0))}\n"
# f" - decorators: {repr(decorators)}\n"
# f" - func_def: {repr(matched.group("func_def"))}\n"
# f" - decorators: {repr(matched.group('decorators'))}\n"
# f" - func_def: {repr(func_def)}\n"
# f" - func_name: {repr(func_name)}\n"
# f" - signature: {repr(signature)}\n"
# f" - docstring: {repr(docstring)}\n"
Expand Down Expand Up @@ -387,8 +387,11 @@ def update_existing_endpoints(target_api_class: type[APIClassType] = api_class):
updated_api_func_code = updated_api_func_code.replace(docstring, expected_docstring)
else:
updated_api_func_code = updated_api_func_code.replace(
func_def, func_def + f"{TAB * 2}{expected_docstring}\n"
func_def, func_def + f"\n{TAB * 2}{expected_docstring}\n"
)
if func_body == " ...\n":
# New black format after v24.1.0 (#3796)
updated_api_func_code = updated_api_func_code.replace(func_body, f"{TAB * 2}...\n")

# Update API function signatures
new_func_signature = endpoint_model_util.generate_func_signature_in_str(endpoint_model).replace(
Expand Down
9 changes: 3 additions & 6 deletions src/openapi_test_client/libraries/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,11 @@ class ParamGroup(tuple):
def is_required(self) -> bool:
return any(p.is_required for p in self)

class OneOf(ParamGroup):
...
class OneOf(ParamGroup): ...

class AnyOf(ParamGroup):
...
class AnyOf(ParamGroup): ...

class AllOf(ParamGroup):
...
class AllOf(ParamGroup): ...

@staticmethod
@freeze_args
Expand Down
11 changes: 8 additions & 3 deletions src/openapi_test_client/libraries/common/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import black
import isort
from common_libs.ansi_colors import ColorCodes, color
from common_libs.logging import get_logger

from openapi_test_client import _PROJECT_ROOT_DIR

logger = get_logger(__name__)
TAB = " " * 4


Expand All @@ -20,8 +22,11 @@ def format_code(code: str, remove_unused_imports: bool = True) -> str:
- Run isort
- Run black
"""

ast.parse(code)
try:
ast.parse(code)
except Exception:
logger.error(f"Failed to parse code:\n{code}")
raise

# Apply autoflake
code = autoflake.fix_code(code, remove_all_unused_imports=remove_unused_imports)
Expand All @@ -31,7 +36,6 @@ def format_code(code: str, remove_unused_imports: bool = True) -> str:

# Apply black
code = run_black(code)

return code


Expand All @@ -52,6 +56,7 @@ class _BlackCtx:
def __init__(self, src):
self.default_map = {}
self.params = {"src": src}
self.command = black.main

ctx = _BlackCtx((_PROJECT_ROOT_DIR,))
black.read_pyproject_toml(ctx, None, None)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_update_client(temp_app_client: OpenAPIClient, dry_run: bool, option: st
for code_to_delete in [
"from ..models.users import Metadata\n",
"metadata: Optional[Metadata] = Unset,\n",
f"{create_user_func_docstring}\n",
f"{TAB*2}{create_user_func_docstring}\n",
]:
assert code_to_delete in original_api_class_code
modified_api_class_code = modified_api_class_code.replace(code_to_delete, "")
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def EmptyParamModel() -> type[ParamModel]:
"""A ParamModel that has no attributes"""

@dataclass
class Model(ParamModel):
...
class Model(ParamModel): ...

return Model

Expand Down
Loading