Skip to content

Commit

Permalink
Read the [project.optional-dependencies] and [dependency-groups]
Browse files Browse the repository at this point in the history
…tables (#66)

I'd quite like to use `ruff-action`'s auto-detection feature, but it
currently doesn't read the `[project.optional-dependency]` table, and
support for the PEP 735 `[dependency-groups]` table is currently limited
to only the special-cased `dev` key, and does not account for the
possibility of `{include-group="..."}` inline tables, as [described by
the PEP](https://peps.python.org/pep-0735/#dependency-group-include).

I have guessed how to make the tests work, as the only JS development I
do is plain JS (sans frameworks), so TypeScript is still quite new to me
-- feel free to push required changes to this branch.

A
  • Loading branch information
AA-Turner authored Jan 31, 2025
1 parent 47de3de commit f14634c
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 10 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,40 @@ jobs:
fi
env:
RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }}
test-default-version-from-pyproject-dependency-groups:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use default version from pyproject.toml dependency groups
id: ruff-action
uses: ./
with:
src: __tests__/fixtures/pyproject-dependency-groups-project
version-file: __tests__/fixtures/pyproject-dependency-groups-project/pyproject.toml
- name: Correct version gets installed
run: |
if [ "$RUFF_VERSION" != "0.8.3" ]; then
exit 1
fi
env:
RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }}
test-default-version-from-pyproject-optional-dependencies:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use default version from pyproject.toml optional dependencies
id: ruff-action
uses: ./
with:
src: __tests__/fixtures/pyproject-optional-dependencies-project
version-file: __tests__/fixtures/pyproject-optional-dependencies-project/pyproject.toml
- name: Correct version gets installed
run: |
if [ "$RUFF_VERSION" != "0.8.3" ]; then
exit 1
fi
env:
RUFF_VERSION: ${{ steps.ruff-action.outputs.ruff-version }}
test-semver-range:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ This action adds ruff to the PATH, so you can use it in subsequent steps.

By default this action looks for a pyproject.toml file in the root of the repository to determine
the ruff version to install. If no pyproject.toml file is found, or no ruff version is defined in
either `dependencies` or `dependency-groups.dev` the latest version is installed.
`project.dependencies`, `project.optional-dependencies`, or `dependency-groups`,
the latest version is installed.

#### Install the latest version

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[project]
name = "pyproject-dependency-groups-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"

[dependency-groups]
dev = [
{ include-group = "docs" },
{ include-group = "lint" },
]
docs = [
"sphinx",
]
lint = [
"ruff==0.8.3",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello() -> str:
return "Hello from python-project!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello world!")
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "pyproject-optional-dependencies-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"

[project.optional-dependencies]
lint = [
"ruff==0.8.3",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello() -> str:
return "Hello from python-project!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello world!")
9 changes: 6 additions & 3 deletions dist/ruff-action/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions src/utils/pyproject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export function getRuffVersionFromPyproject(
const pyprojectContent = fs.readFileSync(filePath, "utf-8");
let pyproject:
| {
project?: { dependencies?: string[] };
"dependency-groups"?: { dev?: string[] };
project?: {
dependencies?: string[];
"optional-dependencies"?: Map<string, string[]>;
};
"dependency-groups"?: Map<string, Array<string | object>>;
}
| undefined;
try {
Expand All @@ -25,11 +28,22 @@ export function getRuffVersionFromPyproject(
}

const dependencies: string[] = pyproject?.project?.dependencies || [];
const devDependencies: string[] = pyproject?.["dependency-groups"]?.dev || [];
const optionalDependencies: string[] = Object.values(
pyproject?.project?.["optional-dependencies"] || {},
).flat();
const devDependencies: string[] = Object.values(
pyproject?.["dependency-groups"] || {},
)
.flat()
.filter((item: string | object) => typeof item === "string");
const allDependencies: string[] = dependencies.concat(
optionalDependencies,
devDependencies,
);

const ruffVersionDefinition =
dependencies.find((dep: string) => dep.startsWith("ruff")) ||
devDependencies.find((dep: string) => dep.startsWith("ruff"));
const ruffVersionDefinition = allDependencies.find((dep: string) =>
dep.startsWith("ruff"),
);

if (ruffVersionDefinition) {
const ruffVersion = ruffVersionDefinition
Expand Down

0 comments on commit f14634c

Please sign in to comment.