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

Avoid generating empty ImportFrom statements #337

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 14 additions & 12 deletions ariadne_codegen/client_generators/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,22 @@ def __init__(

def generate(self) -> ast.Module:
"""Generate module with class definition of graphql client."""
self._add_import(
generate_import_from(
names=self.arguments_generator.get_used_inputs(),
from_=self.input_types_module_name,
level=1,
if used_inputs := self.arguments_generator.get_used_inputs():
self._add_import(
generate_import_from(
names=used_inputs,
from_=self.input_types_module_name,
level=1,
)
)
)
self._add_import(
generate_import_from(
names=self.arguments_generator.get_used_enums(),
from_=self.enums_module_name,
level=1,
if used_enums := self.arguments_generator.get_used_enums():
self._add_import(
generate_import_from(
names=used_enums,
from_=self.enums_module_name,
level=1,
)
)
)
for custom_scalar_name in self.arguments_generator.get_used_custom_scalars():
scalar_data = self.custom_scalars[custom_scalar_name]
for import_ in generate_scalar_imports(scalar_data):
Expand Down
4 changes: 2 additions & 2 deletions ariadne_codegen/client_generators/input_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def generate(self, types_to_include: Optional[List[str]] = None) -> ast.Module:
class_defs = self._filter_class_defs(types_to_include=types_to_include)
self._generated_public_names = [class_def.name for class_def in class_defs]

if self._used_enums:
if used_imports := self.get_used_enums():
self._imports.append(
generate_import_from(self.get_used_enums(), self.enums_module, 1)
generate_import_from(used_imports, self.enums_module, 1)
)

for scalar_name in self._used_scalars:
Expand Down
1 change: 1 addition & 0 deletions ariadne_codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def generate_import_from(
names: List[str], from_: Optional[str] = None, level: int = 0
) -> ast.ImportFrom:
"""Generate import from statement."""
assert names, "Using ImportFrom with no names would produce invalid Python code"
return ast.ImportFrom(
module=from_, names=[ast.alias(n) for n in names], level=level
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@
)
def test_generator_returns_module_with_filtered_classes(used_types, expected_classes):
schema_str = """
enum EnumA {
VALUE1
VALUE2
}

input InputA {
valueAA: InputAA!
valueAB: InputAB
valueEnumA: EnumA
}

input InputAA {
Expand All @@ -46,6 +52,7 @@ def test_generator_returns_module_with_filtered_classes(used_types, expected_cla
input InputAB {
val: String!
valueA: InputA
valueEnumA: EnumA
}

input InputX {
Expand Down