Skip to content

Conversation

@manavgup
Copy link
Contributor

Addresses #915

Description

This PR fixes a bug where an AttributeError was raised when parsing a pyproject.toml file with a license field specified as a string (per PEP 639), rather than as a table (per PEP 621). The bug occurred because the code assumed the license field was always a dict and attempted to call .get() on a string.

Key Changes

  • Enhanced project2licenses in pep621.py:
  • Now correctly handles both string and dict types for the license field in pyproject.toml.
  • If the license is a string (PEP 639), it is treated as a license expression or reference.
  • If the license is a dict (PEP 621), it is handled as before (supporting text and file keys).
  • Added a type check and error for unexpected types.

Added Unit Tests:

Created tests/unit/test_pep621.py to directly test project2licenses for:

  • license as a string (PEP 639)
  • license as a dict with text (PEP 621)
  • license as a dict with file (PEP 621)

Tests cover correct parsing and object creation for all supported license formats.

Test Robustness:

Tests are robust to the behavior of the license factory for unknown license references, checking both id and text fields.

Code Style:

  • All code follows project style guidelines (PEP8, sorted imports, f-strings, single quotes, lower_snake_case).

Motivation and Context

  • Fixes an AttributeError when using a PEP 639 license string in pyproject.toml.
  • Ensures compliance with the latest packaging standards and improves compatibility with modern Python projects.
  • Adds direct unit tests for license parsing logic, increasing test coverage and reliability.

Checklist

[x] Code style and imports are clean (pyupgrade, isort, autopep8).
[x] All new and existing tests pass.
[x] Commits are signed off (git commit -s).

Signed-off-by: Manav Gupta <manavg@gmail.com>
@manavgup manavgup requested a review from a team as a code owner June 23, 2025 17:06
Signed-off-by: Manav Gupta <manavg@gmail.com>
@jkowalleck jkowalleck changed the title Fix: update pep621 logic and add unit tests fix: made pep621 type-aware Jun 24, 2025
@jkowalleck jkowalleck changed the title fix: made pep621 type-aware fix: make pep621 type-aware Jun 24, 2025
- Simplified license handling to only process dict (PEP 621) values.
- Silently skip string/other types.
- Combined assignment and type check for clarity.

Signed-off-by: Manav Gupta <manavg@gmail.com>
…ion/test_utils_pep621.py, and improved test coverage

Signed-off-by: Manav Gupta <manavg@gmail.com>
@jkowalleck jkowalleck changed the title fix: make pep621 type-aware fix: make pep621licensedetections type-aware Jun 25, 2025
@jkowalleck jkowalleck changed the title fix: make pep621licensedetections type-aware fix: make pep621license detections type-aware Jun 25, 2025
manavgup and others added 2 commits June 25, 2025 11:55
Signed-off-by: Manav Gupta <manavg@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck jkowalleck self-requested a review June 25, 2025 16:01
@jkowalleck
Copy link
Member

jkowalleck commented Jun 25, 2025

some CI/CT is failing:

manavgup and others added 3 commits June 26, 2025 09:18
Signed-off-by: Manav Gupta <manavg@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck jkowalleck changed the title fix: make pep621license detections type-aware fix: make pep621 license detections type-aware Jun 26, 2025
@jkowalleck
Copy link
Member

fixed the still open points in the tests.

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck jkowalleck merged commit 0c9aeac into CycloneDX:main Jun 26, 2025
30 checks passed
@jkowalleck
Copy link
Member

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants