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

feat(python-sdk): type hinting improvements #480

Merged
merged 9 commits into from
Feb 4, 2025
8 changes: 0 additions & 8 deletions config/clients/python/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@
"destinationFilename": "openfga_sdk/telemetry/telemetry.py",
"templateType": "SupportingFiles"
},
"src/telemetry/utilities.py.mustache": {
"destinationFilename": "openfga_sdk/telemetry/utilities.py",
"templateType": "SupportingFiles"
},
"src/__init__.py.mustache": {
"destinationFilename": "openfga_sdk/__init__.py",
"templateType": "SupportingFiles"
Expand Down Expand Up @@ -320,10 +316,6 @@
"destinationFilename": "test/telemetry/telemetry_test.py",
"templateType": "SupportingFiles"
},
"test/telemetry/utilities_test.py.mustache": {
"destinationFilename": "test/telemetry/utilities_test.py",
"templateType": "SupportingFiles"
},
"test/__init__.py.mustache": {
"destinationFilename": "test/__init__.py",
"templateType": "SupportingFiles"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ python-dateutil >= 2.8.2
urllib3 >= 2.1.0
yarl >= 1.9.4
python-dotenv >= 1, <2

9 changes: 3 additions & 6 deletions config/clients/python/template/model.mustache
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{{>partial_header}}

try:
from inspect import getfullargspec
except ImportError:
from inspect import getargspec as getfullargspec
from inspect import getfullargspec
import pprint

from {{packageName}}.configuration import Configuration
Expand Down Expand Up @@ -37,13 +34,13 @@ class {{classname}}:
attribute_map (dict): The key is attribute name
and the value is json key in definition.
"""
openapi_types = {
openapi_types: dict[str, str] = {
{{#vars}}
'{{name}}': '{{{dataType}}}'{{^-last}},{{/-last}}
{{/vars}}
}

attribute_map = {
attribute_map: dict[str, str] = {
{{#vars}}
'{{name}}': '{{baseName}}'{{^-last}},{{/-last}}
{{/vars}}
Expand Down
18 changes: 14 additions & 4 deletions config/clients/python/template/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ extend-select = [
#"C4", # flake8-comprehensions
#"C9", # mccabe
"I", # isort
#"PGH", # pygrep-hooks
"PGH", # pygrep-hooks
#"RUF", # ruff
#"UP", # pyupgrade
#"W", # pycodestyle
#"YTT", # flake8-2020
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
#"TRY", # tryceratops
#"EM", # flake8-errmsg
]
Expand Down Expand Up @@ -74,3 +74,13 @@ addopts = "--cov=openfga_sdk --cov-report term-missing --cov-report xml --cov-re

asyncio_mode = "strict"
asyncio_default_fixture_loop_scope = "function"

[tool.mypy]
python_version = "3.10"
packages = "openfga_sdk"
exclude = [
"openfa_sdk/models",
]
#warn_return_any = "True"
#warn_unused_configs = "True"
#disallow_untyped_defs = "True"
5 changes: 2 additions & 3 deletions config/clients/python/template/src/api.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ class {{classname}}:
for key, val in local_var_params['kwargs'].items():
if key not in all_params{{#servers.0}} and key != "_host_index"{{/servers.0}}:
raise FgaValidationException(
"Got an unexpected keyword argument '%s'"
" to method {{operationId}}" % key
f"Got an unexpected keyword argument '{key}' to method {{operationId}}"
)
local_var_params[key] = val
del local_var_params['kwargs']
Expand Down Expand Up @@ -290,7 +289,7 @@ class {{classname}}:
response_types_map = {}
{{/returnType}}

telemetry_attributes: dict[TelemetryAttribute, str | int] = {
telemetry_attributes: dict[TelemetryAttribute, str | bool | int | float] = {
TelemetryAttributes.fga_client_request_method: "{{operationId}}",
TelemetryAttributes.fga_client_request_store_id: self.api_client.get_store_id(),
TelemetryAttributes.fga_client_request_model_id: local_var_params.get(
Expand Down
17 changes: 8 additions & 9 deletions config/clients/python/template/src/api_client.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import tornado.gen
{{/tornado}}
from multiprocessing.pool import ThreadPool

from dateutil.parser import parse
from dateutil.parser import parse # type: ignore[import-untyped]

import {{modelPackage}}
from {{packageName}} import rest, oauth2
Expand Down Expand Up @@ -175,7 +175,7 @@ class ApiClient:
_request_auth=None,
_retry_params=None,
_oauth2_client=None,
_telemetry_attributes: dict[TelemetryAttribute, str | int] = None,
_telemetry_attributes: dict[TelemetryAttribute, str | bool | int | float] | None = None,
_streaming: bool = False,
):

Expand All @@ -200,10 +200,9 @@ class ApiClient:
collection_formats)
for k, v in path_params:
# specified safe chars, encode everything
resource_path = resource_path.replace(
'{%s}' % k,
urllib.parse.quote(str(v), safe=config.safe_chars_for_path_param)
)
_k = urllib.parse.quote(str(k), safe=config.safe_chars_for_path_param)
_v = urllib.parse.quote(str(v), safe=config.safe_chars_for_path_param)
resource_path = resource_path.replace("{" + str(k) + "}", _v)

# query parameters
if query_params:
Expand Down Expand Up @@ -415,7 +414,7 @@ class ApiClient:
elif isinstance(obj, tuple):
return tuple(self.sanitize_for_serialization(sub_obj)
for sub_obj in obj)
elif isinstance(obj, (datetime.datetime, datetime.date)):
elif isinstance(obj, datetime.datetime | datetime.date):
return obj.isoformat()

if isinstance(obj, dict):
Expand Down Expand Up @@ -514,7 +513,7 @@ class ApiClient:
_request_auth=None,
_retry_params=None,
_oauth2_client=None,
_telemetry_attributes: dict[TelemetryAttribute, str | int] = None,
_telemetry_attributes: dict[TelemetryAttribute, str | bool | int | float] | None = None,
_streaming: bool = False,
):
"""Makes the HTTP request (synchronous) and returns deserialized data.
Expand Down Expand Up @@ -841,7 +840,7 @@ class ApiClient:
kwargs = {}
if (data is not None
and klass.openapi_types is not None
and isinstance(data, (list, dict))):
and isinstance(data, list | dict)):
for attr, attr_type in klass.openapi_types.items():
if klass.attribute_map[attr] in data:
value = data[klass.attribute_map[attr]]
Expand Down
Loading
Loading