Skip to content

Commit

Permalink
fix(python)!: Poetry python project is using deprecated `dev-dependen…
Browse files Browse the repository at this point in the history
…cies` notation in `pyproject.toml` (#3375)

[Poetry 1.2.0](https://python-poetry.org/blog/announcing-poetry-1.2.0/) introduced the `group` notation for managing dependencies, which has been the recommended way to organize dependencies inside your `pyproject.toml` file for almost two years. The older `dev-dependencies` notation was deprecated, as detailed [here](https://python-poetry.org/docs/master/managing-dependencies/), and its use is discouraged.

BREAKING CHANGE: Poetry is now required to be at version 1.2.0 or above. Please update Poetry if needed.

---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
garysassano committed Feb 24, 2024
1 parent 4d86588 commit a5f5672
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 28 deletions.
35 changes: 21 additions & 14 deletions src/python/poetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,23 +373,30 @@ export class PoetryPyproject extends Component {
constructor(project: Project, options: PoetryPyprojectOptions) {
super(project);

const decamelisedOptions = decamelizeKeysRecursively(options, {
separator: "-",
});

this.file = new TomlFile(project, "pyproject.toml", {
omitEmpty: false,
obj: {
"build-system": {
requires: ["poetry_core>=1.0.0"],
"build-backend": "poetry.core.masonry.api",
},
tool: {
poetry: {
...decamelisedOptions,
const { dependencies, devDependencies, ...otherOptions } = options;
const decamelizedOptions = decamelizeKeysRecursively(otherOptions);

const tomlStructure: any = {
tool: {
poetry: {
...decamelizedOptions,
dependencies: dependencies,
group: {
dev: {
dependencies: devDependencies,
},
},
},
},
"build-system": {
requires: ["poetry-core"],
"build-backend": "poetry.core.masonry.api",
},
};

this.file = new TomlFile(project, "pyproject.toml", {
omitEmpty: false,
obj: tomlStructure,
});
}
}
28 changes: 14 additions & 14 deletions test/python/__snapshots__/poetry.test.ts.snap

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

51 changes: 51 additions & 0 deletions test/python/poetry.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as TOML from "@iarna/toml";
import { python } from "../../src";
import * as logging from "../../src/logging";
import { synthSnapshot } from "../util";
Expand Down Expand Up @@ -218,3 +219,53 @@ class TestPythonProject extends python.PythonProject {
});
}
}

test("generates correct pyproject.toml content", () => {
const project = new TestPythonProject({
poetry: true,
homepage: "http://www.example.com",
description: "A short project description",
license: "Apache-2.0",
deps: ["aws-cdk-lib@^2.128.0"],
devDeps: ["black@^24.2.0", "flake8@^7.0.0"],
});

const snapshot = synthSnapshot(project);
const actualTomlContent = snapshot["pyproject.toml"];

const actualContentObject = TOML.parse(actualTomlContent);

const expectedContentObject = {
tool: {
poetry: {
name: "test-python-project",
version: "0.1.0",
description: "A short project description",
license: "Apache-2.0",
authors: ["First Last <email@example.com>"],
homepage: "http://www.example.com",
readme: "README.md",
dependencies: {
"aws-cdk-lib": "^2.128.0",
python: "^3.8",
},
group: {
dev: {
dependencies: {
black: "^24.2.0",
flake8: "^7.0.0",
projen: "99.99.99",
pytest: "7.4.3",
},
},
},
},
},
"build-system": {
requires: ["poetry-core"],
"build-backend": "poetry.core.masonry.api",
},
};

expect(actualContentObject).toEqual(expectedContentObject);
});

0 comments on commit a5f5672

Please sign in to comment.