Skip to content

Commit

Permalink
chore(clients): use the ruff python formatter (#3204)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jun 19, 2024
1 parent 9f579db commit 36aabab
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 1,555 deletions.
3 changes: 2 additions & 1 deletion config/generation.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,18 @@ export const patterns = [

// Python
'!snippets/python/pyproject.toml',
'!snippets/python/poetry.lock',
'clients/algoliasearch-client-python/**',
'!clients/algoliasearch-client-python/algoliasearch/http/**',
'!clients/algoliasearch-client-python/algoliasearch/py.typed',
'clients/algoliasearch-client-python/algoliasearch/http/__init__.py',
'!clients/algoliasearch-client-python/*',
'clients/algoliasearch-client-python/pyproject.toml',
'clients/algoliasearch-client-python/poetry.lock',
'clients/algoliasearch-client-python/requirements.txt',
'clients/algoliasearch-client-python/.gitignore',

'tests/output/python/requirements.txt',
'tests/output/python/poetry.lock',
'!tests/output/python/**/__init__.py',

// Ruby
Expand Down
14 changes: 11 additions & 3 deletions playground/python/app/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
async def main():
print("SearchClient version", __version__)

client = SearchClient(environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY"))
client = SearchClient(
environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
)
print("client initialized", client)

try:
resp = await client.replace_all_objects(
index_name="newoneeverytime",
objects=[{"name": f"John Doe{i}", "objectID": f"fff2bd4d-bb17-4e21-a0c4-0a8ea5e363f2{i}" } for i in range(33)],
batch_size=10
objects=[
{
"name": f"John Doe{i}",
"objectID": f"fff2bd4d-bb17-4e21-a0c4-0a8ea5e363f2{i}",
}
for i in range(33)
],
batch_size=10,
)

print(resp)
Expand Down
1,534 changes: 24 additions & 1,510 deletions playground/python/poetry.lock

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions playground/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ repository = "https://github.com/algolia/api-clients-automation"
[tool.poetry.dependencies]
python = "3.11.6"
algoliasearch = { path = "../../clients/algoliasearch-client-python", develop = true }
flake8 = "6.1.0"
autoflake = "2.2.1"
autopep8 = "2.0.4"
black = "24.3.0"
isort = "5.13.0"
ruff = "0.4.9"
python-dotenv = "1.0.0"

[tool.poetry.scripts]
Expand Down
2 changes: 1 addition & 1 deletion scripts/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function formatter(language: string, cwd: string): Promise<void> {
break;
case 'python':
await run(
'poetry lock --no-update && poetry install --sync && pip freeze > requirements.txt && poetry run autopep8 -r --in-place --aggressive . && poetry run autoflake -r --remove-unused-variables --remove-all-unused-imports --in-place . && poetry run isort . && poetry run black . && poetry run flake8 --ignore=E501,W503 .',
'poetry lock --no-update && poetry install --sync && pip freeze > requirements.txt && poetry run ruff check --fix && poetry run ruff format',
{ cwd, language },
);
break;
Expand Down
6 changes: 1 addition & 5 deletions snippets/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ repository = "https://github.com/algolia/api-clients-automation"
[tool.poetry.dependencies]
python = "3.11.6"
algoliasearch = { path = "../../clients/algoliasearch-client-python", develop = true }
flake8 = "6.1.0"
autoflake = "2.2.1"
autopep8 = "2.0.4"
black = "24.3.0"
isort = "5.13.0"
ruff = "0.4.9"
18 changes: 9 additions & 9 deletions templates/python/model_generic.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
{{/items.items.isPrimitiveType}}
{{^items.items.isPrimitiveType}}
"{{{baseName}}}": [
"{{{baseName}}}": ([
[{{{items.items.dataType}}}.from_dict(_inner_item) for _inner_item in _item]
for _item in obj.get("{{{baseName}}}")
] if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
] if obj.get("{{{baseName}}}") is not None else None){{^-last}},{{/-last}}
{{/items.items.isPrimitiveType}}
{{/items.isArray}}
{{^items.isArray}}
Expand All @@ -251,7 +251,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
{{/items.isEnumOrRef}}
{{^items.isEnumOrRef}}
"{{{baseName}}}": [{{{items.dataType}}}.from_dict(_item) for _item in obj.get("{{{baseName}}}")] if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
"{{{baseName}}}": ([{{{items.dataType}}}.from_dict(_item) for _item in obj.get("{{{baseName}}}")] if obj.get("{{{baseName}}}") is not None else None){{^-last}},{{/-last}}
{{/items.isEnumOrRef}}
{{/items.isPrimitiveType}}
{{#items.isPrimitiveType}}
Expand All @@ -264,7 +264,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{^items.isEnumOrRef}}
{{#items.isContainer}}
{{#items.isMap}}
"{{{baseName}}}": dict(
"{{{baseName}}}": (dict(
(_k, dict(
(_ik, {{{items.items.dataType}}}.from_dict(_iv))
for _ik, _iv in _v.items()
Expand All @@ -275,7 +275,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
for _k, _v in obj.get("{{{baseName}}}").items()
)
if obj.get("{{{baseName}}}") is not None
else None{{^-last}},{{/-last}}
else None){{^-last}},{{/-last}}
{{/items.isMap}}
{{#items.isArray}}
"{{{baseName}}}": dict(
Expand All @@ -289,12 +289,12 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{/items.isArray}}
{{/items.isContainer}}
{{^items.isContainer}}
"{{{baseName}}}": dict(
"{{{baseName}}}": (dict(
(_k, {{{items.dataType}}}.from_dict(_v))
for _k, _v in obj.get("{{{baseName}}}").items()
)
if obj.get("{{{baseName}}}") is not None
else None{{^-last}},{{/-last}}
else None){{^-last}},{{/-last}}
{{/items.isContainer}}
{{/items.isEnumOrRef}}
{{#items.isEnumOrRef}}
Expand All @@ -309,7 +309,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{^isContainer}}
{{^isPrimitiveType}}
{{^isEnumOrRef}}
"{{{baseName}}}": {{{dataType}}}.from_dict(obj.get("{{{baseName}}}")) if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
"{{{baseName}}}": ({{{dataType}}}.from_dict(obj.get("{{{baseName}}}")) if obj.get("{{{baseName}}}") is not None else None){{^-last}},{{/-last}}
{{/isEnumOrRef}}
{{#isEnumOrRef}}
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
Expand All @@ -329,4 +329,4 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}

{{/isAdditionalPropertiesTrue}}
return _obj
{{/hasChildren}}
{{/hasChildren}}
1 change: 0 additions & 1 deletion templates/python/model_oneof.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}

{{/isNullable}}
error_messages = []
match = 0

{{#composedSchemas.oneOf}}
{{#isContainer}}
Expand Down
26 changes: 12 additions & 14 deletions templates/python/pyproject.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@ async-timeout = ">= 4.0.3"
pydantic = ">= 2"

[tool.poetry.group.dev.dependencies]
flake8 = "7.0.0"
autoflake = "2.3.1"
autopep8 = "2.2.0"
black = "24.4.2"
isort = "5.13.2"
ruff = "0.4.9"

[tool.flake8]
max-line-length = 88
ignore = ['E501']
[tool.ruff]
line-length = 88

[tool.isort]
line_length=88
include_trailing_comma=true
multi_line_output=3
force_grid_wrap=0
combine_as_imports=true
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I"]
ignore = ["E501"]

[tool.ruff.lint.isort]
split-on-trailing-comma = false

[tool.ruff.format]
quote-style = "double"

[build-system]
requires = ["poetry-core"]
Expand Down
6 changes: 5 additions & 1 deletion templates/python/tests/client/createClient.mustache
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
_config = {{#lambda.pascalcase}}{{clientPrefix}}Config{{/lambda.pascalcase}}("{{parametersWithDataTypeMap.appId.value}}", "{{parametersWithDataTypeMap.apiKey.value}}"{{#hasRegionalHost}}{{#parametersWithDataTypeMap.region}},"{{parametersWithDataTypeMap.region.value}}"{{/parametersWithDataTypeMap.region}}{{/hasRegionalHost}});{{#hasCustomHosts}}_config.hosts = HostsCollection([{{#customHosts}}Host(url='{{host}}', scheme='http', port={{port}}){{^-last}},{{/-last}}{{/customHosts}}]){{/hasCustomHosts}};self._client = {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}.create_with_config(config=_config{{#useEchoRequester}}, transporter=EchoTransporter(_config){{/useEchoRequester}})
_config = {{#lambda.pascalcase}}{{clientPrefix}}Config{{/lambda.pascalcase}}("{{parametersWithDataTypeMap.appId.value}}", "{{parametersWithDataTypeMap.apiKey.value}}"{{#hasRegionalHost}}{{#parametersWithDataTypeMap.region}},"{{parametersWithDataTypeMap.region.value}}"{{/parametersWithDataTypeMap.region}}{{/hasRegionalHost}})
{{#hasCustomHosts}}
{{#isError}} {{/isError}}_config.hosts = HostsCollection([{{#customHosts}}Host(url='{{host}}', scheme='http', port={{port}}){{^-last}},{{/-last}}{{/customHosts}}])
{{/hasCustomHosts}}
{{#isError}} {{/isError}}self._client = {{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}.create_with_config(config=_config{{#useEchoRequester}}, transporter=EchoTransporter(_config){{/useEchoRequester}})
6 changes: 1 addition & 5 deletions tests/output/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ repository = "https://github.com/algolia/api-clients-automation"
[tool.poetry.dependencies]
python = "3.11.6"
algoliasearch = { path = "../../../clients/algoliasearch-client-python", develop = true }
flake8 = "6.1.0"
autoflake = "2.2.1"
autopep8 = "2.0.4"
black = "24.3.0"
isort = "5.13.0"
ruff = "0.4.9"
pytest = "7.4.3"
python-dotenv = "1.0.0"
pytest-aiohttp = "1.0.5"
Expand Down

1 comment on commit 36aabab

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.