Skip to content

Commit

Permalink
Replaced flake8 with Ruff
Browse files Browse the repository at this point in the history
Flake8 giving some trouble b/c API is deprecated.  Added additional tests for pycode to make sure it worked as expected.
  • Loading branch information
e-lo committed Oct 10, 2024
1 parent d70a908 commit cc22db3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
4 changes: 2 additions & 2 deletions projectcard/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ def _change_keys(obj: dict, convert: Callable = _replace_selected) -> dict:
VALID_EXT = list(_read_method_map.keys())


def read_card(filepath: ProjectCardFilepath, validate: bool = False):
def read_card(filepath: ProjectCardFilepath, validate: bool = True):
"""Read single project card from a path and return project card object.
Args:
filepath: file where the project card is.
validate: if True, will validate the project card schemea
validate: if True, will validate the project card schema. Defaults to True.
"""
if not Path(filepath).is_file():
msg = f"Cannot find project card file: {filepath}"
Expand Down
38 changes: 22 additions & 16 deletions projectcard/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Optional, Union

import jsonref
from flake8.api import legacy as flake8
from jsonschema import validate
from jsonschema.exceptions import SchemaError, ValidationError

Expand All @@ -17,12 +16,14 @@
ROOTDIR = Path(__file__).resolve().parent
PROJECTCARD_SCHEMA = ROOTDIR / "schema" / "projectcard.json"

# Errors to catch in valdiating "wrangler" project cards which use python code.
# E9 Runtime
# F63 undefined name name
# F823 local variable name ... referenced before assignment
# F405 name may be undefined, or defined from star imports: module
FLAKE8_ERRORS = ["E9", "F821", "F823", "F405"]
CRITICAL_ERRORS = ["E9", "F821", "F823", "F405"]
"""
Errors in Ruff that will cause a code execution failure.
E9: Syntax errors.
F821: Undefined name.
F823: Local variable referenced before assignment.
F405: Name may be undefined, or defined from star imports.
"""


def _open_json(schema_path: Path) -> dict:
Expand Down Expand Up @@ -203,7 +204,6 @@ def _validate_pycode(jsondata: dict, mocked_vars: list[str] = DEFAULT_MOCKED_VAR
jsondata: project card json data as a python dictionary
mocked_vars: list of variables available in the execution of the code
"""
style_guide = flake8.get_style_guide(select=FLAKE8_ERRORS, ignore=["E", "F", "W"])
dir = TemporaryDirectory()
tmp_py_path = Path(dir.name) / "tempcode.py"
CardLogger.debug(f"Storing temporary python files at: {tmp_py_path!s}")
Expand All @@ -215,14 +215,20 @@ def _validate_pycode(jsondata: dict, mocked_vars: list[str] = DEFAULT_MOCKED_VAR

with tmp_py_path.open("w") as py_file:
py_file.write(py_file_contents)
import subprocess

report = style_guide.check_files([tmp_py_path])

if report.total_errors:
try:
result = subprocess.run(
["ruff", "check", tmp_py_path, "--select", ",".join(CRITICAL_ERRORS)],
capture_output=True,
text=True,
check=True,
)
except subprocess.CalledProcessError as e:
CardLogger.error(f"Errors found in {jsondata['project']}")
CardLogger.debug(f"FILE CONTENTS\n{py_file_contents}")
errors = {c: report.get_statistics(c) for c in FLAKE8_ERRORS if report.get_statistics(c)}
CardLogger.debug(f"Flake 8 Report:\n {errors}")
msg = f"Found {report.total_errors} errors in {jsondata['project']}"
raise PycodeError(msg)
dir.cleanup()
CardLogger.debug(f"Ruff Report:\n {e.stdout}")
msg = f"Found errors in {jsondata['project']}"
raise PycodeError(msg) from e
finally:
dir.cleanup()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ classifiers = [
]
requires-python = ">=3.9"
dependencies = [
"flake8",
"jsonref",
"jsonschema",
"pydantic>=2.0",
"pyyaml",
"tabulate",
"ruff",
"toml",
]

Expand Down
1 change: 0 additions & 1 deletion requirements.tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ mypy
pre-commit
pytest
pytest-cov
ruff
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
flake8
jsonref
jsonschema
pydantic>=2.0
pyyaml
ruff
toml
35 changes: 35 additions & 0 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Tests for the projectcard.validate module."""

import pytest

from projectcard import ProjectCard
from projectcard.errors import ProjectCardValidationError, PycodeError
from projectcard.logger import CardLogger
from projectcard.validate import validate_card


def test_update_dict_with_schema_defaults():
Expand Down Expand Up @@ -134,3 +139,33 @@ def test_update_project_card_with_defaults():
card = ProjectCard(project_card_data)
CardLogger.debug(f"card:\n{card}")
assert card.transit_service_deletion["clean_shapes"] is False


def test_good_pycode_(request):
"""Make sure bad pycode syntax will raise an error."""
CardLogger.info(f"--Starting: {request.node.name}")

_pycode = "roadway_net.links_df.loc[roadway_net.links_df['lanes'] == 5, 'lanes'] = 12"
project_data = {
"project": "Test Project",
"self_obj_type": "RoadwayNetwork",
"pycode": _pycode,
}
project_card = ProjectCard(project_data)
valid = project_card.validate()
assert valid


def test_bad_pycode_(request):
"""Make sure bad pycode syntax will raise an error."""
CardLogger.info(f"--Starting: {request.node.name}")

_pycode = "roadway_net.links_df.loc[[roadway_net.links_df['lanes'] == 5, 'lanes'] = 12"
project_data = {
"project": "Test Project",
"self_obj_type": "RoadwayNetwork",
"pycode": _pycode,
}
project_card = ProjectCard(project_data)
with pytest.raises(PycodeError):
valid = project_card.validate()

0 comments on commit cc22db3

Please sign in to comment.