Skip to content

Commit

Permalink
Always unset the required-version option during ecosystem checks (#…
Browse files Browse the repository at this point in the history
…9593)

Uses our existing configuration overrides to unset the
`required-version` option in ecosystem projects during checks.

The downside to this approach, is we will now update the config file of
_every_ project (with a config file). This roughly normalizes the configuration file, as we
don't preserve comments and such. We could instead do a more targeted
approach applying this override to projects which we know use this
setting 🤷‍♀️
  • Loading branch information
zanieb committed Jan 21, 2024
1 parent 49a445a commit a42600e
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions python/ruff-ecosystem/ruff_ecosystem/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def __post_init__(self):
)


ALWAYS_CONFIG_OVERRIDES = {
# Always unset the required version or we'll fail
"required-version": None
}


@dataclass(frozen=True)
class ConfigOverrides(Serializable):
"""
Expand Down Expand Up @@ -89,18 +95,23 @@ def patch_config(
"""
Temporarily patch the Ruff configuration file in the given directory.
"""
dot_ruff_toml = dirpath / ".ruff.toml"
ruff_toml = dirpath / "ruff.toml"
pyproject_toml = dirpath / "pyproject.toml"

# Prefer `ruff.toml` over `pyproject.toml`
if ruff_toml.exists():
if dot_ruff_toml.exists():
path = dot_ruff_toml
base = []
elif ruff_toml.exists():
path = ruff_toml
base = []
else:
path = pyproject_toml
base = ["tool", "ruff"]

overrides = {
**ALWAYS_CONFIG_OVERRIDES,
**self.always,
**(self.when_preview if preview else self.when_no_preview),
}
Expand All @@ -117,17 +128,30 @@ def patch_config(
contents = None
toml = {}

# Do not write a toml file if it does not exist and we're just nulling values
if all((value is None for value in overrides.values())):
yield
return

# Update the TOML, using `.` to descend into nested keys
for key, value in overrides.items():
logger.debug(f"Setting {key}={value!r} in {path}")
if value is not None:
logger.debug(f"Setting {key}={value!r} in {path}")
else:
logger.debug(f"Restoring {key} to default in {path}")

target = toml
names = base + key.split(".")
for name in names[:-1]:
if name not in target:
target[name] = {}
target = target[name]
target[names[-1]] = value

if value is None:
# Remove null values i.e. restore to default
target.pop(names[-1], None)
else:
target[names[-1]] = value

tomli_w.dump(toml, path.open("wb"))

Expand Down

0 comments on commit a42600e

Please sign in to comment.