Skip to content

Commit adccb71

Browse files
authored
feat(python-sdk): type hinting improvements (#480)
2 parents d1598dd + 49c5cf8 commit adccb71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1639
-1032
lines changed

config/clients/python/config.overrides.json

-8
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,6 @@
219219
"destinationFilename": "openfga_sdk/telemetry/telemetry.py",
220220
"templateType": "SupportingFiles"
221221
},
222-
"src/telemetry/utilities.py.mustache": {
223-
"destinationFilename": "openfga_sdk/telemetry/utilities.py",
224-
"templateType": "SupportingFiles"
225-
},
226222
"src/__init__.py.mustache": {
227223
"destinationFilename": "openfga_sdk/__init__.py",
228224
"templateType": "SupportingFiles"
@@ -320,10 +316,6 @@
320316
"destinationFilename": "test/telemetry/telemetry_test.py",
321317
"templateType": "SupportingFiles"
322318
},
323-
"test/telemetry/utilities_test.py.mustache": {
324-
"destinationFilename": "test/telemetry/utilities_test.py",
325-
"templateType": "SupportingFiles"
326-
},
327319
"test/__init__.py.mustache": {
328320
"destinationFilename": "test/__init__.py",
329321
"templateType": "SupportingFiles"

config/clients/python/template/example/example1/requirements.txt.mustache

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ python-dateutil >= 2.8.2
99
urllib3 >= 2.1.0
1010
yarl >= 1.9.4
1111
python-dotenv >= 1, <2
12+

config/clients/python/template/model.mustache

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{{>partial_header}}
22

3-
try:
4-
from inspect import getfullargspec
5-
except ImportError:
6-
from inspect import getargspec as getfullargspec
3+
from inspect import getfullargspec
74
import pprint
85

96
from {{packageName}}.configuration import Configuration
@@ -37,13 +34,13 @@ class {{classname}}:
3734
attribute_map (dict): The key is attribute name
3835
and the value is json key in definition.
3936
"""
40-
openapi_types = {
37+
openapi_types: dict[str, str] = {
4138
{{#vars}}
4239
'{{name}}': '{{{dataType}}}'{{^-last}},{{/-last}}
4340
{{/vars}}
4441
}
4542

46-
attribute_map = {
43+
attribute_map: dict[str, str] = {
4744
{{#vars}}
4845
'{{name}}': '{{baseName}}'{{^-last}},{{/-last}}
4946
{{/vars}}

config/clients/python/template/pyproject.toml

+14-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ extend-select = [
3939
#"C4", # flake8-comprehensions
4040
#"C9", # mccabe
4141
"I", # isort
42-
#"PGH", # pygrep-hooks
42+
"PGH", # pygrep-hooks
4343
#"RUF", # ruff
44-
#"UP", # pyupgrade
45-
#"W", # pycodestyle
46-
#"YTT", # flake8-2020
44+
"UP", # pyupgrade
45+
"W", # pycodestyle
46+
"YTT", # flake8-2020
4747
#"TRY", # tryceratops
4848
#"EM", # flake8-errmsg
4949
]
@@ -74,3 +74,13 @@ addopts = "--cov=openfga_sdk --cov-report term-missing --cov-report xml --cov-re
7474

7575
asyncio_mode = "strict"
7676
asyncio_default_fixture_loop_scope = "function"
77+
78+
[tool.mypy]
79+
python_version = "3.10"
80+
packages = "openfga_sdk"
81+
exclude = [
82+
"openfa_sdk/models",
83+
]
84+
#warn_return_any = "True"
85+
#warn_unused_configs = "True"
86+
#disallow_untyped_defs = "True"

config/clients/python/template/src/api.py.mustache

+2-3
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ class {{classname}}:
187187
for key, val in local_var_params['kwargs'].items():
188188
if key not in all_params{{#servers.0}} and key != "_host_index"{{/servers.0}}:
189189
raise FgaValidationException(
190-
"Got an unexpected keyword argument '%s'"
191-
" to method {{operationId}}" % key
190+
f"Got an unexpected keyword argument '{key}' to method {{operationId}}"
192191
)
193192
local_var_params[key] = val
194193
del local_var_params['kwargs']
@@ -290,7 +289,7 @@ class {{classname}}:
290289
response_types_map = {}
291290
{{/returnType}}
292291

293-
telemetry_attributes: dict[TelemetryAttribute, str | int] = {
292+
telemetry_attributes: dict[TelemetryAttribute, str | bool | int | float] = {
294293
TelemetryAttributes.fga_client_request_method: "{{operationId}}",
295294
TelemetryAttributes.fga_client_request_store_id: self.api_client.get_store_id(),
296295
TelemetryAttributes.fga_client_request_model_id: local_var_params.get(

config/clients/python/template/src/api_client.py.mustache

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import tornado.gen
1616
{{/tornado}}
1717
from multiprocessing.pool import ThreadPool
1818

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

2121
import {{modelPackage}}
2222
from {{packageName}} import rest, oauth2
@@ -175,7 +175,7 @@ class ApiClient:
175175
_request_auth=None,
176176
_retry_params=None,
177177
_oauth2_client=None,
178-
_telemetry_attributes: dict[TelemetryAttribute, str | int] = None,
178+
_telemetry_attributes: dict[TelemetryAttribute, str | bool | int | float] | None = None,
179179
_streaming: bool = False,
180180
):
181181

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

208207
# query parameters
209208
if query_params:
@@ -415,7 +414,7 @@ class ApiClient:
415414
elif isinstance(obj, tuple):
416415
return tuple(self.sanitize_for_serialization(sub_obj)
417416
for sub_obj in obj)
418-
elif isinstance(obj, (datetime.datetime, datetime.date)):
417+
elif isinstance(obj, datetime.datetime | datetime.date):
419418
return obj.isoformat()
420419

421420
if isinstance(obj, dict):
@@ -514,7 +513,7 @@ class ApiClient:
514513
_request_auth=None,
515514
_retry_params=None,
516515
_oauth2_client=None,
517-
_telemetry_attributes: dict[TelemetryAttribute, str | int] = None,
516+
_telemetry_attributes: dict[TelemetryAttribute, str | bool | int | float] | None = None,
518517
_streaming: bool = False,
519518
):
520519
"""Makes the HTTP request (synchronous) and returns deserialized data.
@@ -841,7 +840,7 @@ class ApiClient:
841840
kwargs = {}
842841
if (data is not None
843842
and klass.openapi_types is not None
844-
and isinstance(data, (list, dict))):
843+
and isinstance(data, list | dict)):
845844
for attr, attr_type in klass.openapi_types.items():
846845
if klass.attribute_map[attr] in data:
847846
value = data[klass.attribute_map[attr]]

0 commit comments

Comments
 (0)