Skip to content

Commit

Permalink
Merge pull request #270 from mirumee/fix-tests
Browse files Browse the repository at this point in the history
Fix blank line between class and first method not being formatted out.
  • Loading branch information
rafalp committed Jan 29, 2024
2 parents 3b8164a + 29dee20 commit c1a0548
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
18 changes: 9 additions & 9 deletions ariadne_codegen/client_generators/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ def add_method(
raise NotSupported(
"Subscriptions are only available when using async client."
)
method_def: Union[
ast.FunctionDef, ast.AsyncFunctionDef
] = self._generate_subscription_method_def(
name=name,
operation_name=operation_name,
return_type=return_type,
arguments=arguments,
arguments_dict=arguments_dict,
operation_str=operation_str,
method_def: Union[ast.FunctionDef, ast.AsyncFunctionDef] = (
self._generate_subscription_method_def(
name=name,
operation_name=operation_name,
return_type=return_type,
arguments=arguments,
arguments_dict=arguments_dict,
operation_str=operation_str,
)
)
elif async_:
method_def = self._generate_async_method(
Expand Down
6 changes: 3 additions & 3 deletions ariadne_codegen/client_generators/result_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def parse_operation_field(
schema=schema,
field_node=field,
custom_scalars=custom_scalars if custom_scalars else {},
fragments_definitions=fragments_definitions
if fragments_definitions
else {},
fragments_definitions=(
fragments_definitions if fragments_definitions else {}
),
)
)

Expand Down
20 changes: 19 additions & 1 deletion ariadne_codegen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from keyword import iskeyword
from textwrap import indent
from typing import Optional
from typing import List, Optional

import isort
from autoflake import fix_code # type: ignore
Expand All @@ -25,13 +25,31 @@ def ast_to_str(
) -> str:
"""Convert ast object into string."""
code = ast.unparse(ast_obj)
code = remove_blank_line_between_class_and_content(code)
if remove_unused_imports:
code = fix_code(code, remove_all_unused_imports=True)
if multiline_strings:
code = format_multiline_strings(code, offset=multiline_strings_offset)
return format_str(isort.code(code), mode=Mode())


def remove_blank_line_between_class_and_content(code: str) -> str:
"""Removes blank lines between class and first method.
We are doing this for code style consistency and backwards compatibility.
"""
code_lines: List[str] = []
skip_blank_lines = False
for line in code.splitlines():
if skip_blank_lines and line:
skip_blank_lines = False
elif line.startswith("class "):
skip_blank_lines = True
if not skip_blank_lines or line:
code_lines.append(line)
return "\n".join(code_lines)


def str_to_snake_case(name: str) -> str:
"""Converts camelCase or PascalCase string into snake_case."""
# lower-case letters that optionally start with a single upper-case letter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class UnionA(BaseModel):
query_u: Union[
"UnionAQueryUTypeA", "UnionAQueryUTypeB", "UnionAQueryUTypeC"
] = Field(alias="queryU", discriminator="typename__")
query_u: Union["UnionAQueryUTypeA", "UnionAQueryUTypeB", "UnionAQueryUTypeC"] = (
Field(alias="queryU", discriminator="typename__")
)


class UnionAQueryUTypeA(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


class UnionB(BaseModel):
query_u: Union[
"UnionBQueryUTypeA", "UnionBQueryUTypeB", "UnionBQueryUTypeC"
] = Field(alias="queryU", discriminator="typename__")
query_u: Union["UnionBQueryUTypeA", "UnionBQueryUTypeB", "UnionBQueryUTypeC"] = (
Field(alias="queryU", discriminator="typename__")
)


class UnionBQueryUTypeA(BaseModel):
Expand Down
8 changes: 2 additions & 6 deletions tests/main/clients/shorter_results/expected_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ async def list_type_a(
data = self.get_data(response)
return ListTypeA.model_validate(data).list_optional_type_a

async def get_animal_by_name(
self, name: str, **kwargs: Any
) -> Union[
async def get_animal_by_name(self, name: str, **kwargs: Any) -> Union[
GetAnimalByNameAnimalByNameAnimal,
GetAnimalByNameAnimalByNameCat,
GetAnimalByNameAnimalByNameDog,
Expand Down Expand Up @@ -163,9 +161,7 @@ async def get_animal_by_name(
data = self.get_data(response)
return GetAnimalByName.model_validate(data).animal_by_name

async def list_animals(
self, **kwargs: Any
) -> List[
async def list_animals(self, **kwargs: Any) -> List[
Union[
ListAnimalsListAnimalsAnimal,
ListAnimalsListAnimalsCat,
Expand Down

0 comments on commit c1a0548

Please sign in to comment.