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

0 commit comments

Comments
 (0)