Skip to content

Commit 0638659

Browse files
authored
python: simplify module imports (#17507)
In #16624, I introduced a new mechanism to record imports to other modules, instead of having specialized datetime/typing/pydantic objects to manage imports for these modules. This change reuses the mechanism from #16624 and replace the specialized import managers by the generic one. Unused imports from various .mustache templates are also cleaned up.
1 parent dffb5c1 commit 0638659

File tree

266 files changed

+356
-1335
lines changed

Some content is hidden

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

266 files changed

+356
-1335
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java

Lines changed: 84 additions & 133 deletions
Large diffs are not rendered by default.

modules/openapi-generator/src/main/resources/python/api.mustache

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# coding: utf-8
22

33
{{>partial_header}}
4-
5-
import io
64
import warnings
7-
85
from pydantic import validate_call, Field, StrictFloat, StrictStr, StrictInt
9-
from typing import Dict, List, Optional, Tuple, Union, Any
6+
from typing import Any, Dict, List, Optional, Tuple, Union
107

118
try:
129
from typing import Annotated
@@ -170,7 +167,7 @@ class {{classname}}:
170167
{{#isBinary}}
171168
# convert to byte array if the input is a file name (str)
172169
if isinstance({{paramName}}, str):
173-
with io.open({{paramName}}, "rb") as _fp:
170+
with open({{paramName}}, "rb") as _fp:
174171
_body_params = _fp.read()
175172
else:
176173
_body_params = {{paramName}}

modules/openapi-generator/src/main/resources/python/api_client.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
{{>partial_header}}
44

5-
import atexit
65
import datetime
76
from dateutil.parser import parse
87
import json

modules/openapi-generator/src/main/resources/python/api_doc_example.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11

22
```python
3-
import time
4-
import os
53
import {{{packageName}}}
64
{{#vendorExtensions.x-py-example-import}}
75
{{{.}}}

modules/openapi-generator/src/main/resources/python/api_response.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""API response object."""
22

33
from __future__ import annotations
4-
from typing import Any, Dict, Optional, Generic, TypeVar
4+
from typing import Dict, Optional, Generic, TypeVar
55
from pydantic import Field, StrictInt, StrictStr, StrictBytes, BaseModel
66

77
T = TypeVar("T")

modules/openapi-generator/src/main/resources/python/common_README.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
```python
22
{{#apiInfo}}{{#apis}}{{#-last}}{{#hasHttpSignatureMethods}}import datetime{{/hasHttpSignatureMethods}}{{/-last}}{{/apis}}{{/apiInfo}}
3-
import time
43
import {{{packageName}}}
54
from {{{packageName}}}.rest import ApiException
65
from pprint import pprint

modules/openapi-generator/src/main/resources/python/model_anyof.mustache

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@ from inspect import getfullargspec
33
import json
44
import pprint
55
import re # noqa: F401
6-
{{#vendorExtensions.x-py-datetime-imports}}{{#-first}}from datetime import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-datetime-imports}}
7-
{{#vendorExtensions.x-py-typing-imports}}{{#-first}}from typing import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-typing-imports}}
8-
{{#vendorExtensions.x-py-pydantic-imports}}{{#-first}}from pydantic import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-pydantic-imports}}
96
{{#vendorExtensions.x-py-other-imports}}
107
{{{.}}}
118
{{/vendorExtensions.x-py-other-imports}}
129
{{#vendorExtensions.x-py-model-imports}}
1310
{{{.}}}
1411
{{/vendorExtensions.x-py-model-imports}}
1512
from typing import Union, Any, List, TYPE_CHECKING, Optional, Dict
16-
from typing_extensions import Literal
17-
from pydantic import StrictStr, Field
18-
try:
19-
from typing import Self
20-
except ImportError:
21-
from typing_extensions import Self
13+
from typing_extensions import Literal, Self
2214

2315
{{#lambda.uppercase}}{{{classname}}}{{/lambda.uppercase}}_ANY_OF_SCHEMAS = [{{#anyOf}}"{{.}}"{{^-last}}, {{/-last}}{{/anyOf}}]
2416

modules/openapi-generator/src/main/resources/python/model_enum.mustache

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
from __future__ import annotations
22
import json
3-
import pprint
4-
import re # noqa: F401
53
from enum import Enum
6-
{{#vendorExtensions.x-py-datetime-imports}}{{#-first}}from datetime import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-datetime-imports}}
7-
{{#vendorExtensions.x-py-typing-imports}}{{#-first}}from typing import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-typing-imports}}
8-
{{#vendorExtensions.x-py-pydantic-imports}}{{#-first}}from pydantic import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-pydantic-imports}}
9-
try:
10-
from typing import Self
11-
except ImportError:
12-
from typing_extensions import Self
4+
{{#vendorExtensions.x-py-other-imports}}
5+
{{{.}}}
6+
{{/vendorExtensions.x-py-other-imports}}
7+
from typing_extensions import Self
138

149

1510
class {{classname}}({{vendorExtensions.x-py-enum-type}}, Enum):

modules/openapi-generator/src/main/resources/python/model_generic.mustache

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import pprint
33
import re # noqa: F401
44
import json
55

6-
{{#vendorExtensions.x-py-datetime-imports}}{{#-first}}from datetime import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-datetime-imports}}
7-
{{#vendorExtensions.x-py-typing-imports}}{{#-first}}from typing import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-typing-imports}}
8-
{{#vendorExtensions.x-py-pydantic-imports}}{{#-first}}from pydantic import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-pydantic-imports}}
96
{{#vendorExtensions.x-py-other-imports}}
107
{{{.}}}
118
{{/vendorExtensions.x-py-other-imports}}

modules/openapi-generator/src/main/resources/python/model_oneof.mustache

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
from __future__ import annotations
2-
from inspect import getfullargspec
32
import json
43
import pprint
5-
import re # noqa: F401
6-
{{#vendorExtensions.x-py-datetime-imports}}{{#-first}}from datetime import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-datetime-imports}}
7-
{{#vendorExtensions.x-py-typing-imports}}{{#-first}}from typing import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-typing-imports}}
8-
{{#vendorExtensions.x-py-pydantic-imports}}{{#-first}}from pydantic import{{/-first}} {{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-py-pydantic-imports}}
94
{{#vendorExtensions.x-py-other-imports}}
105
{{{.}}}
116
{{/vendorExtensions.x-py-other-imports}}
127
{{#vendorExtensions.x-py-model-imports}}
138
{{{.}}}
149
{{/vendorExtensions.x-py-model-imports}}
15-
from typing import Union, Any, List, TYPE_CHECKING, Optional, Dict
16-
from typing_extensions import Literal
1710
from pydantic import StrictStr, Field
18-
try:
19-
from typing import Self
20-
except ImportError:
21-
from typing_extensions import Self
11+
from typing import Union, List, Optional, Dict
12+
from typing_extensions import Literal, Self
2213

2314
{{#lambda.uppercase}}{{{classname}}}{{/lambda.uppercase}}_ONE_OF_SCHEMAS = [{{#oneOf}}"{{.}}"{{^-last}}, {{/-last}}{{/oneOf}}]
2415

modules/openapi-generator/src/main/resources/python/model_test.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{{>partial_header}}
44

55
import unittest
6-
import datetime
76

87
{{#models}}
98
{{#model}}

modules/openapi-generator/src/main/resources/python/signing.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ from Crypto.Hash import SHA256, SHA512
66
from Crypto.PublicKey import RSA, ECC
77
from Crypto.Signature import PKCS1_v1_5, pss, DSS
88
from email.utils import formatdate
9-
import json
109
import os
1110
import re
1211
from time import time

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ Please follow the [installation procedure](#installation--usage) and then run th
5050

5151
```python
5252

53-
import time
5453
import openapi_client
5554
from openapi_client.rest import ApiException
5655
from pprint import pprint

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/docs/AuthApi.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ To test HTTP basic authentication
2020
* Basic Authentication (http_auth):
2121

2222
```python
23-
import time
24-
import os
2523
import openapi_client
2624
from openapi_client.rest import ApiException
2725
from pprint import pprint
@@ -96,8 +94,6 @@ To test HTTP bearer authentication
9694
* Bearer Authentication (http_bearer_auth):
9795

9896
```python
99-
import time
100-
import os
10197
import openapi_client
10298
from openapi_client.rest import ApiException
10399
from pprint import pprint

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/docs/BodyApi.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ Test binary (gif) response body
2626

2727

2828
```python
29-
import time
30-
import os
3129
import openapi_client
3230
from openapi_client.rest import ApiException
3331
from pprint import pprint
@@ -91,8 +89,6 @@ Test body parameter(s)
9189

9290

9391
```python
94-
import time
95-
import os
9692
import openapi_client
9793
from openapi_client.rest import ApiException
9894
from pprint import pprint
@@ -160,8 +156,6 @@ Test array of binary in multipart mime
160156

161157

162158
```python
163-
import time
164-
import os
165159
import openapi_client
166160
from openapi_client.rest import ApiException
167161
from pprint import pprint
@@ -229,8 +223,6 @@ Test single binary in multipart mime
229223

230224

231225
```python
232-
import time
233-
import os
234226
import openapi_client
235227
from openapi_client.rest import ApiException
236228
from pprint import pprint
@@ -298,8 +290,6 @@ Test body parameter(s)
298290

299291

300292
```python
301-
import time
302-
import os
303293
import openapi_client
304294
from openapi_client.models.pet import Pet
305295
from openapi_client.rest import ApiException
@@ -368,8 +358,6 @@ Test free form object
368358

369359

370360
```python
371-
import time
372-
import os
373361
import openapi_client
374362
from openapi_client.rest import ApiException
375363
from pprint import pprint
@@ -437,8 +425,6 @@ Test body parameter(s)
437425

438426

439427
```python
440-
import time
441-
import os
442428
import openapi_client
443429
from openapi_client.models.pet import Pet
444430
from openapi_client.rest import ApiException
@@ -507,8 +493,6 @@ Test empty response body
507493

508494

509495
```python
510-
import time
511-
import os
512496
import openapi_client
513497
from openapi_client.models.pet import Pet
514498
from openapi_client.rest import ApiException
@@ -577,8 +561,6 @@ Test empty json (request body)
577561

578562

579563
```python
580-
import time
581-
import os
582564
import openapi_client
583565
from openapi_client.models.tag import Tag
584566
from openapi_client.rest import ApiException

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/docs/FormApi.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ Test form parameter(s)
1919

2020

2121
```python
22-
import time
23-
import os
2422
import openapi_client
2523
from openapi_client.rest import ApiException
2624
from pprint import pprint
@@ -92,8 +90,6 @@ Test form parameter(s) for oneOf schema
9290

9391

9492
```python
95-
import time
96-
import os
9793
import openapi_client
9894
from openapi_client.rest import ApiException
9995
from pprint import pprint

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/docs/HeaderApi.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Test header parameter(s)
1818

1919

2020
```python
21-
import time
22-
import os
2321
import openapi_client
2422
from openapi_client.models.string_enum_ref import StringEnumRef
2523
from openapi_client.rest import ApiException

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/docs/PathApi.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ Test path parameter(s)
1818

1919

2020
```python
21-
import time
22-
import os
2321
import openapi_client
2422
from openapi_client.models.string_enum_ref import StringEnumRef
2523
from openapi_client.rest import ApiException

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/docs/QueryApi.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ Test query parameter(s)
2525

2626

2727
```python
28-
import time
29-
import os
3028
import openapi_client
3129
from openapi_client.models.string_enum_ref import StringEnumRef
3230
from openapi_client.rest import ApiException
@@ -97,8 +95,6 @@ Test query parameter(s)
9795

9896

9997
```python
100-
import time
101-
import os
10298
import openapi_client
10399
from openapi_client.rest import ApiException
104100
from pprint import pprint
@@ -170,8 +166,6 @@ Test query parameter(s)
170166

171167

172168
```python
173-
import time
174-
import os
175169
import openapi_client
176170
from openapi_client.rest import ApiException
177171
from pprint import pprint
@@ -243,8 +237,6 @@ Test query parameter(s)
243237

244238

245239
```python
246-
import time
247-
import os
248240
import openapi_client
249241
from openapi_client.models.pet import Pet
250242
from openapi_client.rest import ApiException
@@ -313,8 +305,6 @@ Test query parameter(s)
313305

314306

315307
```python
316-
import time
317-
import os
318308
import openapi_client
319309
from openapi_client.rest import ApiException
320310
from pprint import pprint
@@ -382,8 +372,6 @@ Test query parameter(s)
382372

383373

384374
```python
385-
import time
386-
import os
387375
import openapi_client
388376
from openapi_client.models.test_query_style_form_explode_true_array_string_query_object_parameter import TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter
389377
from openapi_client.rest import ApiException
@@ -452,8 +440,6 @@ Test query parameter(s)
452440

453441

454442
```python
455-
import time
456-
import os
457443
import openapi_client
458444
from openapi_client.models.pet import Pet
459445
from openapi_client.rest import ApiException
@@ -522,8 +508,6 @@ Test query parameter(s)
522508

523509

524510
```python
525-
import time
526-
import os
527511
import openapi_client
528512
from openapi_client.rest import ApiException
529513
from pprint import pprint

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/api/auth_api.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
Do not edit the class manually.
1313
""" # noqa: E501
1414

15-
16-
import io
1715
import warnings
18-
1916
from pydantic import validate_call, Field, StrictFloat, StrictStr, StrictInt
20-
from typing import Dict, List, Optional, Tuple, Union, Any
17+
from typing import Any, Dict, List, Optional, Tuple, Union
2118

2219
try:
2320
from typing import Annotated

0 commit comments

Comments
 (0)