Stricter and more checks for python code#395
Conversation
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
|
@mum4k requested a review, it would be great to have your thoughts on the broadened |
Resolves envoyproxy#371 Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
|
This now fails check_format with: So just the the missing docstrings are left to work in to complete this. |
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
mum4k
left a comment
There was a problem hiding this comment.
This is awesome, would you mind updating the PR description with the latest list of remaining findings?
| @@ -0,0 +1,98 @@ | |||
| """A library with assertation helpers for unit tests.""" | |||
There was a problem hiding this comment.
I wonder - do we have a good reason to maintain our own library as compared to using something like:
https://docs.python.org/2/library/unittest.html
Note - it would be ok to address this in a follow-up.
There was a problem hiding this comment.
This came to be out of unawareness of that library existing. I agree it would be good to move to that.
I would prefer to punt this to an issue though, to not increase size of this PR (and keep it a no-op)
| assertLessEqual(a, max_value) | ||
|
|
||
|
|
||
| def assertCounterEqual(counters, name, value): |
There was a problem hiding this comment.
Do we need assert helpers like this? Assuming we do switch to the standard unittest library, we could simply assert on:
self.assertEquals(counters[name], value)
And if the counter isn't in the dictionary, it would still "correctly" fail.
test/integration/common.py
Outdated
| Exception (string): exception message. | ||
| """ | ||
|
|
||
| pass |
There was a problem hiding this comment.
(nit, optional) "pass" is no longer needed now that we have a docstring.
test/integration/common.py
Outdated
| class NighthawkException(Exception): | ||
| """Base exception class for Nighthawk's python code. | ||
|
|
||
| Args: |
There was a problem hiding this comment.
Class docstring doesn't really have Args section, since classes don't take arguments. We would typically put that in the docstring of the init method, but that one isn't needed here.
We can just say:
class NighthawkException(Exception):
"""Base exception class for Nighthawk's python code."""| def determineIpVersionsFromEnvironment(): | ||
| """Determine the IP version(s) for test execution from the environment. | ||
|
|
||
| Raises: |
There was a problem hiding this comment.
We should only add the Raises section for exceptions raised by the function directly. Otherwise this is a futile effort since every line and character of Python code can raise at least one exception (SyntaxError, MemoryError, RuntimeError, ...).
This functions doesn't have the raise statement, so it shouldn't need the Raises section.
There was a problem hiding this comment.
determineIpVersionsFromEnvironment() has:
else:
raise NighthawkException("Unknown ip version: '%s'" % versions)
So I think in this instance it's OK?
| """ | ||
| Utility for getting the http://host:port/ that can be used to query the server we started in setUp() | ||
| """ | ||
| """Get the http://host:port/ that can be used to query the server we started in setUp().""" |
There was a problem hiding this comment.
(nit) The one-line docstring should fit on a single line (80 chars).
If it doesn't (i.e. we need to say more) we should still choose an abstract that fits into 80 charts and write the rest into the body of the docstring.
Applies in a few places.
| @@ -1,67 +1,20 @@ | |||
| """Packages utility methods for tests.""" | |||
There was a problem hiding this comment.
(nit, pre-existing) We may want to choose a better name, "utility" is a catch all and doesn't help the reader much. Even something like "test_utility" would be better.
tools/format_python_tools.sh
Outdated
| # Check everything, except indentation and line length for now. | ||
| # Also, we ignore unused imports and redefinitions of unused, as those seems to raise false flags in test definitions. | ||
| flake8 ../benchmarks/ ${EXCLUDE} --docstring-convention pep257 --ignore=E114,E111,E501,F401,F811 --count --show-source --statistics | ||
| # We ignore unused imports and redefinitions of unused, as those seems to raise false flags in test definitions. |
There was a problem hiding this comment.
Is this worth investigating? Can you help by providing some details / examples?
tools/format_python_tools.sh
Outdated
| # Also, we ignore unused imports and redefinitions of unused, as those seems to raise false flags in test definitions. | ||
| flake8 ../benchmarks/ ${EXCLUDE} --docstring-convention pep257 --ignore=E114,E111,E501,F401,F811 --count --show-source --statistics | ||
| # We ignore unused imports and redefinitions of unused, as those seems to raise false flags in test definitions. | ||
| # We ignore formatting isssues E124/E125/E126 because they conflict with the automtic fix format feature. |
There was a problem hiding this comment.
Are we using the wrong auto formatter? Which formatting issues are we ignoring here?
| flake8 ../benchmarks/ ${EXCLUDE} --docstring-convention pep257 --ignore=E114,E111,E501,F401,F811 --count --show-source --statistics | ||
| # We ignore unused imports and redefinitions of unused, as those seems to raise false flags in test definitions. | ||
| # We ignore formatting isssues E124/E125/E126 because they conflict with the automtic fix format feature. | ||
| flake8 . ${EXCLUDE} --ignore=E114,E111,E501,F401,F811,E124,E125,E126,D --count --show-source --statistics |
There was a problem hiding this comment.
What is this and the following excludes excluding?
There was a problem hiding this comment.
This ignores/excludes:
- ignored, because of conflict with the automatic fix format script. This reformats according to https://github.com/envoyproxy/nighthawk/blob/master/tools/.style.yapf. We followed Envoy's lead
there, but maybe this is a good time to evaluate that:
E111 Indentation is not a multiple of four
E114 Indentation is not a multiple of four (comment)
E501 Line too long (82 > 79 characters)
E124 Closing bracket does not match visual indentation
E125 Continuation line with same indent as next logical line
E126 Continuation line over-indented for hanging indent - False positives because of what I think are pytest peculiarities [1]. Taking a look at this again, I do think it's worth following up, because there's some actually valid complaints about unused imports too it seems. But there's also real false positives; e.g. importing pytest is something we really must do.
F401 Module imported but unused
F811 Redefinition of unused name from line n - doc conventions
D = doc comment related checks (we do those below, checking both p257 AND google conventions as both seem to have different strengths in their checks; and so far I haven't experienced any conflicts)
[1]
./test/integration/test_integration_zipkin.py:3:1: F401 'pytest' imported but unused
import pytest
^
./test/integration/test_integration_zipkin.py:7:1: F401 'integration_test_fixtures.http_test_server_fixture' imported but unused
from integration_test_fixtures import (http_test_server_fixture, server_config)
^
./test/integration/test_integration_zipkin.py:7:1: F401 'integration_test_fixtures.server_config' imported but unused
from integration_test_fixtures import (http_test_server_fixture, server_config)
^
./test/integration/test_integration_zipkin.py:11:1: F811 redefinition of unused 'http_test_server_fixture' from line 7
def test_tracing_zipkin(http_test_server_fixture):
There was a problem hiding this comment.
Thank you for the explanations and detailed list. I think it is generally fine for us to remain aligned with Envoy. I don't see anything that would be very concerning in the list above. This PR will significantly help on future reviews of Python code, thank you for these improvements.
Would you mind copying the explanation of the exceptions from this discussion thread into an inline comment next to the exceptions for posterity?
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
I updated the description -- but I kept the list, mentioning that these where the issues before the changes in here. |
Fixes envoyproxy#400 Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
|
@mum4k I added be8f7ce to update the flake8/yapf versions. This should fix the python 3.8 compatibility issue that recently came up, as well as enhances automatic formatting. Let me know if you're OK with having these changes in here, we can also split that out to a follow up (but having them in flight separately at the same time will be challenging because of lots of merge conflict opportunities). |
|
Thank you @oschaaf, it is fine to have them here. I will review them in the next few days. |
Signed-off-by: Otto van der Schaaf <oschaaf@we-amp.com>
|
(merged this, as this was approved & passing CI) |
In #337 some review comments related
to doc comments have been postponed to a followup.
Adds more flake8 convention checks. This broadens the checks we introduced in the
benchmarking flow to cover the global code base, and exclude a few
files that we either don't own or care about.
Also updates yapf & flake8 version requirements, which enhances automatic formatting,
as well as fixes a python 3.8 compatibility bug.
Should fix #371
Should fix #400
Summary of compliance failures under the proposed checks before this change:
Signed-off-by: Otto van der Schaaf oschaaf@we-amp.com