Skip to content

Commit

Permalink
fix: Add ability to override default python version for Poetry projec…
Browse files Browse the repository at this point in the history
…ts (projen#1028)

* The Python version for poetry projects is currently hard-coded to ^3.6
* This hard-coded Python version has a knock-on effect to the versions of other packages that are retrieved by poetry. Some projects require different versions of Python.
* Remove the auto-addition of the Python dependency in favour of specifying it explicitly in the 'deps' section of each project alongside other project dependencies e.g. `"python@^3.6,<3.9"`. This allows individual projects to specify the version(s) of python they use.


---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
lmarsden authored Sep 14, 2021
1 parent 4422a24 commit 6093315
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
17 changes: 17 additions & 0 deletions docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ converted to the appropriate dependency manager syntax in synthesized files. For
example, `lib^3.1.0` will be converted to `lib>=3.1.0, <4.0.0` in
`requirements.txt`.

### Poetry Dependencies

When using the `poetry` project type, note the following:

* Poetry requires the `python` version to be specified as a dependency e.g. `python@^3.7,<=3.9`.
When not specified, projen will set it to a default value of `python@^3.6`.
* The `[@version]` part of the dependency declaration must be present for all dependencies
* Dependencies that require additional metadata regarding extras etc. may be declared as
follows:

```text
deps: [
...
"mypackage@{version = '^3.3.3', extras = ['mypackage-extra']}",
],
```

## Unit Testing with Pytest

The `Pytest` component adds support for writing Python tests with
Expand Down
5 changes: 0 additions & 5 deletions src/__tests__/python/__snapshots__/poetry.test.ts.snap

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

20 changes: 20 additions & 0 deletions src/__tests__/python/poetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ test('poetry enabled', () => {
expect(snapshot['pyproject.toml']).toContain('a short project description');
expect(snapshot['pyproject.toml']).toContain('Apache-2.0');
expect(snapshot['pyproject.toml']).toContain('Development Status :: 4 - Beta');
expect(snapshot['pyproject.toml']).toContain('python = "^3.6"'); // default python version
});

test('poetry enabled with specified python version', () => {
const p = new TestPythonProject({
venv: false,
pip: false,
setuptools: false,
poetry: true,
homepage: 'http://www.example.com',
description: 'a short project description',
license: 'Apache-2.0',
classifiers: [
'Development Status :: 4 - Beta',
],
});
p.addDependency('python@^3.7,<=3.9');

const snapshot = synthSnapshot(p);
expect(snapshot['pyproject.toml']).toContain('python = "^3.7,<=3.9"');
});

test('poetry enabled with poetry-specific options', () => {
Expand Down
11 changes: 8 additions & 3 deletions src/python/poetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ export class Poetry extends Component implements IPythonDeps, IPythonEnv, IPytho
this.project.tasks.addEnvironment('VIRTUAL_ENV', '$(poetry env info -p)');
this.project.tasks.addEnvironment('PATH', '$(echo $(poetry env info -p)/bin:$PATH)');

// declare the python versions for which the package is compatible
this.addDependency('python@^3.6');

this.packageTask = project.addTask('package', {
description: 'Creates source archive and wheel for distribution.',
exec: 'poetry build',
Expand Down Expand Up @@ -78,11 +75,19 @@ export class Poetry extends Component implements IPythonDeps, IPythonEnv, IPytho

private synthDependencies() {
const dependencies: { [key: string]: any } = {};
let pythonDefined: boolean = false;
for (const pkg of this.project.deps.all) {
if (pkg.name === 'python') {
pythonDefined = true;
}
if (pkg.type === DependencyType.RUNTIME) {
dependencies[pkg.name] = pkg.version;
}
}
if (!pythonDefined) {
// Python version must be defined for poetry projects. Default to ^3.6.
dependencies.python = '^3.6';
}
return dependencies;
}

Expand Down

0 comments on commit 6093315

Please sign in to comment.