feat: check requires-poetry before any validation#10593
Merged
finswimmer merged 1 commit intopython-poetry:mainfrom Oct 22, 2025
Merged
feat: check requires-poetry before any validation#10593finswimmer merged 1 commit intopython-poetry:mainfrom
finswimmer merged 1 commit intopython-poetry:mainfrom
Conversation
Reviewer's GuideThis PR centralizes the Sequence diagram for early requires-poetry version check in create_poetrysequenceDiagram
participant Caller
participant Factory
participant "_ensure_valid_poetry_version()"
participant "super().create_poetry()"
Caller->>Factory: create_poetry(cwd, with_groups, io)
Factory->>"_ensure_valid_poetry_version()": Check version constraint
"_ensure_valid_poetry_version()"-->>Factory: Raise error if not allowed
Factory->>"super().create_poetry()": Create Poetry object
"super().create_poetry()"-->>Factory: Poetry object
Factory-->>Caller: Poetry object
Class diagram for updated Factory class (requires-poetry check centralization)classDiagram
class Factory {
+_ensure_valid_poetry_version(cwd: Path | None) void
+create_poetry(cwd: Path | None = None, with_groups: bool = True, io: IO | None = None) Poetry
}
Factory --|> BaseFactory
class BaseFactory {
+create_poetry(cwd: Path | None = None, with_groups: bool = True) Poetry
}
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/poetry/factory.py:55-62` </location>
<code_context>
Factory class to create various elements needed by Poetry.
"""
+ def _ensure_valid_poetry_version(self, cwd: Path | None) -> None:
+ poetry_file = self.locate(cwd)
+ pyproject = PyProjectTOML(path=poetry_file)
+ poetry_config = pyproject.data.get("tool", {}).get("poetry", {})
+
+ if version_str := poetry_config.get("requires-poetry"):
+ version_constraint = parse_constraint(version_str)
+ version = Version.parse(__version__)
+ if not version_constraint.allows(version):
+ raise PoetryError(
+ f"This project requires Poetry {version_constraint},"
+ f" but you are using Poetry {version}"
+ )
+
def create_poetry(
</code_context>
<issue_to_address>
**suggestion:** Consider handling missing or malformed 'requires-poetry' values more robustly.
If 'requires-poetry' is malformed, parse_constraint may throw an uncaught exception. Consider catching parsing errors and raising a clear PoetryError, or validating the format before parsing.
```suggestion
if version_str := poetry_config.get("requires-poetry"):
if not isinstance(version_str, str):
raise PoetryError(
f"Invalid 'requires-poetry' value in pyproject.toml: expected a string, got {type(version_str).__name__}"
)
try:
version_constraint = parse_constraint(version_str)
except Exception as exc:
raise PoetryError(
f"Malformed 'requires-poetry' value in pyproject.toml: '{version_str}'. Error: {exc}"
) from exc
version = Version.parse(__version__)
if not version_constraint.allows(version):
raise PoetryError(
f"This project requires Poetry {version_constraint},"
f" but you are using Poetry {version}"
)
```
</issue_to_address>
### Comment 2
<location> `tests/test_factory.py:259-256` </location>
<code_context>
)
+def test_create_poetry_check_version_before_validation(
+ fixture_dir: FixtureDirGetter,
+) -> None:
+ with pytest.raises(PoetryError) as e:
+ Factory().create_poetry(fixture_dir("self_version_not_ok_invalid_config"))
+ assert (
+ str(e.value)
+ == f"This project requires Poetry <1.2, but you are using Poetry {__version__}"
+ )
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Consider adding a test for the case where 'requires-poetry' is missing or malformed.
Adding tests for missing, empty, or invalid 'requires-poetry' fields will improve error handling and test coverage for configuration edge cases.
Suggested implementation:
```python
def test_create_poetry_check_version_before_validation(
fixture_dir: FixtureDirGetter,
) -> None:
with pytest.raises(PoetryError) as e:
Factory().create_poetry(fixture_dir("self_version_not_ok_invalid_config"))
assert (
str(e.value)
== f"This project requires Poetry <1.2, but you are using Poetry {__version__}"
)
def test_create_poetry_missing_requires_poetry(
fixture_dir: FixtureDirGetter,
) -> None:
with pytest.raises(PoetryError) as e:
Factory().create_poetry(fixture_dir("missing_requires_poetry"))
assert "requires-poetry" in str(e.value)
def test_create_poetry_empty_requires_poetry(
fixture_dir: FixtureDirGetter,
) -> None:
with pytest.raises(PoetryError) as e:
Factory().create_poetry(fixture_dir("empty_requires_poetry"))
assert "requires-poetry" in str(e.value)
def test_create_poetry_invalid_requires_poetry(
fixture_dir: FixtureDirGetter,
) -> None:
with pytest.raises(PoetryError) as e:
Factory().create_poetry(fixture_dir("invalid_requires_poetry"))
assert "requires-poetry" in str(e.value)
```
You will need to add the corresponding fixture directories:
- `missing_requires_poetry`
- `empty_requires_poetry`
- `invalid_requires_poetry`
Each should contain a `pyproject.toml` file with the appropriate configuration for the test case:
- Missing: no `requires-poetry` field.
- Empty: `requires-poetry = ""`
- Invalid: `requires-poetry = "not_a_version_specifier"`
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
5981c2e to
5c20f15
Compare
neersighted
approved these changes
Oct 22, 2025
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Check List
Resolves: #10582
Summary by Sourcery
Ensure that the project’s requires-poetry constraint is validated immediately when creating a Poetry instance, raising an error early if the running Poetry version does not satisfy the requirement.
Enhancements:
Tests: