From ea71bc85c441c6893c092c8c04c013e1a9786161 Mon Sep 17 00:00:00 2001 From: Owen Littlejohns Date: Wed, 2 Aug 2023 13:20:18 -0400 Subject: [PATCH 1/2] DAS-1892 - Add pycodestyle check to automated test suite. --- dev-requirements.txt | 1 + tests/test_code_format.py | 31 +++++++++++++++++++++++++++++++ varinfo/cf_config.py | 4 ++-- varinfo/var_info.py | 10 +++++++--- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/test_code_format.py diff --git a/dev-requirements.txt b/dev-requirements.txt index 942a6b3..3565fcb 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,5 +1,6 @@ coverage ~= 5.5 ipython ~= 8.0.1 jsonschema ~= 4.17.3 +pycodestyle ~= 2.11.0 pylint >= 2.5.0 unittest-xml-reporting ~= 3.0.4 diff --git a/tests/test_code_format.py b/tests/test_code_format.py new file mode 100644 index 0000000..44e0730 --- /dev/null +++ b/tests/test_code_format.py @@ -0,0 +1,31 @@ +from pathlib import Path +from unittest import TestCase + +from pycodestyle import StyleGuide + + +class TestCodeFormat(TestCase): + """ This test class should ensure all earthdata-varinfo Python code adheres + to standard Python code styling. + + Ignored errors and warnings: + + * E501: Line length, which defaults to 80 characters. This is a + preferred feature of the code, but not always easily achieved. + * W503: Break before binary operator. Have to ignore one of W503 or + W504 to allow for breaking of some long lines. PEP8 suggests + breaking the line for a binary operator is more "Pythonic". + + """ + @classmethod + def setUpClass(cls): + cls.python_files = Path('varinfo').rglob('*.py') + + def test_pycodestyle_adherence(self): + """ Ensure all code in the `varinfo` directory adheres to PEP8 defined + standards. + + """ + style_guide = StyleGuide(ignore=['E501', 'W503']) + results = style_guide.check_files(self.python_files) + self.assertEqual(results.total_errors, 0, 'Found code style issues.') diff --git a/varinfo/cf_config.py b/varinfo/cf_config.py index 188e65a..f716a67 100644 --- a/varinfo/cf_config.py +++ b/varinfo/cf_config.py @@ -125,8 +125,8 @@ class object. mission_matches = re.match(mission, self.mission) is not None short_name_matches = ( - short_name is None or - re.match(short_name, self.short_name) is not None + short_name is None + or re.match(short_name, self.short_name) is not None ) return mission_matches and short_name_matches diff --git a/varinfo/var_info.py b/varinfo/var_info.py index d319087..03c68b9 100644 --- a/varinfo/var_info.py +++ b/varinfo/var_info.py @@ -417,9 +417,13 @@ def group_variables_by_horizontal_dimensions(self) -> DimensionsGroupType: for grid_dimensions, variables in grid_groups.items(): horizontal_dimensions = tuple( dimension for dimension in grid_dimensions - if (self.get_variable(dimension) is not None and - (self.get_variable(dimension).is_geographic() or - self.get_variable(dimension).is_projection_x_or_y())) + if ( + self.get_variable(dimension) is not None + and ( + self.get_variable(dimension).is_geographic() + or self.get_variable(dimension).is_projection_x_or_y() + ) + ) ) if horizontal_dimensions in horizontal_groups: From a323c20d121c2f78ad724fed631247e69f886349 Mon Sep 17 00:00:00 2001 From: Owen Littlejohns Date: Wed, 2 Aug 2023 13:26:01 -0400 Subject: [PATCH 2/2] DAS-1892 - Fix typo in test documentation string. --- tests/test_code_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_code_format.py b/tests/test_code_format.py index 44e0730..09b7adf 100644 --- a/tests/test_code_format.py +++ b/tests/test_code_format.py @@ -14,7 +14,7 @@ class TestCodeFormat(TestCase): preferred feature of the code, but not always easily achieved. * W503: Break before binary operator. Have to ignore one of W503 or W504 to allow for breaking of some long lines. PEP8 suggests - breaking the line for a binary operator is more "Pythonic". + breaking the line before a binary operator is more "Pythonic". """ @classmethod