Skip to content

Commit e9fef9d

Browse files
committed
use builtin types for python
1 parent 0bfeb68 commit e9fef9d

960 files changed

Lines changed: 11294 additions & 11392 deletions

File tree

Some content is hidden

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

docs/generators/python-aiohttp.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5353
## LANGUAGE PRIMITIVES
5454

5555
<ul class="column-ul">
56-
<li>Dict</li>
57-
<li>List</li>
5856
<li>UUID</li>
5957
<li>bool</li>
6058
<li>bytes</li>

docs/generators/python-blueplanet.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5353
## LANGUAGE PRIMITIVES
5454

5555
<ul class="column-ul">
56-
<li>Dict</li>
57-
<li>List</li>
5856
<li>UUID</li>
5957
<li>bool</li>
6058
<li>bytes</li>

docs/generators/python-fastapi.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4848
## LANGUAGE PRIMITIVES
4949

5050
<ul class="column-ul">
51-
<li>Dict</li>
52-
<li>List</li>
5351
<li>UUID</li>
5452
<li>bool</li>
5553
<li>bytes</li>

docs/generators/python-flask.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5353
## LANGUAGE PRIMITIVES
5454

5555
<ul class="column-ul">
56-
<li>Dict</li>
57-
<li>List</li>
5856
<li>UUID</li>
5957
<li>bool</li>
6058
<li>bytes</li>

docs/generators/python-pydantic-v1.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4848
## LANGUAGE PRIMITIVES
4949

5050
<ul class="column-ul">
51-
<li>Dict</li>
52-
<li>List</li>
5351
<li>bool</li>
5452
<li>bytearray</li>
5553
<li>bytes</li>

docs/generators/python.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5252
## LANGUAGE PRIMITIVES
5353

5454
<ul class="column-ul">
55-
<li>Dict</li>
56-
<li>List</li>
5755
<li>UUID</li>
5856
<li>bool</li>
5957
<li>bytes</li>

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,8 @@ public DefaultCodegen() {
17711771
.build();
17721772

17731773
defaultIncludes = new HashSet<>(
1774-
Arrays.asList("double",
1774+
Arrays.asList(
1775+
"double",
17751776
"int",
17761777
"long",
17771778
"short",
@@ -1784,7 +1785,19 @@ public DefaultCodegen() {
17841785
"Void",
17851786
"Integer",
17861787
"Long",
1787-
"Float")
1788+
"Float",
1789+
// OpenAPI primitive/container keywords (these are schema-level concepts, not language types)
1790+
// and should never produce language import statements.
1791+
"string",
1792+
"number",
1793+
"integer",
1794+
"boolean",
1795+
"null",
1796+
"object",
1797+
"array",
1798+
"map",
1799+
"set"
1800+
)
17881801
);
17891802

17901803
typeMapping = new HashMap<>();

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
5151

5252
public static final String MAP_NUMBER_TO = "mapNumberTo";
5353

54+
/**
55+
* Names which should never be treated as model imports for Python.
56+
* <p>
57+
* These can leak into {@code CodegenModel.imports} from generic container/import collection logic
58+
* (e.g. schema keywords like {@code array} or typing/builtin names), and then later get
59+
* incorrectly converted into model import statements.
60+
*/
61+
private static final Set<String> PYTHON_NON_MODEL_IMPORTS = Collections.unmodifiableSet(new HashSet<>(
62+
Arrays.asList(
63+
// OpenAPI schema keywords (should never be imported)
64+
"array", "map", "set", "object",
65+
// Python builtins
66+
"list", "dict", "tuple", "set", "type",
67+
// typing names (capitalized)
68+
"List", "Dict", "Tuple", "Set", "Type"
69+
)
70+
));
71+
5472
protected String packageName = "openapi_client";
5573
@Setter protected String packageVersion = "1.0.0";
5674
@Setter protected String projectName; // for setup.py, e.g. petstore-api
@@ -101,8 +119,6 @@ public AbstractPythonCodegen() {
101119
languageSpecificPrimitives.add("float");
102120
languageSpecificPrimitives.add("list");
103121
languageSpecificPrimitives.add("dict");
104-
languageSpecificPrimitives.add("List");
105-
languageSpecificPrimitives.add("Dict");
106122
languageSpecificPrimitives.add("bool");
107123
languageSpecificPrimitives.add("str");
108124
languageSpecificPrimitives.add("datetime");
@@ -146,6 +162,21 @@ public AbstractPythonCodegen() {
146162
regexModifiers.put('x', "VERBOSE");
147163
}
148164

165+
@Override
166+
protected boolean shouldAddImport(String type) {
167+
if (type == null) {
168+
return false;
169+
}
170+
// Reject type expressions (e.g. list[str], dict[str, Foo]) and other non-symbol entries early.
171+
if (type.indexOf('[') >= 0 || type.indexOf(']') >= 0 || type.indexOf('<') >= 0 || type.indexOf('>') >= 0) {
172+
return false;
173+
}
174+
if (PYTHON_NON_MODEL_IMPORTS.contains(type)) {
175+
return false;
176+
}
177+
return super.shouldAddImport(type);
178+
}
179+
149180
@Override
150181
public void processOpts() {
151182
super.processOpts();
@@ -896,7 +927,6 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
896927
if (!model.oneOf.isEmpty()) { // oneOfValidationError
897928
codegenProperties = model.getComposedSchemas().getOneOf();
898929
moduleImports.add("typing", "Any");
899-
moduleImports.add("typing", "List");
900930
moduleImports.add("pydantic", "Field");
901931
moduleImports.add("pydantic", "StrictStr");
902932
moduleImports.add("pydantic", "ValidationError");
@@ -932,11 +962,7 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
932962
// if model_generic.mustache is used
933963
if (model.oneOf.isEmpty() && model.anyOf.isEmpty() && !model.isEnum) {
934964
moduleImports.add("typing", "ClassVar");
935-
moduleImports.add("typing", "Dict");
936965
moduleImports.add("typing", "Any");
937-
if (this.disallowAdditionalPropertiesIfNotPresent || model.isAdditionalPropertiesTrue) {
938-
moduleImports.add("typing", "List");
939-
}
940966
}
941967

942968
// if pydantic model
@@ -1571,7 +1597,7 @@ public PythonType addTypeParam(PythonType typeParam) {
15711597
* The Python / Pydantic type can be as expressive as needed:
15721598
*
15731599
* - it could simply be `str`
1574-
* - or something more complex like `Optional[List[Dict[str, List[int]]]]`.
1600+
* - or something more complex like `Optional[list[dict[str, list[int]]]]`.
15751601
*
15761602
* Note that the default value (if available) and/or the metadata about
15771603
* the field / variable being defined are *not* part of the
@@ -1773,13 +1799,9 @@ private PythonType arrayType(IJsonSchemaValidationProperties cp) {
17731799
// Also, having a set instead of list creates complications:
17741800
// random JSON serialization order, unable to easily serialize
17751801
// to JSON, etc.
1776-
//pt.setType("Set");
1777-
//moduleImports.add("typing", "Set");
1778-
pt.setType("List");
1779-
moduleImports.add("typing", "List");
1802+
pt.setType("list");
17801803
} else {
1781-
pt.setType("List");
1782-
moduleImports.add("typing", "List");
1804+
pt.setType("list");
17831805
}
17841806
pt.addTypeParam(collectionItemType(cp.getItems()));
17851807
return pt;
@@ -1828,8 +1850,7 @@ private PythonType stringType(IJsonSchemaValidationProperties cp) {
18281850
}
18291851

18301852
private PythonType mapType(IJsonSchemaValidationProperties cp) {
1831-
moduleImports.add("typing", "Dict");
1832-
PythonType pt = new PythonType("Dict");
1853+
PythonType pt = new PythonType("dict");
18331854
pt.addTypeParam(new PythonType("str"));
18341855
pt.addTypeParam(collectionItemType(cp.getItems()));
18351856
return pt;
@@ -1954,9 +1975,7 @@ private PythonType binaryType(IJsonSchemaValidationProperties cp) {
19541975
pt.addTypeParam(strt);
19551976

19561977
if (cp.getIsBinary()) {
1957-
moduleImports.add("typing", "Tuple");
1958-
1959-
PythonType tt = new PythonType("Tuple");
1978+
PythonType tt = new PythonType("tuple");
19601979
// this string is a filename, not a validated value
19611980
tt.addTypeParam(new PythonType("str"));
19621981
tt.addTypeParam(bytest);
@@ -1976,9 +1995,7 @@ private PythonType binaryType(IJsonSchemaValidationProperties cp) {
19761995
pt.addTypeParam(new PythonType("StrictStr"));
19771996

19781997
if (cp.getIsBinary()) {
1979-
moduleImports.add("typing", "Tuple");
1980-
1981-
PythonType tt = new PythonType("Tuple");
1998+
PythonType tt = new PythonType("tuple");
19821999
tt.addTypeParam(new PythonType("StrictStr"));
19832000
tt.addTypeParam(new PythonType("StrictBytes"));
19842001

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fi
9999
simpleModule.addSerializer(Boolean.class, new PythonBooleanSerializer());
100100
MAPPER.registerModule(simpleModule);
101101

102-
// TODO may remove these later to default to the setting in abstract python base class instead
103-
languageSpecificPrimitives.add("List");
104-
languageSpecificPrimitives.add("Dict");
105-
typeMapping.put("array", "List");
106-
typeMapping.put("map", "Dict");
107-
108102
// set the output folder here
109103
outputFolder = "generated-code" + File.separatorChar + "connexion";
110104

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp
4848

4949
public static final String MAP_NUMBER_TO = "mapNumberTo";
5050

51+
/**
52+
* Names which should never be treated as model imports for Python.
53+
* <p>
54+
* These can leak into {@code CodegenModel.imports} from generic container/import collection logic
55+
* (e.g. schema keywords like {@code array} or typing/builtin names), and then later get
56+
* incorrectly converted into model import statements.
57+
*/
58+
private static final Set<String> PYTHON_NON_MODEL_IMPORTS = Collections.unmodifiableSet(new HashSet<>(
59+
Arrays.asList(
60+
// OpenAPI schema keywords (should never be imported)
61+
"array", "map", "set", "object",
62+
// Python builtins
63+
"list", "dict", "tuple", "set", "type",
64+
// typing names (capitalized)
65+
"List", "Dict", "Tuple", "Set", "Type"
66+
)
67+
));
68+
5169
protected String packageName = "openapi_client";
5270
@Setter protected String packageVersion = "1.0.0";
5371
@Setter protected String projectName; // for setup.py, e.g. petstore-api
@@ -91,8 +109,6 @@ public AbstractPythonPydanticV1Codegen() {
91109
languageSpecificPrimitives.add("float");
92110
languageSpecificPrimitives.add("list");
93111
languageSpecificPrimitives.add("dict");
94-
languageSpecificPrimitives.add("List");
95-
languageSpecificPrimitives.add("Dict");
96112
languageSpecificPrimitives.add("bool");
97113
languageSpecificPrimitives.add("str");
98114
languageSpecificPrimitives.add("datetime");
@@ -136,6 +152,21 @@ public AbstractPythonPydanticV1Codegen() {
136152
regexModifiers.put('x', "VERBOSE");
137153
}
138154

155+
@Override
156+
protected boolean shouldAddImport(String type) {
157+
if (type == null) {
158+
return false;
159+
}
160+
// Reject type expressions (e.g. list[str], dict[str, Foo]) and other non-symbol entries early.
161+
if (type.indexOf('[') >= 0 || type.indexOf(']') >= 0 || type.indexOf('<') >= 0 || type.indexOf('>') >= 0) {
162+
return false;
163+
}
164+
if (PYTHON_NON_MODEL_IMPORTS.contains(type)) {
165+
return false;
166+
}
167+
return super.shouldAddImport(type);
168+
}
169+
139170
@Override
140171
public void processOpts() {
141172
super.processOpts();
@@ -843,7 +874,6 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
843874
if (!model.oneOf.isEmpty()) { // oneOfValidationError
844875
codegenProperties = model.getComposedSchemas().getOneOf();
845876
typingImports.add("Any");
846-
typingImports.add("List");
847877
pydanticImports.add("Field");
848878
pydanticImports.add("StrictStr");
849879
pydanticImports.add("ValidationError");
@@ -879,7 +909,6 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
879909
if (model.oneOf.isEmpty() && model.anyOf.isEmpty()
880910
&& !model.isEnum
881911
&& !this.disallowAdditionalPropertiesIfNotPresent) {
882-
typingImports.add("Dict");
883912
typingImports.add("Any");
884913
}
885914

@@ -1070,8 +1099,7 @@ private String getPydanticType(CodegenParameter cp,
10701099
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, postponedModelImports, postponedExampleImports, classname),
10711100
constraints);
10721101
} else if (cp.isMap) {
1073-
typingImports.add("Dict");
1074-
return String.format(Locale.ROOT, "Dict[str, %s]",
1102+
return String.format(Locale.ROOT, "dict[str, %s]",
10751103
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, postponedModelImports, postponedExampleImports, classname));
10761104
} else if (cp.isString) {
10771105
if (cp.hasValidation) {
@@ -1266,9 +1294,8 @@ private String getPydanticType(CodegenParameter cp,
12661294
} else if (cp.isUuid) {
12671295
return cp.dataType;
12681296
} else if (cp.isFreeFormObject) { // type: object
1269-
typingImports.add("Dict");
12701297
typingImports.add("Any");
1271-
return "Dict[str, Any]";
1298+
return "dict[str, Any]";
12721299
} else if (!cp.isPrimitiveType) {
12731300
// add model prefix
12741301
hasModelsToImport = true;
@@ -1351,13 +1378,11 @@ private String getPydanticType(CodegenProperty cp,
13511378
constraints += ", unique_items=True";
13521379
}
13531380
pydanticImports.add("conlist");
1354-
typingImports.add("List"); // for return type
13551381
return String.format(Locale.ROOT, "conlist(%s%s)",
13561382
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, postponedModelImports, postponedExampleImports, classname),
13571383
constraints);
13581384
} else if (cp.isMap) {
1359-
typingImports.add("Dict");
1360-
return String.format(Locale.ROOT, "Dict[str, %s]", getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, postponedModelImports, postponedExampleImports, classname));
1385+
return String.format(Locale.ROOT, "dict[str, %s]", getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, postponedModelImports, postponedExampleImports, classname));
13611386
} else if (cp.isString) {
13621387
if (cp.hasValidation) {
13631388
List<String> fieldCustomization = new ArrayList<>();
@@ -1550,9 +1575,8 @@ private String getPydanticType(CodegenProperty cp,
15501575
} else if (cp.isUuid) {
15511576
return cp.dataType;
15521577
} else if (cp.isFreeFormObject) { // type: object
1553-
typingImports.add("Dict");
15541578
typingImports.add("Any");
1555-
return "Dict[str, Any]";
1579+
return "dict[str, Any]";
15561580
} else if (!cp.isPrimitiveType || cp.isModel) { // model
15571581
// skip import if it's a circular reference
15581582
if (classname == null) {

0 commit comments

Comments
 (0)