Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/poetry): fix schema for optional dependencies #32096

Merged
merged 8 commits into from
Oct 30, 2024
5 changes: 0 additions & 5 deletions lib/modules/manager/poetry/__fixtures__/pyproject.1.toml
Original file line number Diff line number Diff line change
@@ -11,11 +11,6 @@ dep2 = "^0.6.0"
dep3 = "^0.33.6"
python = "~2.7 || ^3.4"

[tool.poetry.extras]
extra_dep1 = "^0.8.3"
extra_dep2 = "^0.9.4"
extra_dep3 = "^0.4.0"

[tool.poetry.dev-dependencies]
dev_dep1 = "^3.0"
dev_dep2 = "Invalid version."
16 changes: 13 additions & 3 deletions lib/modules/manager/poetry/__fixtures__/pyproject.2.toml
Original file line number Diff line number Diff line change
@@ -10,11 +10,21 @@ dep2 = { version = "^0.6.0" }
dep3 = { path = "/some/path/", version = '^0.33.6' }
dep4 = { path = "/some/path/" }
dep5 = {}
extra_dep1 = {version = "^0.8.3", optional = true}
extra_dep2 = {version = "^0.9.4", optional = true}
extra_dep3 = {version = "^0.4.0", optional = true}

[tool.poetry.extras]
extra_dep1 = "^0.8.3"
extra_dep2 = "^0.9.4"
extra_dep3 = '^0.4.0'
extra1 = ["extra_dep1", "extra_dep2"]
extra2 = ["extra_dep3"]

[tool.poetry.dev-dependencies]
required_dev_dep = {version = "^3.0", optional = false}
optional_dev_dep = {version = "^3.0", optional = true} # NOTE: `optional = true` is meaningless here

[tool.poetry.group.group1.dependencies]
required_group_dep = {version = "^3.0", optional = false}
optional_group_dep = {version = "^3.0", optional = true} # NOTE: `optional = true` is meaningless here

[build-system]
requires = ["poetry>=1.0", "wheel"]
83 changes: 47 additions & 36 deletions lib/modules/manager/poetry/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -397,7 +397,7 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"depName": "extra_dep1",
"depType": "extras",
"managerData": {
"nestedVersion": false,
"nestedVersion": true,
},
"packageName": "extra-dep1",
"versioning": "poetry",
@@ -408,7 +408,7 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"depName": "extra_dep2",
"depType": "extras",
"managerData": {
"nestedVersion": false,
"nestedVersion": true,
},
"packageName": "extra-dep2",
"versioning": "poetry",
@@ -419,11 +419,55 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"depName": "extra_dep3",
"depType": "extras",
"managerData": {
"nestedVersion": false,
"nestedVersion": true,
},
"packageName": "extra-dep3",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "required_dev_dep",
"depType": "dev-dependencies",
"managerData": {
"nestedVersion": true,
},
"packageName": "required-dev-dep",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "optional_dev_dep",
"depType": "dev-dependencies",
"managerData": {
"nestedVersion": true,
},
"packageName": "optional-dev-dep",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "required_group_dep",
"depType": "group1",
"managerData": {
"nestedVersion": true,
},
"packageName": "required-group-dep",
"versioning": "poetry",
},
{
"currentValue": "^3.0",
"datasource": "pypi",
"depName": "optional_group_dep",
"depType": "group1",
"managerData": {
"nestedVersion": true,
},
"packageName": "optional-group-dep",
"versioning": "poetry",
},
],
"extractedConstraints": {},
"packageFileVersion": "0.1.0",
@@ -508,39 +552,6 @@ exports[`modules/manager/poetry/extract extractPackageFile() extracts multiple d
"packageName": "dev-dep2",
"skipReason": "invalid-version",
},
{
"currentValue": "^0.8.3",
"datasource": "pypi",
"depName": "extra_dep1",
"depType": "extras",
"managerData": {
"nestedVersion": false,
},
"packageName": "extra-dep1",
"versioning": "poetry",
},
{
"currentValue": "^0.9.4",
"datasource": "pypi",
"depName": "extra_dep2",
"depType": "extras",
"managerData": {
"nestedVersion": false,
},
"packageName": "extra-dep2",
"versioning": "poetry",
},
{
"currentValue": "^0.4.0",
"datasource": "pypi",
"depName": "extra_dep3",
"depType": "extras",
"managerData": {
"nestedVersion": false,
},
"packageName": "extra-dep3",
"versioning": "poetry",
},
]
`;

4 changes: 2 additions & 2 deletions lib/modules/manager/poetry/extract.spec.ts
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ describe('modules/manager/poetry/extract', () => {
it('extracts multiple dependencies', async () => {
const res = await extractPackageFile(pyproject1toml, filename);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(10);
expect(res?.deps).toHaveLength(7);
expect(res?.extractedConstraints).toEqual({
python: '~2.7 || ^3.4',
});
@@ -60,7 +60,7 @@ describe('modules/manager/poetry/extract', () => {
it('extracts multiple dependencies (with dep = {version = "1.2.3"} case)', async () => {
const res = await extractPackageFile(pyproject2toml, filename);
expect(res).toMatchSnapshot();
expect(res?.deps).toHaveLength(8);
expect(res?.deps).toHaveLength(12);
});

it('handles case with no dependencies', async () => {
27 changes: 20 additions & 7 deletions lib/modules/manager/poetry/schema.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,15 @@ import * as poetryVersioning from '../../versioning/poetry';
import { dependencyPattern } from '../pip_requirements/extract';
import type { PackageDependency, PackageFileContent } from '../types';

const PoetryOptionalDependencyMixin = z
.object({
optional: z.boolean().optional().catch(false),
})
.transform(
({ optional }): PackageDependency =>
optional ? { depType: 'extras' } : {},
);

const PoetryPathDependency = z
.object({
path: z.string(),
@@ -37,7 +46,8 @@ const PoetryPathDependency = z
}

return dep;
});
})
.and(PoetryOptionalDependencyMixin);

const PoetryGitDependency = z
.object({
@@ -88,7 +98,8 @@ const PoetryGitDependency = z
packageName: git,
skipReason: 'git-dependency',
};
});
})
.and(PoetryOptionalDependencyMixin);

const PoetryPypiDependency = z.union([
z
@@ -106,7 +117,8 @@ const PoetryPypiDependency = z.union([
},
currentValue,
};
}),
})
.and(PoetryOptionalDependencyMixin),
z.string().transform(
(version): PackageDependency => ({
datasource: PypiDatasource.id,
@@ -259,12 +271,15 @@ export const PoetrySources = LooseArray(PoetrySource, {
export const PoetrySectionSchema = z
.object({
version: z.string().optional().catch(undefined),
dependencies: withDepType(PoetryDependencies, 'dependencies').optional(),
dependencies: withDepType(
PoetryDependencies,
'dependencies',
false,
).optional(),
'dev-dependencies': withDepType(
PoetryDependencies,
'dev-dependencies',
).optional(),
extras: withDepType(PoetryDependencies, 'extras').optional(),
group: PoetryGroupDependencies.optional(),
source: PoetrySources,
})
@@ -273,14 +288,12 @@ export const PoetrySectionSchema = z
version,
dependencies = [],
'dev-dependencies': devDependencies = [],
extras: extraDependencies = [],
group: groupDependencies = [],
source: sourceUrls,
}) => {
const deps: PackageDependency[] = [
...dependencies,
...devDependencies,
...extraDependencies,
...groupDependencies,
];

6 changes: 4 additions & 2 deletions lib/util/schema-utils.ts
Original file line number Diff line number Diff line change
@@ -268,10 +268,12 @@ export const Toml = z.string().transform((str, ctx) => {
export function withDepType<
Output extends PackageDependency[],
Schema extends ZodType<Output, ZodTypeDef, unknown>,
>(schema: Schema, depType: string): ZodEffects<Schema> {
>(schema: Schema, depType: string, force: boolean = true): ZodEffects<Schema> {
return schema.transform((deps) => {
for (const dep of deps) {
dep.depType = depType;
if (!dep.depType || force) {
dep.depType = depType;
}
}
return deps;
});