Fix Poetry lock optional dependency parsing - #5295
Conversation
Signed-off-by: Adnan Raza <imadnanraza4@gmail.com>
AyanSinhaMahapatra
left a comment
There was a problem hiding this comment.
Thanks @Mars-60 see comments for updating the tests, ready to merge otherwise.
| expected_loc = self.get_test_loc('pypi/poetry/univers-poetry.lock-expected.json') | ||
| self.check_packages_data(package, expected_loc, regen=REGEN_TEST_FIXTURES) | ||
|
|
||
| def test_parse_poetry_lock_package_optional(self): |
There was a problem hiding this comment.
Can you use an actual test file and expected results file like in the tests above?
| resolved_package = models.PackageData.from_data(package_data, package_only) | ||
|
|
||
| is_optional = package.get("is_optional") or True | ||
| is_optional = package.get("optional", False) |
| } | ||
|
|
||
| assert dependencies_by_purl['pkg:pypi/required-dep@1.0.0']['is_optional'] is False | ||
| assert dependencies_by_purl['pkg:pypi/optional-dep@2.0.0']['is_optional'] is True |
There was a problem hiding this comment.
No need to parse results and assert specifically, we run whole file results test with a test and an expected file, just check the expected behaviour and results as a whole
Signed-off-by: Adnan Raza <imadnanraza4@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
The fix correctly maps Poetry’s optional field to is_optional and is backed by updated fixtures plus a targeted regression test.
Pull request overview
Fixes PoetryLockHandler so resolved dependencies reflect Poetry’s optional = true/false flag instead of incorrectly defaulting to optional.
Changes:
- Update
PoetryLockHandler.parse()to map Poetry lockfileoptional→ ScanCodeis_optional. - Refresh Poetry fixture expected outputs and add a minimal lockfile fixture + test covering both optional and non-optional packages.
- Document the fix in
CHANGELOG.rstand add contributor toAUTHORS.rst.
File summaries
| File | Description |
|---|---|
src/packagedcode/pypi.py |
Fixes optional parsing by using package.get("optional", False) when creating DependentPackage entries. |
tests/packagedcode/test_pypi.py |
Adds a regression test validating optional vs. required packages in a Poetry lockfile. |
tests/packagedcode/data/pypi/poetry/univers-poetry.lock-expected.json |
Updates expected dependency is_optional values to match optional = false in the fixture lockfile. |
tests/packagedcode/data/pypi/poetry/univers-package-assembly-expected.json |
Updates assembled expected outputs to align with corrected is_optional behavior. |
tests/packagedcode/data/pypi/poetry/optional/poetry.lock |
Adds a minimal Poetry lockfile fixture containing both optional = false and optional = true packages. |
tests/packagedcode/data/pypi/poetry/optional-poetry.lock-expected.json |
Adds expected parsed output for the new optional/required fixture. |
CHANGELOG.rst |
Adds an entry for the Poetry optional dependency parsing fix (links issue #5294). |
AUTHORS.rst |
Adds the contributor. |
Review details
- Files reviewed: 7/8 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Updated the test to use an actual Poetry lockfile fixture and expected results file, as suggested. Also removed the explicit assertions and now uses check_packages_data() for the whole result. |
Fixes #5294
Tasks
Run tests locally to check for errors.
Description
PoetryLockHandlerincorrectly reported resolved Poetry lockfile packages as optional even when the package hadoptional = falseinpoetry.lock.The fix maps Poetry's
optionalfield to ScanCode'sis_optionalfield: