Skip to content

Commit 00de163

Browse files
authored
[python-flask] adopt python3.5+ syntax (#16375)
* adopt python3.5+ syntax removing some residual python2 code, since it is not supported anymore, like: - no need for `six` anymore - no need for encoding utf8 in top file - remove `object` inheritance in base model - remove absolute import `__future__` * generate samples applying the new templates * update python ignore pattern ignore all the `.venv` folders
1 parent 646ec8b commit 00de163

28 files changed

Lines changed: 23 additions & 117 deletions

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,13 @@ samples/client/petstore/csharp-refactor/OpenAPIClient/nuget.exe
173173
# Python
174174
*.pyc
175175
__pycache__
176+
.venv/
176177
samples/client/petstore/python/dev-requirements.txt.log
177178
samples/client/petstore/python/swagger_client.egg-info/SOURCES.txt
178179
samples/client/petstore/python/.coverage
179180
samples/client/petstore/python/.projectile
180-
samples/client/petstore/python/.venv/
181-
samples/client/petstore/python-asyncio/.venv/
182181
samples/client/petstore/python-asyncio/.pytest_cache/
183-
samples/client/petstore/python-tornado/.venv/
182+
184183

185184
# PHP
186185
samples/client/petstore/php/OpenAPIClient-php/composer.lock
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
# coding: utf-8
2-
31
# flake8: noqa
4-
from __future__ import absolute_import
52
# import models into model package
63
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}{{/model}}
74
{{/models}}

modules/openapi-generator/src/main/resources/python-flask/base_model.mustache

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import pprint
22

3-
import six
43
import typing
54

65
from {{packageName}} import util
76

87
T = typing.TypeVar('T')
98

109

11-
class Model(object):
10+
class Model:
1211
# openapiTypes: The key is attribute name and the
1312
# value is attribute type.
1413
openapi_types: typing.Dict[str, type] = {}
@@ -29,7 +28,7 @@ class Model(object):
2928
"""
3029
result = {}
3130

32-
for attr, _ in six.iteritems(self.openapi_types):
31+
for attr in self.openapi_types:
3332
value = getattr(self, attr)
3433
if isinstance(value, list):
3534
result[attr] = list(map(

modules/openapi-generator/src/main/resources/python-flask/controller.mustache

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import connexion
2-
import six
32
from typing import Dict
43
from typing import Tuple
54
from typing import Union
@@ -97,15 +96,15 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
9796
{{#items}}
9897
{{#isDate}}
9998
if connexion.request.is_json:
100-
{{paramName}} = {k: util.deserialize_date(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
99+
{{paramName}} = {k: util.deserialize_date(v) for k, v in connexion.request.get_json().items()} # noqa: E501
101100
{{/isDate}}
102101
{{#isDateTime}}
103102
if connexion.request.is_json:
104-
{{paramName}} = {k: util.deserialize_datetime(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
103+
{{paramName}} = {k: util.deserialize_datetime(v) for k, v in connexion.request.get_json().items()} # noqa: E501
105104
{{/isDateTime}}
106105
{{#complexType}}
107106
if connexion.request.is_json:
108-
{{paramName}} = {k: {{baseType}}.from_dict(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
107+
{{paramName}} = {k: {{baseType}}.from_dict(v) for k, v in connexion.request.get_json().items()} # noqa: E501
109108
{{/complexType}}
110109
{{/items}}
111110
{{/isMap}}

modules/openapi-generator/src/main/resources/python-flask/controller_test.mustache

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
# coding: utf-8
2-
3-
from __future__ import absolute_import
41
import unittest
52

63
from flask import json
7-
from six import BytesIO
84

95
{{#imports}}{{import}} # noqa: E501
106
{{/imports}}

modules/openapi-generator/src/main/resources/python-flask/encoder.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from connexion.apps.flask_app import FlaskJSONEncoder
2-
import six
32

43
from {{modelPackage}}.base_model import Model
54

@@ -10,7 +9,7 @@ class JSONEncoder(FlaskJSONEncoder):
109
def default(self, o):
1110
if isinstance(o, Model):
1211
dikt = {}
13-
for attr, _ in six.iteritems(o.openapi_types):
12+
for attr in o.openapi_types:
1413
value = getattr(o, attr)
1514
if value is None and not self.include_nulls:
1615
continue

modules/openapi-generator/src/main/resources/python-flask/model.mustache

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# coding: utf-8
2-
3-
from __future__ import absolute_import
41
from datetime import date, datetime # noqa: F401
52

63
from typing import List, Dict # noqa: F401

modules/openapi-generator/src/main/resources/python-flask/setup.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# coding: utf-8
2-
31
import sys
42
from setuptools import setup, find_packages
53

modules/openapi-generator/src/main/resources/python-flask/typing_utils.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# coding: utf-8
2-
31
import sys
42

53
if sys.version_info < (3, 7):

modules/openapi-generator/src/main/resources/python-flask/util.mustache

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime
22

3-
import six
43
import typing
54
from {{packageName}} import typing_utils
65

@@ -16,7 +15,7 @@ def _deserialize(data, klass):
1615
if data is None:
1716
return None
1817

19-
if klass in six.integer_types or klass in (float, str, bool, bytearray):
18+
if klass in (int, float, str, bool, bytearray):
2019
return _deserialize_primitive(data, klass)
2120
elif klass == object:
2221
return _deserialize_object(data)
@@ -45,7 +44,7 @@ def _deserialize_primitive(data, klass):
4544
try:
4645
value = klass(data)
4746
except UnicodeEncodeError:
48-
value = six.u(data)
47+
value = data
4948
except TypeError:
5049
value = data
5150
return value
@@ -110,7 +109,7 @@ def deserialize_model(data, klass):
110109
if not instance.openapi_types:
111110
return data
112111

113-
for attr, attr_type in six.iteritems(instance.openapi_types):
112+
for attr, attr_type in instance.openapi_types.items():
114113
if data is not None \
115114
and instance.attribute_map[attr] in data \
116115
and isinstance(data, (list, dict)):
@@ -145,4 +144,4 @@ def _deserialize_dict(data, boxed_type):
145144
:rtype: dict
146145
"""
147146
return {k: _deserialize(v, boxed_type)
148-
for k, v in six.iteritems(data)}
147+
for k, v in data.items() }

0 commit comments

Comments
 (0)