Skip to content

Commit

Permalink
Run ruff linter on src and formatter on src and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samkellerhals committed Feb 21, 2024
1 parent df32186 commit 0013227
Show file tree
Hide file tree
Showing 34 changed files with 112 additions and 254 deletions.
115 changes: 12 additions & 103 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
default_language_version:
python: python3.10
repos:
# - repo: meta
# hooks:
# - id: check-hooks-apply
# - id: check-useless-excludes

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.2.2
hooks:
# Run the linter.
- id: ruff
files: ^src/
args: [--fix]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.6.0
Expand All @@ -33,20 +40,6 @@ repos:
types: [python]
args: [--comment-style, "|#|", --license-filepath, ./LICENSE_HEADER.txt, --fuzzy-match-generates-todo]

# - repo: https://github.com/asottile/yesqa
# rev: v1.4.0
# hooks:
# - id: yesqa
# additional_dependencies:
# - flake8==5.0.4 # version from constraints.txt
# - darglint
# - flake8-bugbear
# - flake8-builtins
# - flake8-debugger
# - flake8-docstrings
# - flake8-eradicate
# - flake8-mutable

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand All @@ -56,90 +49,6 @@ repos:
- id: check-yaml
- id: debug-statements

- repo: https://github.com/psf/black
##[[[cog
## import re
## version = re.search('black==([0-9\.]*)', open("constraints.txt").read())[1]
## print(f"rev: '{version}' # version from constraints.txt")
##]]]
rev: '23.9.1' # version from constraints.txt
##[[[end]]]
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
##[[[cog
## import re
## version = re.search('isort==([0-9\.]*)', open("constraints.txt").read())[1]
## print(f"rev: '{version}' # version from constraints.txt")
##]]]
rev: '5.12.0' # version from constraints.txt
##[[[end]]]
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
##[[[cog
## import re
## version = re.search('flake8==([0-9\.]*)', open("constraints.txt").read())[1]
## print(f"rev: '{version}' # version from constraints.txt")
##]]]
rev: '6.1.0' # version from constraints.txt
##[[[end]]]
hooks:
- id: flake8
additional_dependencies:
##[[[cog
## import re
## constraints = open("constraints.txt").read()
## for pkg in ["darglint", "flake8-bugbear", "flake8-builtins", "flake8-debugger", "flake8-docstrings",
## "flake8-eradicate", "flake8-mutable", "flake8-pyproject", "pygments"]:
## print(f"- {pkg}==" + str(re.search(f'\n{pkg}==([0-9\.]*)', constraints)[1]))
##]]]
- darglint==1.8.1
- flake8-bugbear==23.9.16
- flake8-builtins==2.1.0
- flake8-debugger==4.1.2
- flake8-docstrings==1.7.0
- flake8-eradicate==1.5.0
- flake8-mutable==1.2.0
- flake8-pyproject==1.2.3
- pygments==2.16.1
##[[[end]]]
# - flake8-rst-docstrings # Disabled for now due to random false positives
exclude: |
(?x)^(
setup.py |
docs/user/cartesian/conf.py |
src/gt4py/cartesian/__gtscript__.py |
src/gt4py/cartesian/__init__.py |
src/gt4py/cartesian/gtscript.py |
src/gt4py/cartesian/backend/__init__.py |
src/gt4py/cartesian/backend/pyext_builder.py |
src/gt4py/cartesian/frontend/__init__.py |
src/gt4py/cartesian/frontend/nodes.py |
src/gt4py/cartesian/frontend/node_util.py |
src/gt4py/cartesian/utils/__init__.py |
src/gt4py/cartesian/utils/base.py |
src/gt4py/cartesian/utils/attrib.py |
src/gt4py/cartesian/utils/meta.py |
src/gt4py/eve/extended_typing.py |
tests/conftest.py |
tests/cartesian_tests/integration_tests/multi_feature_tests/stencil_definitions.py |
tests/cartesian_tests/integration_tests/multi_feature_tests/test_code_generation.py |
tests/cartesian_tests/integration_tests/multi_feature_tests/utils.py |
tests/cartesian_tests/integration_tests/feature_tests/test_call_interface.py |
tests/cartesian_tests/unit_tests/frontend_tests/test_gtscript_frontend.py |
tests/next_tests/unit_tests/.* |
tests/next_tests/integration_tests/multi_feature_tests/.* |
tests/next_tests/integration_tests/feature_tests/ffront_tests/.* |
tests/next_tests/integration_tests/feature_tests/iterator_tests/.* |
tests/next_tests/integration_tests/feature_tests/otf_tests/.* |
tests/next_tests/integration_tests/feature_tests/math_builtin_test_data.py |
tests/next_tests/past_common_fixtures.py |
tests/next_tests/toy_connectivity.py |
)$
- repo: https://github.com/pre-commit/mirrors-mypy
##[[[cog
## import re
Expand All @@ -162,7 +71,7 @@ repos:
##]]]
- astunparse==1.6.3
- attrs==23.1.0
- black==23.9.1
- ruff==0.2.2
- boltons==23.0.0
- cached-property==1.5.2
- click==8.1.7
Expand Down
2 changes: 1 addition & 1 deletion constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ referencing==0.30.2 # via jsonschema, jsonschema-specifications
requests==2.31.0 # via dace, sphinx
restructuredtext-lint==1.4.0 # via flake8-rst-docstrings
rpds-py==0.10.3 # via jsonschema, referencing
ruff==0.0.290 # via -r requirements-dev.in
ruff==0.2.2 # via -r requirements-dev.in
six==1.16.0 # via asttokens, astunparse, python-dateutil
snowballstemmer==2.2.0 # via pydocstyle, sphinx
sortedcontainers==2.4.0 # via hypothesis
Expand Down
2 changes: 1 addition & 1 deletion min-extra-requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pytest-cov==2.8
pytest-factoryboy==2.0.3
pytest-xdist[psutil]==2.4
pytest==7.0
ruff==0.0.265
ruff==0.2.2
scipy==1.7.2
setuptools==65.5.0
sphinx==4.4
Expand Down
2 changes: 1 addition & 1 deletion min-requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pytest-cov==2.8
pytest-factoryboy==2.0.3
pytest-xdist[psutil]==2.4
pytest==7.0
ruff==0.0.265
ruff==0.2.2
setuptools==65.5.0
sphinx==4.4
sphinx_rtd_theme==1.0
Expand Down
115 changes: 18 additions & 97 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,89 +137,6 @@ ignore_errors = true
branch = true
source_pkgs = ['gt4py']

# -- flake8 --
# flake8-pyproject plugin is used to load configuration settings
# from this file, since flake8 doesn't support it natively.
[tool.flake8]
count = true
doctests = true
exclude = [
'.eggs',
'.gt_cache',
'.ipynb_checkpoints',
'.tox',
'_local/',
'build',
'dist',
'docs',
'tests/_disabled',
'setup.py'
]
ignore = [
'B008', # Do not perform function calls in argument defaults
'B028', # Consider replacing f"'{foo}'" with f"{foo!r}" # TODO: review this ignore
'D1', # Public code object needs docstring
'DAR', # Disable dargling errors by default
'E203', # Whitespace before ':' (black formatter breaks this sometimes)
'E501', # Line too long (using Bugbear's B950 warning)
'W503' # Line break occurred before a binary operator
]
max-complexity = 15
max-line-length = 100 # It should be the same as in `tool.black.line-length` above
per-file-ignores = [
'src/gt4py/eve/extended_typing.py:F401,F405',
'src/gt4py/next/__init__.py:F401' # We import stuff there in order to reexport.
]
rst-roles = [
'py:mod, mod',
'py:func, func',
'py:data, data',
'py:const, const',
'py:class, class',
'py:meth, meth',
'py:attr, attr',
'py:exc, exc',
'py:obj, obj'
]

# -- isort --
[tool.isort]
combine_as_imports = true
group_by_package = true
known_first_party = ['gt4py', '__externals__', '__gtscript__']
known_tests = ['cartesian_tests', 'eve_tests', 'next_tests', 'storage_tests']
known_third_party = [
'attr',
'black',
'boltons',
'cached_property',
'click',
'dace',
'devtools',
'factory',
'hypothesis',
'importlib_resources',
'jinja2',
'mako',
'networkx',
'numpy',
'packaging',
'pybind11',
'pytest',
'pytest_factoryboy',
'setuptools',
'tabulate',
'typing_extensions',
'xxhash'
]
lexicographical = true
line_length = 100 # It should be the same as in `tool.black.line-length` above
lines_after_imports = 2
profile = 'black'
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'TESTS', 'LOCALFOLDER']
skip_gitignore = true
skip_glob = ['*.venv/**', '_local/**']

# -- mypy --
[tool.mypy]
disallow_incomplete_defs = true
Expand Down Expand Up @@ -351,18 +268,25 @@ testpaths = 'tests'

# -- ruff --
[tool.ruff]
line-length = 100
respect-gitignore = true
show-fixes = true
target-version = 'py310'

[tool.ruff.format]
quote-style = "double"

[tool.ruff.lint]
ignore = [
'E501',
'B008', # Do not perform function calls in argument defaults
'B028', # Consider replacing f"'{foo}'" with f"{foo!r}" # TODO: review
'B905', # B905 `zip()` without an explicit `strict=` parameter # TODO: review
'RUF012', # RUF012 Mutable class attributes should be annotated with `typing.ClassVar` # TODO: review
'RUF001', # RUF001 String contains ambiguous `∨` (LOGICAL OR). Did you mean `v` (LATIN SMALL LETTER V)?
'RUF003', # RUF003 Comment contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?
'B028', # Consider replacing f"'{foo}'" with f"{foo!r}"
'B905', # B905 `zip()` without an explicit `strict=` parameter
'RUF012', # RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
'RUF001', # RUF001 String contains ambiguous `∨` (LOGICAL OR). Did you mean `v` (LATIN SMALL LETTER V)?
'RUF003' # RUF003 Comment contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?
]
ignore-init-module-imports = true
line-length = 100 # It should be the same as in `tool.black.line-length` above
respect-gitignore = true
# Rules:
# E: pycodestyle
# F: Pyflakes
Expand All @@ -374,13 +298,10 @@ respect-gitignore = true
# NPY: NumPy-specific rules
# RUF: Ruff-specific rules
select = ['E', 'F', 'I', 'B', 'A', 'T100', 'ERA', 'NPY', 'RUF']
show-fixes = true
# show-source = true
target-version = 'py310'
typing-modules = ['gt4py.eve.extended_typing']
unfixable = []

[tool.ruff.isort]
[tool.ruff.lint.isort]
combine-as-imports = true
# force-wrap-aliases = true
known-first-party = ['gt4py', '__externals__', '__gtscript__']
Expand Down Expand Up @@ -413,13 +334,13 @@ order-by-type = true
section-order = ['future', 'standard-library', 'third-party', 'first-party', 'tests', 'local-folder']
split-on-trailing-comma = false

[tool.ruff.isort.sections]
[tool.ruff.lint.isort.sections]
'tests' = ['cartesian_tests', 'eve_tests', 'next_tests', 'storage_tests']

[tool.ruff.mccabe]
[tool.ruff.lint.mccabe]
max-complexity = 15

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
'src/gt4py/eve/extended_typing.py' = ['F401', 'F405']

# -- setuptools build backend --
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pytest-cache>=1.0
pytest-cov>=2.8
pytest-factoryboy>=2.0.3
pytest-xdist[psutil]>=2.4
ruff>=0.0.265
ruff>=0.2.2
sphinx>=4.4
sphinx_rtd_theme>=1.0
tomli>=2.0.1
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ referencing==0.30.2 # via jsonschema, jsonschema-specifications
requests==2.31.0 # via dace, sphinx
restructuredtext-lint==1.4.0 # via flake8-rst-docstrings
rpds-py==0.10.3 # via jsonschema, referencing
ruff==0.0.290 # via -r requirements-dev.in
ruff==0.2.2 # via -r requirements-dev.in
six==1.16.0 # via asttokens, astunparse, python-dateutil
snowballstemmer==2.2.0 # via pydocstyle, sphinx
sortedcontainers==2.4.0 # via hypothesis
Expand Down
4 changes: 2 additions & 2 deletions src/gt4py/cartesian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@
"stencil_builder",
"stencil_object",
"type_hints",
"StencilObject"
)
"StencilObject",
)
4 changes: 1 addition & 3 deletions src/gt4py/cartesian/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ def convert(
try:
value = self._convert_value(backend.options[name]["type"], value, param, ctx)
except click.BadParameter as conversion_error:
self.fail(
f'Invalid value for backend option "{name}": {conversion_error.message}'
)
self.fail(f'Invalid value for backend option "{name}": {conversion_error.message}')
return (name, value)


Expand Down
6 changes: 2 additions & 4 deletions src/gt4py/cartesian/frontend/gtscript_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ def visit_Assign(self, node: ast.Assign):
else:
return self.generic_visit(node)

def visit_Call( # Cyclomatic complexity too high
self, node: ast.Call, *, target_node=None
):
def visit_Call(self, node: ast.Call, *, target_node=None): # Cyclomatic complexity too high
call_name = gt_meta.get_qualified_name_from_node(node.func)

if call_name in self.call_stack:
Expand Down Expand Up @@ -618,7 +616,7 @@ def visit_If(self, node: ast.If):


def _make_temp_decls(
descriptors: Dict[str, gtscript._FieldDescriptor]
descriptors: Dict[str, gtscript._FieldDescriptor],
) -> Dict[str, nodes.FieldDecl]:
return {
name: nodes.FieldDecl(
Expand Down
2 changes: 1 addition & 1 deletion src/gt4py/cartesian/gtc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def data_type_to_typestr(dtype: DataType) -> str:
def op_to_ufunc(
op: Union[
UnaryOperator, ArithmeticOperator, ComparisonOperator, LogicalOperator, NativeFunction
]
],
) -> np.ufunc:
if not isinstance(
op, (UnaryOperator, ArithmeticOperator, ComparisonOperator, LogicalOperator, NativeFunction)
Expand Down
Loading

0 comments on commit 0013227

Please sign in to comment.