Skip to content

Commit

Permalink
Enable more ruff rulesets.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Dec 17, 2023
1 parent 3f2edbf commit db2edac
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 66 deletions.
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def tests(session):

if session.posargs and session.posargs[0] == "coverage":
if len(session.posargs) > 1 and session.posargs[1] == "github":
github = os.environ["GITHUB_STEP_SUMMARY"]
github = Path(os.environ["GITHUB_STEP_SUMMARY"])
else:
github = None

Expand All @@ -50,7 +50,7 @@ def tests(session):
if github is None:
session.run("coverage", "report")
else:
with open(github, "a") as summary:
with github.open("a") as summary:
summary.write("### Coverage\n\n")
summary.flush() # without a flush, output seems out of order.
session.run(
Expand Down
97 changes: 55 additions & 42 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ source = "vcs"
[project]
name = "referencing"
description = "JSON Referencing + Python"
requires-python = ">=3.8"
readme = "README.rst"
license = {text = "MIT"}
requires-python = ">=3.8"
keywords = ["json", "referencing", "jsonschema", "openapi", "asyncapi"]
authors = [
{email = "[email protected]"},
Expand Down Expand Up @@ -66,6 +66,7 @@ skip_covered = true

[tool.doc8]
ignore = [
"D000", # see PyCQA/doc8#125
"D001", # one sentence per line, so max length doesn't make sense
]

Expand All @@ -87,52 +88,64 @@ exclude = [

[tool.ruff]
line-length = 79
select = [
"ANN", "B", "D", "D204", "E", "F", "Q", "RUF", "SIM", "TCH", "UP", "W",
]
select = ["ALL"]
ignore = [
# Wat, type annotations for self and cls, why is this a thing?
"ANN101",
"ANN102",
# Private annotations are fine to leave out.
"ANN202",
# I don't know how to more properly annotate "pass along all arguments".
"ANN401",
# It's totally OK to call functions for default arguments.
"B008",
# raise SomeException(...) is fine.
"B904",
# There's no need for explicit strict, this is simply zip's default behavior.
"B905",
# It's fine to not have docstrings for magic methods.
"D105",
# __init__ especially doesn't need a docstring
"D107",
# This rule makes diffs uglier when expanding docstrings (and it's uglier)
"D200",
# No blank lines before docstrings.
"D203",
# Start docstrings on the second line.
"D212",
# This rule misses sassy docstrings ending with ! or ?.
"D400",
# Section headers should end with a colon not a newline
"D406",
# Underlines aren't needed
"D407",
# Plz spaces after section headers
"D412",
# Not sure what heuristic this uses, but it seems easy for it to be wrong.
"SIM300",
# We support 3.8 + 3.9
"UP007",
"A001", # It's fine to shadow builtins
"A002",
"A003",
"ARG", # This is all wrong whenever an interface is involved
"ANN", # Just let the type checker do this
"B008", # It's totally OK to call functions for default arguments.
"B904", # raise SomeException(...) is fine.
"B905", # No need for explicit strict, this is simply zip's default behavior
"C408", # Calling dict is fine when it saves quoting the keys
"C901", # Not really something to focus on
"D105", # It's fine to not have docstrings for magic methods.
"D107", # __init__ especially doesn't need a docstring
"D200", # This rule makes diffs uglier when expanding docstrings
"D203", # No blank lines before docstrings.
"D212", # Start docstrings on the second line.
"D400", # This rule misses sassy docstrings ending with ! or ?
"D401", # This rule is too flaky.
"D406", # Section headers should end with a colon not a newline
"D407", # Underlines aren't needed
"D412", # Plz spaces after section headers
"EM101", # These don't bother me, it's fine there's some duplication.
"EM102",
"FBT", # It's worth avoiding boolean args but I don't care to enforce it
"FIX", # Yes thanks, if I could it wouldn't be there
"I001", # We can't yet use ruff's isort
"N", # These naming rules are silly
"PLR0912", # These metrics are fine to be aware of but not to enforce
"PLR0913",
"PLR0915",
"PLW2901", # Shadowing for loop variables is occasionally fine.
"PT006", # pytest parametrize takes strings as well
"PYI025", # wat, I'm not confused, thanks.
"RET502", # Returning None implicitly is fine
"RET503",
"RET505", # These push you to use `if` instead of `elif`, but for no reason
"RET506",
"RSE102", # Ha, what, who even knew you could leave the parens off. But no.
"SIM300", # Not sure what heuristic this uses, but it's easily incorrect
"SLF001", # Private usage within this package itself is fine
"TD", # These TODO style rules are also silly
"UP007", # We support 3.8 + 3.9
]
extend-exclude = ["suite"]

[tool.ruff.lint.flake8-pytest-style]
mark-parentheses = false

[tool.ruff.flake8-quotes]
docstring-quotes = "double"

[tool.ruff.lint.isort]
combine-as-imports = true
from-first = true

[tool.ruff.per-file-ignores]
"noxfile.py" = ["ANN", "D100"]
"docs/*" = ["ANN", "D"]
"referencing/tests/*" = ["ANN", "D", "RUF012"]
"noxfile.py" = ["ANN", "D100", "S101", "T201"]
"docs/*" = ["ANN", "D", "INP001"]
"referencing/tests/*" = ["ANN", "D", "RUF012", "S", "PLR", "TRY"]
"referencing/typing.py" = ["PLC0414"]
13 changes: 7 additions & 6 deletions referencing/_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ def frozen(cls: type[_T]) -> type[_T]:


class UnsupportedSubclassing(Exception):
pass
def __str__(self):
return (
"Subclassing is not part of referencing's public API. "
"If no other suitable API exists for what you're trying to do, "
"feel free to file an issue asking for one."
)


@staticmethod
def _do_not_subclass() -> NoReturn: # pragma: no cover
raise UnsupportedSubclassing(
"Subclassing is not part of referencing's public API. "
"If no other suitable API exists for what you're trying to do, "
"feel free to file an issue asking for one.",
)
raise UnsupportedSubclassing()
17 changes: 9 additions & 8 deletions referencing/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@ def pointer(self, pointer: str, resolver: Resolver[D]) -> Resolved[D]:
segment = segment.replace("~1", "/").replace("~0", "~")
try:
contents = contents[segment] # type: ignore[reportUnknownArgumentType]
except LookupError:
raise exceptions.PointerToNowhere(ref=pointer, resource=self)
except LookupError as lookup_error:
error = exceptions.PointerToNowhere(ref=pointer, resource=self)
raise error from lookup_error

segments.append(segment)
last = resolver
Expand Down Expand Up @@ -326,7 +327,7 @@ def __getitem__(self, uri: URI) -> Resource[D]:
try:
return self._resources[uri.rstrip("#")]
except KeyError:
raise exceptions.NoSuchResource(ref=uri)
raise exceptions.NoSuchResource(ref=uri) from None

def __iter__(self) -> Iterator[URI]:
"""
Expand Down Expand Up @@ -421,8 +422,8 @@ def get_or_retrieve(self, uri: URI) -> Retrieved[D, Resource[D]]:
exceptions.NoSuchResource,
):
raise
except Exception:
raise exceptions.Unretrievable(ref=uri)
except Exception as error: # noqa: BLE001
raise exceptions.Unretrievable(ref=uri) from error
else:
registry = registry.with_resource(uri, resource)
return Retrieved(registry=registry, value=resource)
Expand Down Expand Up @@ -556,7 +557,7 @@ def combine(self, *registries: Registry[D]) -> Registry[D]:

if registry._retrieve is not _fail_to_retrieve:
if registry._retrieve is not retrieve is not _fail_to_retrieve:
raise ValueError(
raise ValueError( # noqa: TRY003
"Cannot combine registries with conflicting retrieval "
"functions.",
)
Expand Down Expand Up @@ -668,8 +669,8 @@ def lookup(self, ref: URI) -> Resolved[D]:
retrieved = self._registry.get_or_retrieve(uri)
except exceptions.NoSuchResource:
raise exceptions.Unresolvable(ref=ref) from None
except exceptions.Unretrievable:
raise exceptions.Unresolvable(ref=ref)
except exceptions.Unretrievable as error:
raise exceptions.Unresolvable(ref=ref) from error

if fragment.startswith("/"):
resolver = self._evolve(registry=retrieved.registry, base_uri=uri)
Expand Down
10 changes: 5 additions & 5 deletions referencing/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NoSuchResource(KeyError):

ref: URI

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if self.__class__ is not other.__class__:
return NotImplemented
return attrs.astuple(self) == attrs.astuple(other)
Expand All @@ -47,7 +47,7 @@ class NoInternalID(Exception):

resource: Resource[Any]

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if self.__class__ is not other.__class__:
return NotImplemented
return attrs.astuple(self) == attrs.astuple(other)
Expand All @@ -64,7 +64,7 @@ class Unretrievable(KeyError):

ref: URI

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if self.__class__ is not other.__class__:
return NotImplemented
return attrs.astuple(self) == attrs.astuple(other)
Expand All @@ -84,7 +84,7 @@ class CannotDetermineSpecification(Exception):

contents: Any

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if self.__class__ is not other.__class__:
return NotImplemented
return attrs.astuple(self) == attrs.astuple(other)
Expand All @@ -101,7 +101,7 @@ class Unresolvable(Exception):

ref: URI

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if self.__class__ is not other.__class__:
return NotImplemented
return attrs.astuple(self) == attrs.astuple(other)
Expand Down
7 changes: 4 additions & 3 deletions referencing/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ def maybe_in_subresource(
) -> _Resolver[Any]:
_segments = iter(segments)
for segment in _segments:
if (
segment == "items" or segment == "dependencies"
) and isinstance(subresource.contents, Mapping):
if segment in {"items", "dependencies"} and isinstance(
subresource.contents,
Mapping,
):
return resolver.in_subresource(subresource)
if segment not in in_value and (
segment not in in_child or next(_segments, None) is None
Expand Down

0 comments on commit db2edac

Please sign in to comment.