Skip to content

Commit

Permalink
switch to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfiola committed Feb 27, 2024
1 parent ec7988e commit 188d74d
Show file tree
Hide file tree
Showing 32 changed files with 141 additions and 217 deletions.
8 changes: 0 additions & 8 deletions .coveragerc

This file was deleted.

8 changes: 0 additions & 8 deletions .flake8

This file was deleted.

79 changes: 13 additions & 66 deletions .github/workflows/quality-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: quality checks
on: push

jobs:
black-lint:
ruff-lint:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
runs-on: ubuntu-latest
steps:
Expand All @@ -13,75 +13,22 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: "3.10"
- run: pip install -r requirements-dev.txt
- run: black . --check
flake8-lint:
- run: pip install -e ".[dev]"
- run: ruff check .
unit-test:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
needs: [ruff-lint]
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.10"
- run: pip install -r requirements-dev.txt
- run: flake8 .
pydocstyle-lint:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
runs-on: ubuntu-latest
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.10"
- run: pip install -r requirements-dev.txt
- run: pydocstyle src
unit-test-python-310:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
needs: [black-lint, flake8-lint, pydocstyle-lint]
runs-on: ubuntu-latest
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.10"
- run: pip install -r requirements.txt
- run: pip install -r requirements-dev.txt
- run: pip install -e .
- run: coverage run -a -m pytest tests/unit
unit-test-python-311:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
needs: [ black-lint, flake8-lint, pydocstyle-lint ]
runs-on: ubuntu-latest
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.11"
- run: pip install -r requirements.txt
- run: pip install -r requirements-dev.txt
- run: pip install -e .
- run: coverage run -a -m pytest tests/unit
unit-test-python-312:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
needs: [ black-lint, flake8-lint, pydocstyle-lint ]
runs-on: ubuntu-latest
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: "3.12"
- run: pip install -r requirements.txt
- run: pip install -r requirements-dev.txt
- run: pip install -e .
- run: coverage run -a -m pytest tests/unit
python-version: ${{ matrix.python-version }}
- run: pip install tox
- run: tox

3 changes: 0 additions & 3 deletions .pydocstyle

This file was deleted.

Empty file added .tox/.pkg/file.lock
Empty file.
3 changes: 1 addition & 2 deletions example/test_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging

from locust import between, task

from grasshopper.lib.grasshopper import Grasshopper
from grasshopper.lib.journeys.base_journey import BaseJourney
from grasshopper.lib.util.utils import check
from locust import between, task

logger = logging.getLogger(__name__)

Expand Down
33 changes: 7 additions & 26 deletions pre-commit
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/bin/sh

# Verify that paths in index (i.e. files being committed) comply
# with the pipeline codestyle checks and create a commit
# Verify that paths in index (i.e. files being committed) comply
# with the pipeline codestyle checks and create a commit
# only if all checks pass
# https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
files_changed=$(git diff --diff-filter=ACM --cached --name-only -- "*.py" "requirements*.txt")
files_changed=$(git diff --diff-filter=ACM --cached --name-only -- "*.py")
files_change_py=$(git diff --diff-filter=ACM --cached --name-only -- "*.py")
files_changed_no_tests=$(git diff --diff-filter=ACM --cached --name-only -- "*.py" |
grep -ve "test_[^\\\/]\+\.py")
files_change_requirements=$(git diff --diff-filter=ACM --cached --name-only -- "requirements*.txt")

exit_on_error() {
exit_code=$1
Expand All @@ -22,32 +21,14 @@ exit_on_error() {
[ -z "$files_changed" ] && exit 0

if [ -n "$files_change_py" ]; then
echo "Sorting imports with isort..."
isort $files_change_py
echo "Checking ruff..."
ruff check --fix $files_change_py
exit_on_error $?
fi

if [ -n "$files_change_py" ]; then
echo "Formatting with black..."
black $files_change_py
exit_on_error $?
fi

if [ -n "$files_change_py" ]; then
echo "Checking flake8..."
flake8 $files_change_py
exit_on_error $?
fi

if [ -n "$files_change_requirements" ]; then
echo "Sorting requirements files..."
sort-requirements $files_change_requirements
exit_on_error $?
fi

if [ -n "$files_changed_no_tests" ]; then
echo "Checking pydocstyle..."
pydocstyle $files_changed_no_tests
echo "Formatting with ruff..."
ruff format $files_change_py
exit_on_error $?
fi

Expand Down
89 changes: 73 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=42", "wheel", "cmake>=3.11.0,<4.0.0"]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
Expand Down Expand Up @@ -40,26 +40,83 @@ dependencies = [
"termcolor ~= 1.1.0",
]

[project.optional-dependencies]
dev = [
# Testing stuff
"assertpy==1.1",
"pytest-cov==4.0.0",
"pytest-mock==3.10.0",
"requests-mock==1.11.0",
"tox==4.13.0",

# Linting and formatting
"ruff==0.2.2",
]

[project.urls]
"repository" = "https://github.com/alteryx/locust-grasshopper"

[tool.setuptools.packages.find]
where = ["src"]

[tool.isort]
profile = "black"
known_local_folder = [
"grasshopper"
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-rap --junitxml=reports/junit_test_results.xml --cov=src --cov-report=html --cov-report=xml --cov-report=term"
testpaths = [
"tests/unit",
]
multi_line_output = 3
atomic = "True"
honor_noqa = "True"
include_trailing_comma = "True"
force_grid_wrap = "0"
use_parentheses = "True"
ensure_newline_before_comments = "True"
line_length = "88"
treat_all_comments_as_code = "True"

[tool.black]
junit_suite_name = "locust-grasshopper"
log_level = "INFO"
xfail_strict = "True"
log_cli = "True"
log_cli_level = "INFO"
log_cli_format = "%(asctime)s [%(levelname)s] %(module)s:%(filename)s:%(lineno)s %(message)s"
log_file = "tests.log"
log_file_level = "DEBUG"
log_file_format = "%(asctime)s [%(levelname)s] %(module)s:%(filename)s:%(lineno)s %(message)s"
log_file_date_format = "%m-%d-%Y %H:%M:%S"

[tool.tox]
legacy_tox_ini = """
[tox]
min_version = 4.0
env_list =
py310
py311
py312
[testenv]
deps = .[dev]
commands = pytest tests/unit
"""

[tool.ruff]
exclude = ["data-files", "docs", ".git", "__pycache__", "venv"]
src = ["src/*", "tests/unit/*"]
show-fixes = true
line-length = 88

[tool.ruff.lint]

# Adds Pyflakes, Mccabe, Pydocstyle, etc. run `ruff linter` to see all available linters
extend-select = ["F", "C90", "I", "N", "ICN"]
ignore = [ "N801", "N803", "N806", "N815", "N818"]

# Allows a single underscore to be an unused variable
dummy-variable-rgx = "^_$"

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

[tool.ruff.format]

[tool.ruff.lint.isort]
# order-by-type = false
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"
ignore-decorators = ["property", "pytest.fixture", "locator"]



13 changes: 0 additions & 13 deletions requirements-dev.txt

This file was deleted.

11 changes: 0 additions & 11 deletions requirements.txt

This file was deleted.

6 changes: 3 additions & 3 deletions src/grasshopper/lib/configuration/gh_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
def typecast_dict(value):
"""Ensure that value is a dict (supports json strings) or log a warning."""
new_value = value
if type(value) == str:
if isinstance(value, str):
new_value = json.loads(value)
elif type(value) != dict:
elif not isinstance(value, dict):
logger.warning(
f"Configuration value [{value}] of type [{type(value)}] not able to be "
f"cast to dictionary."
Expand All @@ -40,7 +40,7 @@ def typecast_float(value):

def typecast_bool(value):
"""Ensure value is a bool or raise an error."""
if type(value) == str:
if isinstance(value, str):
new_value = value.lower() in ["true"]
else:
new_value = bool(value)
Expand Down
7 changes: 3 additions & 4 deletions src/grasshopper/lib/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pytest
import tagmatcher
import yaml

from grasshopper.lib.configuration.gh_configuration import (
ConfigurationConstants,
GHConfiguration,
Expand Down Expand Up @@ -107,7 +106,7 @@ def env_var_prefix_key(request):
prefix = "GH_"
try:
prefix = request.getfixturevalue("configuration_prefix_key")
if type(prefix) != str or (type(prefix) == str and len(prefix) == 0):
if isinstance(prefix, str) or (isinstance(prefix, str) and len(prefix) == 0):
logger.warning(
f"CONFIG FIXTURE: Fixture configuration_prefix_key may only be a non "
f"zero length str, returned value {prefix}, ignoring value."
Expand Down Expand Up @@ -586,10 +585,10 @@ def fetch_value_from_multiple_sources(sources, key):
def type_check_list_of_strs(list_of_strs):
"""Return True if list of strings or [], false if anything else."""
check_passed = False
if type(list_of_strs) == list:
if isinstance(list_of_strs, list):
all_strs = True
for s in list_of_strs:
all_strs = all_strs and type(s) == str
all_strs = all_strs and isinstance(s, str)
check_passed = all_strs
return check_passed

Expand Down
5 changes: 2 additions & 3 deletions src/grasshopper/lib/grasshopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@

import gevent
import locust
from locust import LoadTestShape
from locust.env import Environment

from grasshopper.lib.journeys.base_journey import BaseJourney
from grasshopper.lib.util.listeners import GrasshopperListeners
from locust import LoadTestShape
from locust.env import Environment

logger = logging.getLogger()

Expand Down
3 changes: 1 addition & 2 deletions src/grasshopper/lib/journeys/base_journey.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from uuid import uuid4

import gevent
from locust import HttpUser

import grasshopper.lib.util.listeners # noqa: F401
from grasshopper.lib.fixtures.grasshopper_constants import GrasshopperConstants
from locust import HttpUser


class BaseJourney(HttpUser):
Expand Down
Loading

0 comments on commit 188d74d

Please sign in to comment.