Choose between extras of the same dependency with Poetry
Asked Answered
C

1

6

I have a Python project managed with Poetry.

I want to install either psycopg[binary] or psycopg[c] via

$ poetry install -E pgbinary

or

$ poetry install -E pgc

These extras should be mutually exclusive, i.e.

$ poetry install -E pgbinary -E pgc

should raise an error.

This is what I wrote in the pyproject.toml:

[tool.poetry.dependencies]
python = "^3.9"
...
psycopg = { version = "^3.1.0", extras = ["binary", "c"],  optional = true}


[tool.poetry.extras]
pgbinary = ["psycopg[binary]"]
pgc = ["psycopg[c]"]

But when I do one of the following

$ poetry install
$ poetry install -E pgbinay
$ poetry install -E pgc

I always get this

The Poetry configuration is invalid:
  - data.extras.pgbinary[0] must match pattern ^[a-zA-Z-_.0-9]+$

The problem seems related to the square brackets in psycopg[binary] (or psycopg[c]).

Is there a way to choose which extra to install for a specific library during the poetry install?


UPDATE

I've solved with this for now

[tool.poetry.dependencies]
...
psycopg = {version = "^3.1.9", optional=true}
psycopg-binary = {version = "^3.1.9", optional=true}
psycopg-c = {version = "^3.1.9", optional=true}

[tool.poetry.extras]
pgbinary = ["psycopg", "psycopg-binary"]
pgc = ["psycopg", "psycopg-c"]

It works, but has stated here

You shouldn’t install this package directly: use instead

pip install "psycopg[c]"

to install a version of the optimization package matching the psycopg version installed.

If there is a way to write version = "^3.1.9" just once, it would be great.

Moreover, the extras are still not mutually exclusive.

Casabonne answered 12/6 at 9:43 Comment(4)
Post your solution as an answer, not as an extended update to the question!Edy
Why should psycopg[c] and psycopg[binary] be mutually exclusive? You can install both and then choose your favorite implementation on runtime.Edy
Mine is not an answer. I would like poetry install -E pgbinary -E pgc to fail. Also ["psycopg", "psycopg-binary"] is not the complete list of what psycopg[binary] installs, and I would not want to be the one to keep this list in syncCasabonne
IMHO that is an answer with a compromise but still an answer.Edy
H
1

What worked for me was using groups group as follows -

[tool.poetry.group.psycopgc.dependencies]
psycopg = { version = "^3.1.0", extras = ["c"],  optional = true}

[tool.poetry.group.psycopgb.dependencies]
psycopg = { version = "^3.1.0", extras = ["binary"],  optional = true}

omitting psycopg from the general dependencies of the project and installing it as so - poetry install --with psycopgc --without psycopgb

Of course its not optimal, but a good workaround in my opinion.

Harridan answered 12/8 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.