Skip to content

Commit

Permalink
feat(manager/pep621): support hatch environments (#25211)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
3 people committed Oct 16, 2023
1 parent d5508b7 commit 98dccff
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/modules/manager/pep621/__fixtures__/pyproject_with_hatch.toml
@@ -0,0 +1,29 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "hatch"
dependencies = [
"requests==2.30.0"
]

[tool.hatch.envs.default]
dependencies = [
"coverage[toml]==6.5",
"pytest",
]

[[tool.hatch.envs.all.matrix]]
python = ["3.7", "3.8", "3.9", "3.10", "3.11"]

[tool.hatch.envs.lint]
detached = true
dependencies = [
"black>=23.1.0",
]

[tool.hatch.envs.experimental]
extra-dependencies = [
"baz",
]
43 changes: 43 additions & 0 deletions lib/modules/manager/pep621/extract.spec.ts
Expand Up @@ -268,5 +268,48 @@ describe('modules/manager/pep621/extract', () => {
},
]);
});

it('should extract dependencies from hatch environments', function () {
const hatchPyProject = Fixtures.get('pyproject_with_hatch.toml');
const result = extractPackageFile(hatchPyProject, 'pyproject.toml');

expect(result?.deps).toEqual([
{
currentValue: '==2.30.0',
datasource: 'pypi',
depName: 'requests',
depType: 'project.dependencies',
packageName: 'requests',
},
{
currentValue: '==6.5',
datasource: 'pypi',
depName: 'coverage',
depType: 'tool.hatch.envs.default',
packageName: 'coverage',
},
{
datasource: 'pypi',
depName: 'pytest',
depType: 'tool.hatch.envs.default',
packageName: 'pytest',
skipReason: 'unspecified-version',
},
{
currentValue: '>=23.1.0',
datasource: 'pypi',
depName: 'black',
depType: 'tool.hatch.envs.lint',
packageName: 'black',
},
{
datasource: 'pypi',
depName: 'baz',
depType: 'tool.hatch.envs.experimental',
packageName: 'baz',
skipReason: 'unspecified-version',
},
]);
});
});
});
43 changes: 43 additions & 0 deletions lib/modules/manager/pep621/processors/hatch.ts
@@ -0,0 +1,43 @@
import is from '@sindresorhus/is';
import type {
PackageDependency,
UpdateArtifact,
UpdateArtifactsResult,
} from '../../types';
import type { PyProject } from '../schema';
import { parseDependencyList } from '../utils';
import type { PyProjectProcessor } from './types';

export class HatchProcessor implements PyProjectProcessor {
process(
pyproject: PyProject,
deps: PackageDependency[]
): PackageDependency[] {
const hatch_envs = pyproject.tool?.hatch?.envs;
if (is.nullOrUndefined(hatch_envs)) {
return deps;
}

for (const [envName, env] of Object.entries(hatch_envs)) {
const depType = `tool.hatch.envs.${envName}`;
const envDeps = parseDependencyList(depType, env?.dependencies);
deps.push(...envDeps);
const extraDeps = parseDependencyList(
depType,
env?.['extra-dependencies']
);
deps.push(...extraDeps);
}

return deps;
}

updateArtifacts(
updateArtifact: UpdateArtifact,
project: PyProject
): Promise<UpdateArtifactsResult[] | null> {
// Hatch does not have lock files at the moment
// https://github.com/pypa/hatch/issues/749
return Promise.resolve(null);
}
}
3 changes: 2 additions & 1 deletion lib/modules/manager/pep621/processors/index.ts
@@ -1,3 +1,4 @@
import { HatchProcessor } from './hatch';
import { PdmProcessor } from './pdm';

export const processors = [new PdmProcessor()];
export const processors = [new HatchProcessor(), new PdmProcessor()];
4 changes: 3 additions & 1 deletion lib/modules/manager/pep621/readme.md
Expand Up @@ -2,10 +2,12 @@ This manager supports updating dependencies inside `pyproject.toml` files.

In addition to standard dependencies, these toolsets are also supported:

- `pdm` ( including `pdm.lock` files )
- `pdm` (including `pdm.lock` files)
- `hatch`

Available `depType`s:

- `project.dependencies`
- `project.optional-dependencies`
- `tool.pdm.dev-dependencies`
- `tool.hatch.envs.<env-name>`
15 changes: 15 additions & 0 deletions lib/modules/manager/pep621/schema.ts
Expand Up @@ -31,6 +31,21 @@ export const PyProjectSchema = z.object({
.optional(),
})
.optional(),
hatch: z
.object({
envs: z
.record(
z.string(),
z
.object({
dependencies: DependencyListSchema,
'extra-dependencies': DependencyListSchema,
})
.optional()
)
.optional(),
})
.optional(),
})
.optional(),
});

0 comments on commit 98dccff

Please sign in to comment.