Skip to content

[BUG] cyclonedx-py poetry ignores optional and extras on multi-constraint [package.dependencies] entries in poetry.lock #1151

Not planned

Description

@trackoor

Describe the bug

When poetry.lock contains a multi-constraint dependency entry (list[dict]) under [package.dependencies], cyclonedx-py poetry wraps the list as {'version': [...]} instead of normalizing it as a sequence of dependency specs (unlike root dependencies in pyproject.toml, which were fixed in #840 / #842).

Poetry writes a TOML array of inline tables under [package.dependencies] whenever a locked package has marker-split constraints on the same dependency (for example ray 2.42.1's py-spy optional serve dependency in tests/_data/infiles/poetry/regression-issue702/lock21/poetry.lock). Because dep_spec becomes {'version': [...]}, cyclonedx-py poetry:

  1. Ignores optional = true (dep_spec.get('optional') is None), adding uninstalled optional transitive packages present in poetry.lock to the SBOM and dependency graph even when the extra is not enabled.
  2. Drops extras = [...] (dep_spec.get('extras', ()) is ()), omitting transitive extra dependencies and cdx:python:package:required-extra properties from the SBOM.

To Reproduce

Case 1: Uninstalled optional = true multi-constraint dependency included

Create pyproject.toml where ray is used without extras in main and ray[serve] is only in dev:

[tool.poetry]
name = "demo"
version = "0.1.0"
description = ""
authors = ["Example <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.11"
ray = "2.42.1"

[tool.poetry.group.dev.dependencies]
ray = { version = "2.42.1", extras = ["serve"] }

Lock and generate the main-only SBOM:

poetry lock
cyclonedx-py poetry --only main --of JSON .

In poetry.lock, Poetry 2.x writes single-table optional = true entries for aiohttp, fastapi, prometheus-client, uvicorn, etc., and a two-entry list for py-spy (python_version < "3.12" and extra == "serve" / python_version >= "3.12" and extra == "serve", both with optional = true). Running cyclonedx-py poetry --only main excludes all single-table serve dependencies, but includes py-spy in components and in ray's dependencies.

Case 2: Transitive extras dropped on a multi-constraint dependency

Create pyproject.toml:

[tool.poetry]
name = "demo"
version = "0.1.0"
description = ""
authors = ["Example <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.11"
app-lib = "^1.0.0"

and poetry.lock:

[[package]]
name = "app-lib"
version = "1.0.0"
description = ""
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = []

[package.dependencies]
httpx = [
    {version = ">=0.26", extras = ["http2"], markers = "python_version < \"3.12\""},
    {version = ">=0.27", extras = ["http2"], markers = "python_version >= \"3.12\""},
]

[[package]]
name = "httpx"
version = "0.27.0"
description = "The next generation HTTP client."
optional = false
python-versions = ">=3.8"
groups = ["main"]
files = []

[package.extras]
http2 = ["h2 (>=4.0)"]

[[package]]
name = "h2"
version = "4.1.0"
description = "HTTP/2 State-Machine based protocol implementation"
optional = false
python-versions = ">=3.8"
groups = ["main"]
files = []

[metadata]
lock-version = "2.1"
python-versions = "^3.11"
content-hash = "0000000000000000000000000000000000000000000000000000000000000000"

Run:

cyclonedx-py poetry --of JSON .

Expected behavior

  • In Case 1, py-spy has optional = true on every entry in its constraint list and serve is not enabled in main, so py-spy should be excluded from the --only main SBOM just like single-table optional = true dependencies.
  • In Case 2, httpx's http2 extra should be propagated so [email protected] (and cdx:python:package:required-extra = "http2" on [email protected]) is included in the SBOM, matching single-table {version = ">=0.27", extras = ["http2"]} entries and root multi-constraint handling in _make_bom.

Screenshots or output-paste

  • In Case 1, the JSON BOM from cyclonedx-py poetry --only main --of JSON . includes py-spy (0.4.0) in components and in ray's dependencies, while excluding all single-table extra == "serve" optional packages.
  • In Case 2, the JSON BOM from cyclonedx-py poetry --of JSON . contains only app-lib (1.0.0) and httpx (0.27.0) in components, omitting h2 (4.1.0) and cdx:python:package:required-extra on httpx (_parse_lock stores the httpx dependency spec as {'version': [{'version': '>=0.26', 'markers': "python_version < '3.12'", 'extras': ['http2']}, {'version': '>=0.27', 'markers': "python_version >= '3.12'", 'extras': ['http2']}]}).

Environment

  • cyclonedx-py version: 9.0.0 (PyPI cyclonedx-bom==9.0.0) and main (087a93338905b6c3b4169cb8c35721b442c23ae4)
  • Python version: Python 3.11
  • OS: Linux (x86_64)

Additional context

In cyclonedx_py/_internal/poetry.py (lines 388-391), PoetryBB._parse_lock() builds each _LockEntry.dependencies mapping with:

dependencies={
    normalize_packagename(dn): ds if isinstance(ds, dict) else {'version': ds}
    for dn, ds in package.get('dependencies', {}).items()
},

When ds is a list[dict], isinstance(ds, dict) is False, so ds is wrapped as {'version': ds} instead of normalizing dict | list[dict] | str to list[dict] and checking all(d.get('optional') for d in dep_specs) and chain.from_iterable(d.get('extras', ()) for d in dep_specs) in __add_dep() (lines 337-350), as _make_bom() already does for pyproject.toml dependencies at lines 298-322.

Contribution

  • I am willing to provide a fix
  • I will wait until somebody else fixes it

Credit: https://ucsbplse.github.io/

Activity

  1. jkowalleck commented on Nov 26, 2026

    @jkowalleck
    Member

    sorry, description unclear.
    is this about poetry 1or poetry 2?

  2. added
    question
    on Nov 26, 2026
  3. trackoor commented on Nov 26, 2026

    @trackoor
    Author

    Sorry, should have said! It's Poetry 2 (lock-version 2.1). The regression-issue702/lock21 fixture in this repo has the same kind of entry, if that's easier to look at.

  4. jkowalleck commented on Nov 27, 2026

    @jkowalleck
    Member

    Sorry, should have said! It's Poetry 2 (lock-version 2.1).

    poetry2 is not supported, yet.
    see #839

    Closing as invalid/not planned

  5. jkowalleck commented on Nov 27, 2026

    @jkowalleck
    Member

    The regression-issue702/lock21 fixture in this repo has the same kind of entry, if that's easier to look at.

    if there is a bug with Poetry1, feel free to open a dedicated ticket for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionsource: poetry

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions