-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[python-client] Modify python templates to resolve linting errors #6839
[python-client] Modify python templates to resolve linting errors #6839
Conversation
@@ -189,21 +190,16 @@ public void processOpts() { | |||
setPackageUrl((String) additionalProperties.get(PACKAGE_URL)); | |||
} | |||
|
|||
String swaggerFolder = packageName; | |||
|
|||
modelPackage = swaggerFolder + File.separatorChar + "models"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were overriding the real values of modelPackage
and apiPackage
configurations as specified by user.
Also resulted in not being able to leverage for creating import statements.
} | ||
|
||
private static String dropDots(String str) { | ||
return str.replaceAll("\\.", "_"); | ||
} | ||
|
||
@Override | ||
public String toModelImport(String name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added from server implementation to enable the use of model imports like done within server.
{{>partial_header}} | ||
|
||
from __future__ import absolute_import | ||
|
||
# import models into sdk package | ||
{{#models}}{{#model}}from .models.{{classFilename}} import {{classname}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hardcoded models
and apis
were overriding the values specified by user during configuration.
|
||
self.last_response = response_data | ||
{{^tornado}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resulted in a return statement within a generator which happens when tornado used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand it. Who calls deserialize
? This method returns None
now.
@@ -53,13 +48,15 @@ class RESTClientObject: | |||
ca_certs = certifi.where() | |||
|
|||
ssl_context = ssl.SSLContext() | |||
ssl_context.load_verify_locations(cafile=ca_certs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolves the missing use of SSL when using asyncio.
} | ||
{{#discriminator}} | ||
|
||
discriminator_value_class_map = { | ||
{{#children}}'{{vendorExtensions.x-discriminator-value}}': '{{{classname}}}'{{^-last}}, | ||
{{#children}}'{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}': '{{{classname}}}'{{^-last}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolves issue where if the user doesn't specify x-discriminator-value
and the key value was always an empty string.
@@ -55,9 +50,10 @@ class RESTClientObject: | |||
# if not set certificate file, use Mozilla's root certificates. | |||
ca_certs = certifi.where() | |||
|
|||
self.ssl_context = ssl_context = ssl.SSLContext() | |||
self.ssl_context = ssl.SSLContext() | |||
self.ssl_context.load_verify_locations(cafile=ca_certs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adds the missing use of ca_certs.
11ebea2
to
b0668f6
Compare
|
||
def setUp(self): | ||
self.api = {{packageName}}.apis.{{classVarName}}.{{classname}}() | ||
self.api = {{packageName}}.apis.{{classVarName}}.{{classname}}() # noqa: E501 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it use apiPackage
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great catch
Overall, the PR looks good to me. @kenjones-cisco What are the left linter warnings that are not addressed yet? If we squash all the linter messages, we can make linting required to pass CI. |
{{#vars}}'{{name}}': '{{{datatype}}}'{{#hasMore}}, | ||
{{/hasMore}}{{/vars}} | ||
{{#vars}} | ||
'{{name}}': {{{datatype}}}{{#hasMore}},{{/hasMore}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The {{{datatype}}}
has to be quoted as it should be a string, otherwise, it generates a broken code.
The remaining ones are because the imports are there but the implementation does not currently exist to use the imported modules. (ie within the api tests generated) |
042f49e
to
906e0e9
Compare
After the updates recommended and an ignore to handle to difference between python2 vs python3, each client has the same number of lint errors, 120. 120 are unused imports in the generated test modules. |
@wing328 Do you happen to know how to fix Travis?
|
Since the generated tests are just placeholders, I would like to suggest we just drop them from linting altogether (exclude the |
@frol I've seen this many times recently. Let me try something out tomorrow to see if that helps. |
The linting results for the generated samples are as follows where the first number is the BEFORE and the second is AFTER. pyclient 7714 vs. 120 pyclient3 7717 vs. 120 pyclient3-asyncio 7584 vs. 120 pyclient-tornado 7633 vs. 120 pyclient3-tornado 7633 vs. 120 For the complete details please see the following gist. https://gist.github.com/kenjones-cisco/2eb69a7e8db75e9fd53789f01570d9f2 Enforces linting for python clients by running flake8 for the generated python client.
906e0e9
to
eea711c
Compare
@frol Update the test scripts for python client to run |
Looks like all jobs were successful, not sure why Shippable is not updating the status on the PR but the Job says Success when looking at it. |
@kenjones-cisco Could you, please, update CI scripts so Python samples get linted automatically and fail if any warning is there? This way we will be sure that we won't shift from the guidelines in future. |
@@ -23,6 +23,9 @@ python setup.py develop | |||
### run tests | |||
nosetests | |||
|
|||
### static analysis of code | |||
flake8 --show-source petstore_api/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Static analysis performed on client as part of CI testing
@@ -23,6 +23,9 @@ python setup.py develop | |||
### run tests | |||
tox | |||
|
|||
### static analysis of code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Static analysis performed on client as part of CI testing
@frol I did update the CI scripts I just added a review statement for the lines in this PR for the CI scripts. |
@kenjones-cisco Great! Thank you for your work! @wing328 This PR is awesome and it is ready to merge. |
|
||
# import ApiClient | ||
from .api_client import ApiClient | ||
|
||
from .configuration import Configuration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like breaking changes. Some libraries embed generated code and use relative imports.
Change apis
to api
can be a problem too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The apis
to api
was actually a bugfix as the default was defined as api
but a typo later in the Java code for generating code had a hard coded path of apis
.
{{>partial_header}} | ||
|
||
from __future__ import absolute_import | ||
|
||
# import models into model package | ||
{{#models}}{{#model}}from .{{classFilename}} import {{classname}}{{/model}} | ||
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}{{/model}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wing328 this change broke our use case - we generate code and put it inside our library, so we don't really install swagger generated code itself. so now we get import errors because "swagger_client" is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swagger_client
is just the default top level package name, as part of the inputs when generating the code you can specify the package name to match the package name you leverage within your code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current structure:
some_client/swagger_client
some_client/init.py
some_client/client.py - where we import swagger_client stuff
....
change to the name will not fix the problem unfortunately, we need those relative imports :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just opened #8738 to address the need for relative imports
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.3.0.0
branch for changes related to OpenAPI spec 3.0. Default:master
.Description of the PR
(details of the change, additional tests that have been done, reference to the issue for tracking, etc)
The linting results for the generated samples are as follows
where the first number is the BEFORE and the second is AFTER.
pyclient 7714 vs. 120
pyclient3 7717 vs. 120
pyclient3-asyncio 7584 vs. 120
pyclient-tornado 7633 vs. 120
pyclient3-tornado 7633 vs. 120
For the complete details please see the following gist.
https://gist.github.com/kenjones-cisco/2eb69a7e8db75e9fd53789f01570d9f2
@taxpon @frol @mbohlool @cbornet
CC @wing328