diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index a59b6a3..ef1d0f6 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -28,10 +28,7 @@ jobs: python -m pip install --upgrade pip pip install -r requirements-test.txt - name: Lint - # FIXME - continue-on-error: true - run: | - flake8 lib/pyld tests --count --show-source --statistics + run: make lint test: runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/Makefile b/Makefile index 838f84b..594d886 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,17 @@ test: pytest upgrade-submodules: - git submodule update --remote --init --recursive \ No newline at end of file + git submodule update --remote --init --recursive + +# At this stage, we are limiting our formatting efforts to one file. We need to ensure that: +# * our formatting rules are sane, +# * we are not introducing conflicts with currently open PRs, +# * and the PR introducing `ruff` is not too large. +RUFF_TARGET = lib/pyld/context_resolver.py + +lint: + ruff check $(RUFF_TARGET) + +fmt: + ruff check --fix $(RUFF_TARGET) + ruff format $(RUFF_TARGET) diff --git a/lib/pyld/context_resolver.py b/lib/pyld/context_resolver.py index 784821a..7cee671 100644 --- a/lib/pyld/context_resolver.py +++ b/lib/pyld/context_resolver.py @@ -9,8 +9,10 @@ """ from frozendict import frozendict + from c14n.Canonicalize import canonicalize -from pyld import jsonld, iri_resolver +from pyld import iri_resolver, jsonld + from .resolved_context import ResolvedContext MAX_CONTEXT_URLS = 10 @@ -42,7 +44,7 @@ def resolve(self, active_ctx, context, base, cycles=None): cycles = set() # process `@context` - if (isinstance(context, dict) or isinstance(context, frozendict)) and '@context' in context: + if isinstance(context, (dict, frozendict)) and '@context' in context: context = context['@context'] # context is one or more contexts @@ -154,7 +156,7 @@ def _fetch_context(self, active_ctx, url, cycles): 'provided for a remote context.', 'jsonld.InvalidUrl', {'url': url, 'cause': cause}, - code='loading remote context failed') + code='loading remote context failed') from cause # ensure ctx is an object if not isinstance(context, dict) and not isinstance(context, frozendict): @@ -201,7 +203,7 @@ def _resolve_context_urls(self, context, base): for num, element in enumerate(ctx): if isinstance(element, str): ctx[num] = iri_resolver.resolve(element, base) - elif isinstance(element, dict) or isinstance(element, frozendict): + elif isinstance(element, (dict, frozendict)): self. _resolve_context_urls({'@context': element}, base) return diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..b596e81 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,57 @@ +[tool.ruff] +# Target Python version +target-version = "py310" + +# Line length (matching rdflib's flake8 config) +line-length = 88 + +# Include and exclude patterns +include = ["*.py"] +exclude = [ + ".git", + "__pycache__", + ".venv", + "venv", + "build", + "dist", + "*.egg-info", +] + +[tool.ruff.lint] +# Enable common rule sets +select = [ + "E", # pycodestyle errors + "W", # pycodestyle warnings + "F", # pyflakes + "I", # isort (import sorting) + "N", # pep8-naming + "UP", # pyupgrade + "B", # flake8-bugbear + "C4", # flake8-comprehensions + "SIM", # flake8-simplify +] + +# Ignore specific rules if needed +ignore = [ + "E501", # line too long (handled by formatter, matches rdflib's flake8) + "F821", # undefined name (matches rdflib's flake8 W806 ignore) + "Q000", # quote style (preserved by formatter) + "Q001", # quote style (preserved by formatter) + "Q002", # quote style (preserved by formatter) + "Q003", # quote style (preserved by formatter) +] + +[tool.ruff.format] +# Preserve existing quote styles (don't enforce single or double quotes) +quote-style = "preserve" +# Indent with spaces +indent-style = "space" +# Use 4 spaces for indentation +skip-magic-trailing-comma = false +# Line ending style +line-ending = "auto" + +[tool.ruff.lint.isort] +# Import sorting configuration +known-first-party = ["pyld", "c14n"] + diff --git a/requirements-test.txt b/requirements-test.txt index aad120b..68fc11a 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,2 +1,2 @@ -flake8 -pytest \ No newline at end of file +ruff +pytest