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

chore(python-sdk): add ruff linter and code formatter #478

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
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,15 @@ build-client-python:
make run-in-docker sdk_language=python image=python:${PYTHON_DOCKER_TAG} command="/bin/sh -c 'python -m pip install --upgrade pip && \
python -m pip install --upgrade setuptools wheel && \
python -m pip install -r test-requirements.txt && \
python -m pyupgrade \`find . -name *.py -type f\` --py310-plus --keep-runtime-typing && \
python -m isort . --profile black && \
python -m autoflake --exclude=__init__.py --in-place --remove-unused-variables --remove-all-unused-imports -r . && \
python -m black . && \
python -m ruff check --select I --fix . && \
python -m ruff format . && \
python setup.py sdist bdist_wheel'"

.PHONY: test-client-python
test-client-python: build-client-python
make run-in-docker sdk_language=python image=python:${PYTHON_DOCKER_TAG} command="/bin/sh -c 'python -m pip install -r test-requirements.txt && \
pytest --cov-report term-missing --cov=openfga_sdk test/ && \
flake8 . --count --show-source --statistics'"
ruff check .'"

### Java
.PHONY: tag-client-java
Expand Down
1 change: 1 addition & 0 deletions config/clients/python/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ test/*
.gitlab-ci.yml
.travis.yml
tox.ini
setup.cfg
1 change: 1 addition & 0 deletions config/clients/python/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"supportsStreamedListObjects": "streamed_list_objects",
"files": {
".snyk": {},
"pyproject.toml": {},

"api_sync.mustache": {
"folder": "openfga_sdk/sync",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ruff: noqa: E402

{{>partial_header}}

import asyncio
import os
import sys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
"""
Python SDK for OpenFGA

API version: 0.1
Website: https://openfga.dev
Documentation: https://openfga.dev/docs
Support: https://discord.gg/8naAwJfWN6
License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)

NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
"""
{{>partial_header}}

from setuptools import find_packages, setup

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ruff: noqa: E402

{{>partial_header}}

import asyncio
import os
import sys
Expand Down Expand Up @@ -198,16 +202,15 @@ async def main():
print(f"Making {checks_requests} checks ...", end=" ")
for _ in range(checks_requests):
try:
allowed = app().unpack(
await fga_client.check(
body=ClientCheckRequest(
user="user:anne", relation="owner", object="folder:foo"
),
await fga_client.check(
body=ClientCheckRequest(
user="user:anne", relation="owner", object="folder:foo"
),
"allowed",
)

except FgaValidationException as error:
print(f"Checked failed due to validation exception: {error}")

print("Done!")


Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
"""
Python SDK for OpenFGA

API version: 0.1
Website: https://openfga.dev
Documentation: https://openfga.dev/docs
Support: https://discord.gg/8naAwJfWN6
License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)

NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
"""
{{>partial_header}}

from setuptools import find_packages, setup

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ruff: noqa: E402

{{>partial_header}}

import asyncio
import json
import os
Expand Down Expand Up @@ -72,7 +76,7 @@ async def main():
)
print(f"Created temporary authorization model ({model})")

print(f"Writing 100 mock tuples to store.")
print("Writing 100 mock tuples to store.")

# Write mock data
writes = []
Expand Down Expand Up @@ -111,7 +115,7 @@ async def main():
try:
await fga_client.delete_store()
print(f"Deleted temporary store ({store})")
except:
except Exception:
pass

print("Finished.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
"""
Python SDK for OpenFGA

API version: 0.1
Website: https://openfga.dev
Documentation: https://openfga.dev/docs
Support: https://discord.gg/8naAwJfWN6
License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)

NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
"""
{{>partial_header}}

from setuptools import find_packages, setup

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ruff: noqa: E402

{{>partial_header}}

import json
import os
import sys
Expand Down Expand Up @@ -69,7 +73,7 @@ def main():
)
print(f"Created temporary authorization model ({model})")

print(f"Writing 100 mock tuples to store.")
print("Writing 100 mock tuples to store.")

# Write mock data
writes = []
Expand Down Expand Up @@ -108,7 +112,7 @@ def main():
try:
fga_client.delete_store()
print(f"Deleted temporary store ({store})")
except:
except Exception:
pass

print("Finished.")
Expand Down
1 change: 1 addition & 0 deletions config/clients/python/template/gitignore_custom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ venv/
.venv/
.python-version
.pytest_cache
.ruff_cache
test/__pycache__/

# Translations
Expand Down
1 change: 0 additions & 1 deletion config/clients/python/template/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ try:
except ImportError:
from inspect import getargspec as getfullargspec
import pprint
import re

from {{packageName}}.configuration import Configuration

Expand Down
76 changes: 76 additions & 0 deletions config/clients/python/template/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[tool.ruff]
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

line-length = 88
indent-width = 4

target-version = "py310"

[tool.ruff.lint]
extend-select = [
#"B", # flake8-bugbear
#"C4", # flake8-comprehensions
#"C9", # mccabe
"I", # isort
#"PGH", # pygrep-hooks
#"RUF", # ruff
#"UP", # pyupgrade
#"W", # pycodestyle
#"YTT", # flake8-2020
#"TRY", # tryceratops
#"EM", # flake8-errmsg
]
ignore = []

fixable = ["ALL"]
unfixable = []

dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.ruff.lint.isort]
lines-between-types = 1
lines-after-imports = 2

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[tool.pytest.ini_options]
testpaths = [
"test",
"integration",
]

addopts = "--cov=openfga_sdk --cov-report term-missing --cov-report xml --cov-report html"

asyncio_mode = "strict"
asyncio_default_fixture_loop_scope = "function"
20 changes: 0 additions & 20 deletions config/clients/python/template/setup_cfg.mustache

This file was deleted.

23 changes: 23 additions & 0 deletions config/clients/python/template/src/__init__.py.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{>partial_header}}

__version__ = "{{packageVersion}}"

from {{packageName}}.client.client import OpenFgaClient
Expand Down Expand Up @@ -29,3 +30,25 @@ from {{packageName}}.telemetry.configuration import (

__import__('sys').setrecursionlimit({{{.}}})
{{/recursionLimit}}

__all__ = [
"OpenFgaClient",
"ClientConfiguration",
{{#apiInfo}}{{#apis}}"{{classname}}",
{{/apis}}{{/apiInfo}}
"ApiClient",
"Configuration",
"OpenApiException",
"FgaValidationException",
"ApiValueError",
"ApiKeyError",
"ApiAttributeError",
"ApiException",
{{#models}}{{#model}}"{{classname}}",
{{/model}}{{/models}}
"TelemetryConfiguration",
"TelemetryConfigurations",
"TelemetryConfigurationType",
"TelemetryMetricConfiguration",
"TelemetryMetricsConfiguration",
]
1 change: 0 additions & 1 deletion config/clients/python/template/src/api.py.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{>partial_header}}


from {{packageName}}.api_client import ApiClient
from {{packageName}}.exceptions import ApiValueError, FgaValidationException
from {{packageName}}.oauth2 import OAuth2Client
Expand Down
7 changes: 7 additions & 0 deletions config/clients/python/template/src/api/__init__.py.mustache
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
{{>partial_header}}

{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}}
{{/apis}}{{/apiInfo}}

__all__ = [
{{#apiInfo}}{{#apis}}"{{classname}}",
{{/apis}}{{/apiInfo}}
]
13 changes: 8 additions & 5 deletions config/clients/python/template/src/api_client.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,17 @@ class ApiClient:

if klass in self.PRIMITIVE_TYPES:
return self.__deserialize_primitive(data, klass)
elif klass == object:

if klass is object:
return self.__deserialize_object(data)
elif klass == datetime.date:

if klass is datetime.date:
return self.__deserialize_date(data)
elif klass == datetime.datetime:

if klass is datetime.datetime:
return self.__deserialize_datetime(data)
else:
return self.__deserialize_model(data, klass)

return self.__deserialize_model(data, klass)

{{#asyncio}}async {{/asyncio}}def call_api(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{{>partial_header}}

from {{packageName}}.client.client import OpenFgaClient
from {{packageName}}.client.configuration import ClientConfiguration
from {{packageName}}.client.models.check_request import ClientCheckRequest

__all__ = [
"OpenFgaClient",
"ClientConfiguration",
"ClientCheckRequest",
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ from {{packageName}}.client.configuration import ClientConfiguration
from {{packageName}}.client.models.assertion import ClientAssertion
from {{packageName}}.client.models.batch_check_item import ClientBatchCheckItem, construct_batch_item
from {{packageName}}.client.models.batch_check_request import ClientBatchCheckRequest
from {{packageName}}.client.models.batch_check_response import BatchCheckResponse, ClientBatchCheckResponse
from {{packageName}}.client.models.batch_check_response import ClientBatchCheckResponse
from {{packageName}}.client.models.batch_check_single_response import ClientBatchCheckSingleResponse
from {{packageName}}.client.models.client_batch_check_response import ClientBatchCheckClientResponse
from {{packageName}}.client.models.check_request import ClientCheckRequest, construct_check_request
from {{packageName}}.client.models.batch_check_response import BatchCheckResponse
from {{packageName}}.client.models.tuple import ClientTuple, convert_tuple_keys
from {{packageName}}.client.models.write_request import ClientWriteRequest
from {{packageName}}.client.models.write_response import ClientWriteResponse
Expand Down
Loading
Loading