Skip to content

Commit 1c43834

Browse files
handle reserved python builtin type names as graphql names (#403)
1 parent abede45 commit 1c43834

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

ariadne_codegen/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import builtins
23
import re
34
from keyword import iskeyword
45
from textwrap import indent
@@ -17,6 +18,14 @@
1718
]
1819

1920

21+
def _is_builtin_type_name(name: str) -> bool:
22+
try:
23+
value = getattr(builtins, name)
24+
except AttributeError:
25+
return False
26+
return isinstance(value, type)
27+
28+
2029
def ast_to_str(
2130
ast_obj: ast.AST,
2231
remove_unused_imports: bool = True,
@@ -124,7 +133,7 @@ def process_name(
124133
processed_name = name
125134
if convert_to_snake_case:
126135
processed_name = str_to_snake_case(processed_name)
127-
if iskeyword(processed_name):
136+
if iskeyword(processed_name) or _is_builtin_type_name(processed_name):
128137
processed_name += "_"
129138
if (
130139
handle_pydantic_resrved_field_names

tests/client_generators/input_types_generator/test_names.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,34 @@ def test_generate_returns_module_with_valid_field_names():
197197
"underscore_named_field_",
198198
"schema_",
199199
}
200+
201+
202+
def test_generate_returns_module_with_builtin_field_names():
203+
schema = """
204+
input BuiltinsInput {
205+
list: [Int!]
206+
dict: String
207+
set: Boolean
208+
tuple: Float
209+
int: Int
210+
str: String
211+
bool: Boolean
212+
}
213+
"""
214+
215+
generator = InputTypesGenerator(schema=build_ast_schema(parse(schema)))
216+
217+
module = generator.generate()
218+
219+
parsed = ast.parse(ast_to_str(module))
220+
class_def = get_class_def(parsed)
221+
field_names = get_assignment_target_names(class_def)
222+
assert field_names == {
223+
"list_",
224+
"dict_",
225+
"set_",
226+
"tuple_",
227+
"int_",
228+
"str_",
229+
"bool_",
230+
}

tests/main/clients/example/expected_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .fragments import BasicUser, UserPersonalData
1414
from .get_users_counter import GetUsersCounter
1515
from .input_types import (
16+
BuiltinsInput,
1617
LocationInput,
1718
NotificationsPreferencesInput,
1819
UserCreateInput,
@@ -26,6 +27,7 @@
2627
"AsyncBaseClient",
2728
"BaseModel",
2829
"BasicUser",
30+
"BuiltinsInput",
2931
"Client",
3032
"Color",
3133
"CreateUser",

tests/main/clients/example/expected_client/input_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ class NotificationsPreferencesInput(BaseModel):
4848
title: str
4949

5050

51+
class BuiltinsInput(BaseModel):
52+
list_: Optional[list[int]] = Field(alias="list", default=None)
53+
dict_: Optional[str] = Field(alias="dict", default=None)
54+
set_: Optional[bool] = Field(alias="set", default=None)
55+
tuple_: Optional[float] = Field(alias="tuple", default=None)
56+
int_: Optional[int] = Field(alias="int", default=None)
57+
str_: Optional[str] = Field(alias="str", default=None)
58+
bool_: Optional[bool] = Field(alias="bool", default=None)
59+
60+
5161
UserCreateInput.model_rebuild()
5262
UserPreferencesInput.model_rebuild()

tests/main/clients/example/schema.graphql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ input NotificationsPreferencesInput {
7575
receiveSms: Boolean!
7676
title: String!
7777
}
78+
79+
input BuiltinsInput {
80+
list: [Int!]
81+
dict: String
82+
set: Boolean
83+
tuple: Float
84+
int: Int
85+
str: String
86+
bool: Boolean
87+
}

0 commit comments

Comments
 (0)