Skip to content

Commit efe2533

Browse files
authored
Upgrade from poetry 1.7 to poetry 2.3 (IMAP-Science-Operations-Center#3084)
* Upgrade from poetry 1.7 to poetry 2.3, including conforming to PEP 621. Updated all documentation to remove references to Poetry 1 and include more information for Poetry 2. Co-authored with Claude. * Add python version placeholder file for documentation generation * Updating docs, fixing python version * Downgrading packages to avoid test failures * Adding documentation on upgrading poetry
1 parent de57e30 commit efe2533

13 files changed

Lines changed: 437 additions & 158 deletions

File tree

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ jobs:
2828
uses: actions/setup-python@v4
2929
with:
3030
python-version: ${{ matrix.python-version }}
31-
- uses: Gr1N/setup-poetry@v8
31+
- uses: Gr1N/setup-poetry@v9
3232
with:
33-
poetry-version: "1.8.0"
33+
poetry-version: "2.3.4"
3434

3535

3636
- name: Install dependencies and app

.gitignore

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ data/imap/
44
*.xlsx
55
*.xls
66

7-
# poetry dynamic version creation
8-
_version.py
97

108
# Documentation ignore
119
docs/source/**/generated
@@ -37,6 +35,7 @@ share/python-wheels/
3735
.installed.cfg
3836
*.egg
3937
MANIFEST
38+
.python-version
4039

4140
# PyInstaller
4241
# Usually these files are written by a python script from a template
@@ -106,13 +105,6 @@ ipython_config.py
106105
# install all needed dependencies.
107106
#Pipfile.lock
108107

109-
# poetry
110-
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
111-
# This is especially recommended for binary packages to ensure reproducibility, and is more
112-
# commonly ignored for libraries.
113-
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
114-
#poetry.lock
115-
116108
# pdm
117109
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
118110
#pdm.lock

.pre-commit-config.yaml

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
ci:
22
autofix_prs: false
3-
autoupdate_schedule: 'quarterly'
3+
autoupdate_schedule: "quarterly"
44
skip: [poetry-lock]
55
repos:
66
- repo: https://github.com/pre-commit/pre-commit-hooks
77
rev: v6.0.0
88
hooks:
9-
- id: check-added-large-files
10-
args: ['--maxkb=1000']
11-
- id: detect-aws-credentials
12-
args: [--allow-missing-credentials]
13-
- id: detect-private-key
14-
- id: mixed-line-ending
15-
- id: trailing-whitespace
16-
exclude: ^imap_processing/tests/.*\.(dat|tf)$
17-
- id: no-commit-to-branch
18-
args: [--branch, main, --branch, dev]
9+
- id: check-added-large-files
10+
args: ["--maxkb=1000"]
11+
- id: detect-aws-credentials
12+
args: [--allow-missing-credentials]
13+
- id: detect-private-key
14+
- id: mixed-line-ending
15+
- id: trailing-whitespace
16+
exclude: ^imap_processing/tests/.*\.(dat|tf)$
17+
- id: no-commit-to-branch
18+
args: [--branch, main, --branch, dev]
1919
- repo: https://github.com/astral-sh/ruff-pre-commit
20-
rev: 'v0.13.3'
20+
rev: "v0.13.3"
2121
hooks:
22-
- id: ruff-check
23-
args: [--fix]
24-
- id: ruff-format
22+
- id: ruff-check
23+
args: [--fix]
24+
- id: ruff-format
2525
- repo: https://github.com/codespell-project/codespell
2626
rev: v2.4.1
2727
hooks:
2828
- id: codespell
2929
files: ^.*\.(py|md|rst|yml)$
3030
- repo: https://github.com/python-poetry/poetry
31-
rev: '1.8.0' # add version here
31+
rev: "2.3.4"
3232
hooks:
3333
- id: poetry-check
3434
- id: poetry-lock
35-
args: [--no-update]
3635
- repo: https://github.com/numpy/numpydoc
37-
rev: 'v1.9.0'
36+
rev: "v1.9.0"
3837
hooks:
3938
- id: numpydoc-validation
40-
exclude: '^imap_processing/tests/|.*test.*'
39+
exclude: "^imap_processing/tests/|.*test.*"
4140
- repo: https://github.com/pre-commit/mirrors-mypy
42-
rev: 'v1.18.2'
41+
rev: "v1.18.2"
4342
hooks:
4443
- id: mypy
4544
exclude: .*(tests|docs).*
46-
additional_dependencies: [ numpy==2.3.5 ]
45+
additional_dependencies: [numpy==2.3.5]

docs/source/development/getting-started.rst

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Installing requirements
99
Poetry Installation and Setup
1010
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1111

12-
``imap-processing`` uses :ref:`poetry-link` for dependency management. Check out our :ref:`style guide <poetry-environment>` for more information on specific IMAP Poetry usage.
12+
``imap-processing`` uses :ref:`poetry-link` for dependency management. Check out our :ref:`style guide <poetry-environment>` for more information on specific IMAP Poetry usage. If you are upgrading from Poetry 1.x, see :ref:`upgrading-poetry`.
1313

1414
If you're running locally, you can install the Python requirements with Poetry.
1515

1616
To setup versioning *(recommended for developers)*
1717

1818
.. code-block:: bash
1919
20-
poetry self add poetry-dynamic-versioning
20+
poetry self add "poetry-dynamic-versioning[plugin]"
2121
2222
To install without the extras
2323

@@ -31,19 +31,85 @@ To install all extras
3131
3232
poetry install --all-extras
3333
34-
This will install the dependencies from `poetry.lock`, ensuring that consistent versions are used. Poetry also provides a virtual environment, which you will have to activate.
34+
This will install the dependencies from `poetry.lock`, ensuring that consistent versions are used.
35+
Poetry manages a virtual environment for the project, which you will need to activate.
3536

36-
.. code-block:: bash
37+
.. note::
38+
39+
``poetry shell`` was removed in Poetry 2. Use one of the following to activate
40+
the virtual environment instead:
41+
42+
.. code-block:: bash
43+
44+
# Activate via Poetry (outputs the activation command)
45+
source $(poetry env info --path)/bin/activate
46+
47+
# Or on Windows (PowerShell)
48+
& (poetry env info --path)\Scripts\activate.ps1
49+
50+
Alternatively, you can use the ``venv`` module to create and manage your
51+
own virtual environment independently:
52+
53+
.. code-block:: bash
54+
55+
python3 -m venv .venv
56+
source .venv/bin/activate
57+
pip install -e ".[dev,test,tools]"
58+
59+
.. note::
60+
61+
Some dependencies include compiled extensions and require system libraries
62+
if no pre-built wheel is available for your platform. If installation fails
63+
with a PEP 517 build error, install the relevant system libraries below and
64+
then re-run the install command.
65+
66+
**netcdf4** requires the NetCDF-C and HDF5 libraries:
67+
68+
.. code-block:: bash
69+
70+
# Debian/Ubuntu
71+
sudo apt-get install -y libnetcdf-dev libhdf5-dev
3772
38-
poetry shell
73+
# macOS (Homebrew)
74+
brew install netcdf hdf5
3975
76+
**scipy** provides pre-built wheels for Python 3.10–3.13. If you are using
77+
Python 3.14 or later, no wheel is available yet and scipy must be compiled
78+
from source, which requires a Fortran compiler and BLAS/LAPACK. The
79+
simplest fix is to use Python 3.12 or 3.13.
80+
81+
If you need to build from source, install the following:
82+
83+
.. code-block:: bash
84+
85+
# Debian/Ubuntu
86+
sudo apt-get install -y gfortran libopenblas-dev pkg-config
87+
88+
# macOS (Homebrew)
89+
brew install openblas
90+
91+
On Apple Silicon (M1/M2/M3), Homebrew installs to ``/opt/homebrew`` rather
92+
than ``/usr/local``, so pkg-config cannot find OpenBLAS automatically. Set
93+
``PKG_CONFIG_PATH`` before running the install:
94+
95+
.. code-block:: bash
96+
97+
export PKG_CONFIG_PATH="/opt/homebrew/opt/openblas/lib/pkgconfig:$PKG_CONFIG_PATH"
98+
99+
If you do not need the ``test`` extras, you can avoid these system
100+
dependencies entirely by installing without them:
101+
102+
.. code-block:: bash
103+
104+
poetry install --extras "dev"
40105
41106
In summary, the expected setup of ``Poetry`` for a development environment is:
42107

43108
.. code-block:: bash
44109
45-
poetry self add poetry-dynamic-versioning
110+
poetry self add "poetry-dynamic-versioning[plugin]"
46111
poetry install --all-extras
112+
source $(poetry env info --path)/bin/activate
47113
48114
49115
Using IMAP processing
@@ -55,10 +121,5 @@ Our tests are run using pytest:
55121

56122
.. code-block:: bash
57123
58-
poetry shell
124+
source $(poetry env info --path)/bin/activate
59125
pytest
60-
61-
Related Information
62-
-------------------
63-
64-
For more information relating to infrastructure, see `sds-data-manager <https://sds-data-manager.readthedocs.io/en/latest/>`_.

docs/source/development/git-workflow-and-style-guide/poetry-environment.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ Poetry Environment
66
:ref:`Poetry <poetry-link>` is used for dependency management within this project. To update dependencies, you can either update
77
``pyproject.toml`` manually, or use ``poetry add <dependency>``.
88

9-
If you do add dependencies, please make sure you define the version numbers mindfully. The best way to do this is to use
10-
`caret notation <https://python-poetry.org/docs/dependency-specification/#caret-requirements>`_ to allow for minor
11-
version updates. For example, if you have tested the code using numpy version ``1.24.2``, the best way to specify the
12-
dependency is to allow for updates to the right most version number:
9+
If you do add dependencies, please make sure you define the version numbers mindfully. The required syntax depends on
10+
which section of ``pyproject.toml`` you are editing:
1311

14-
::
12+
* **``[project] dependencies``** (main runtime deps) and **``[project.optional-dependencies]``** (extras) use
13+
`PEP 508 <https://peps.python.org/pep-0508/>`_ specifiers. For example, if you have tested with numpy
14+
``1.24.2`` and want to allow patch and minor updates up to the next major version::
1515

16-
numpy = "^1.24"
16+
"numpy>=1.24,<2"
1717

18-
This will allow for patches for security reasons without upgrading to ``1.25`` or beyond. Use your best judgement for
19-
when packages can upgrade automatically, but try and avoid specifying only one specific version unless it is absolutely
20-
required.
18+
* **``[tool.poetry.group.*]``** sections (dev/test groups used only by Poetry) may continue to use
19+
`Poetry caret notation <https://python-poetry.org/docs/dependency-specification/#caret-requirements>`_::
20+
21+
numpy = "^1.24"
22+
23+
Both forms allow updates to the rightmost non-zero version component. Use your best judgement for when packages can
24+
upgrade automatically, but try to avoid pinning a single exact version unless it is absolutely required.
2125

2226
The ``poetry.lock`` file contains the existing dependencies for the project. These are the dependencies that you should
2327
install to ensure you're getting the accepted versions for every package. If the ``poetry.lock`` file is not up-to-date,

docs/source/development/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ be versioned appropriately to correspond with the code that produced them.
2323
git-workflow-and-style-guide/index
2424
poetry
2525
release-workflow
26+
upgrading-poetry
2627
technology-stack
2728
tools/index

docs/source/development/poetry.rst

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ These advantages ensure that we can all install the same versions of tools, that
2020

2121
Poetry also provides tools for automatically bumping dependency versions only where it makes sense. You can specify when a dependency should get a version change using Poetry's extensive `dependency specification <https://python-poetry.org/docs/dependency-specification/>`_ formatting.
2222

23-
Finally, Poetry provides a :ref:`shell <poetry-shell-link>`, which you can use as a virtual environment. This shell is automatically tied to the project directory, and allows developers to install dependency versions in an isolated environment. This means that, for example, if you have a different project using the same package with a different version, you can have each project with it's own version kept separate from conflicts.
23+
Finally, Poetry manages a virtual environment tied to the project directory, which allows developers to install dependency versions in an isolated environment. This means that, for example, if you have a different project using the same package with a different version, you can have each project with its own version kept separate from conflicts. See :ref:`poetry-shell-link` for how to activate it.
2424

2525
We have a :ref:`Poetry style guide<poetry-environment>` for specific recommendations about using Poetry in these projects.
2626

@@ -52,7 +52,7 @@ Poetry has a command to `add a new dependency <https://python-poetry.org/docs/ma
5252
# Add a new dependency with default latest version
5353
poetry add pendulum
5454

55-
These dependencies are then added automatically to the ``pyproject.toml`` file. The overall project dependencies go under ``[tool.poetry.dependencies]``. The main project dependencies are always installed.
55+
These dependencies are then added automatically to the ``pyproject.toml`` file. The main project dependencies go under the ``[project]`` table as a ``dependencies`` list, using `PEP 508 <https://peps.python.org/pep-0508/>`_ version specifiers (e.g. ``numpy>=1.24,<2``). Dependencies in ``[tool.poetry.group.*]`` sections continue to use Poetry's caret notation. The main project dependencies are always installed.
5656

5757
You can also update the ``pyproject.toml`` file directly, using the existing formatting or the `Poetry documentation on it <https://python-poetry.org/docs/pyproject/>`_ as a guide.
5858

@@ -63,52 +63,84 @@ This will create a new version of the ``poetry.lock`` file, which should be comm
6363

6464
.. _poetry-dependency-groups-link:
6565

66-
Dependency groups
67-
^^^^^^^^^^^^^^^^^^
66+
Optional extras
67+
^^^^^^^^^^^^^^^
6868

69-
Poetry also provides dependency groups for separating dependencies into logical separations. If you are installing the project as an end user, you do not need the development tools. The testing environment does not need the documentation generation dependencies. In our case, the AWS Lambda environment does not need the same dependencies as the CDK deployment. Before you add a dependency to the main group, ask yourself if it would make more sense in one of the other existing dependency groups.
69+
This project uses PEP 621 optional dependencies (extras) to separate dependencies into logical groups. If you are installing the project as an end user, you do not need the development tools. The testing environment does not need the documentation generation dependencies. Before you add a dependency to the main dependencies, ask yourself if it would make more sense in one of the existing extras (``dev``, ``test``, ``doc``, ``tools``, ``map_visualization``).
7070

71-
To add a dependency to an existing group, you can use the ``--group`` flag::
72-
73-
poetry add mkdocs --group docs
74-
75-
These groups can be made optional as well, meaning they will not be installed by default when the user runs ``poetry install``. You can specify what groups to install using the ``--with`` or ``--without`` flags.
76-
77-
Pip also provides a standard for optional dependencies. These can be installed when using ``pip`` instead of Poetry to install the dependencies. This goes under the ``[tool.poetry.extras]`` section in ``pyproject.toml``. These are separate, but similar to the optional dependencies. They can only be all installed or all not installed, with no splitting out into specific groups like the dependency groups.
71+
To add a dependency to an existing extra, update the ``[project.optional-dependencies]`` section in ``pyproject.toml`` directly using PEP 508 version specifiers. These extras can be installed selectively when running ``poetry install`` or ``pip install``.
7872

7973
.. _poetry-shell-link:
8074

81-
Installing and the Poetry Shell
82-
--------------------------------
75+
Installing and Activating the Virtual Environment
76+
-------------------------------------------------
8377

8478
To install the Poetry project, you can use the `install <https://python-poetry.org/docs/cli/#install>`_ command::
8579

8680
# We use dynamic versioning, which requires a plugin to be installed first
87-
poetry self add poetry-dynamic-versioning
81+
poetry self add "poetry-dynamic-versioning[plugin]"
8882

89-
# Install main dependencies and any dependency groups which are installed by default
83+
# Install main dependencies only
9084
poetry install
9185

92-
# Install all extras
86+
# Install all extras (dev, test, doc, tools, map_visualization)
9387
poetry install --all-extras
9488

95-
# install without specific dependency groups
96-
poetry install --without test,docs
89+
# Install with specific extras
90+
poetry install --extras "test doc"
91+
92+
By default, this command will install dependencies out of the ``poetry.lock`` file.
93+
94+
.. note::
95+
96+
``poetry shell`` was removed in Poetry 2. To activate the Poetry-managed
97+
virtual environment, use:
98+
99+
.. code-block:: bash
100+
101+
source $(poetry env info --path)/bin/activate
102+
103+
# To exit the virtual environment
104+
deactivate
97105
98-
# Install with optional dependency groups
99-
poetry install --with lambda_dev
106+
On Windows (PowerShell):
100107

101-
By default, this command will install dependencies out of the ``poetry.lock`` file. This will also install into your Poetry shell for the project.
108+
.. code-block:: powershell
102109
103-
The Poetry shell is a virtual environment tool provided by Poetry. To start the Poetry shell, with your dependencies installed, you can use the poetry `shell <https://python-poetry.org/docs/cli/#shell>`_ command::
110+
& (poetry env info --path)\Scripts\activate.ps1
104111
105-
poetry shell
112+
You can also run a single command inside the environment without activating
113+
it using ``poetry run``::
114+
115+
poetry run pytest
116+
117+
.. _venv-alternative-link:
118+
119+
Using a plain ``venv`` instead of Poetry's virtual environment
120+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121+
122+
If you prefer to manage your virtual environment with the standard Python
123+
``venv`` module rather than Poetry, you can do so by creating the environment
124+
first and then running ``poetry install`` (or ``pip install``) inside it.
125+
Poetry will detect the active virtual environment and install into it instead
126+
of creating its own:
127+
128+
.. code-block:: bash
129+
130+
# Create and activate a venv
131+
python3 -m venv .venv
132+
source .venv/bin/activate # Windows: .venv\Scripts\activate.ps1
133+
134+
# Install via Poetry (uses the active venv)
135+
poetry self add "poetry-dynamic-versioning[plugin]"
136+
poetry install --all-extras
106137
107-
# To exit the shell
108-
exit
138+
# Or install directly with pip using the pyproject.toml extras
139+
pip install -e ".[dev,test,tools]"
109140
110-
However, you are not required to use the Poetry shell as your virtual environment manager if you have another tool you prefer.
141+
The ``venv`` approach avoids the need for ``poetry shell`` entirely: your
142+
shell is already in the virtual environment after running ``source .venv/bin/activate``.
111143

112-
Poetry will, by default, not create a new virtual environment if it detects that it is running in a virtual environment already. So, for example, you can use a `Conda environment <https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html>`_ by activating the environment first, and then running `poetry install`.
144+
Poetry will, by default, not create a new virtual environment if it detects that it is running in a virtual environment already. So, for example, you can use a `Conda environment <https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html>`_ by activating the environment first, and then running ``poetry install``.
113145

114146
There are also `settings <https://python-poetry.org/docs/configuration/#virtualenvscreate>`_ surrounding the virtual environment that you can change to suit your workflow.

0 commit comments

Comments
 (0)