Skip to content

Commit

Permalink
make allow-prereleases a tri-state setting to really forbid pre-rel…
Browse files Browse the repository at this point in the history
…eases if the setting is `false` and keep default behavior to allow pre-releases only if necessary
  • Loading branch information
radoering committed Nov 16, 2024
1 parent 5f6c309 commit 61ae21b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def create_dependency(
python_versions = constraint.get("python")
platform = constraint.get("platform")
markers = constraint.get("markers")
allows_prereleases = constraint.get("allow-prereleases", False)
allows_prereleases = constraint.get("allow-prereleases")

dependency: Dependency
if "git" in constraint:
Expand Down
11 changes: 9 additions & 2 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
constraint: str | VersionConstraint,
optional: bool = False,
groups: Iterable[str] | None = None,
allows_prereleases: bool = False,
allows_prereleases: bool | None = None,
extras: Iterable[str] | None = None,
source_type: str | None = None,
source_url: str | None = None,
Expand Down Expand Up @@ -233,7 +233,14 @@ def base_pep_508_name(self) -> str:
def base_pep_508_name_resolved(self) -> str:
return self.base_pep_508_name

def allows_prereleases(self) -> bool:
def allows_prereleases(self) -> bool | None:
"""
None (default): only use pre-release versions
if no stable version satisfies the constraint
False: do not allow pre-release versions
even if this means there is no solution
True: do not distinguish between stable and pre-release versions
"""
return self._allows_prereleases

def is_optional(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"^1.0.0-1",
],
)
@pytest.mark.parametrize("allows_prereleases", [False, True])
@pytest.mark.parametrize("allows_prereleases", [None, False, True])
def test_allows_prerelease(constraint: str, allows_prereleases: bool) -> None:
dependency = Dependency("A", constraint, allows_prereleases=allows_prereleases)
assert dependency.allows_prereleases() == allows_prereleases
Expand Down
7 changes: 6 additions & 1 deletion tests/packages/test_dependency_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create_dependency(
constraint: str = "*",
*,
extras: tuple[str, ...] = (),
allows_prereleases: bool = False,
allows_prereleases: bool | None = None,
develop: bool = False,
source_name: str | None = None,
marker: str | None = None,
Expand Down Expand Up @@ -147,6 +147,11 @@ def test_remove_dependency_removes_from_both_lists() -> None:
[create_dependency("foo", allows_prereleases=True)],
[create_dependency("foo", ">=1", allows_prereleases=True)],
),
(
[Dependency.create_from_pep_508("foo>=1")],
[create_dependency("foo", allows_prereleases=False)],
[create_dependency("foo", ">=1", allows_prereleases=False)],
),
# directory dependency - develop
(
[DirectoryDependency("foo", Path("path/to/foo"))],
Expand Down
18 changes: 17 additions & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_create_poetry(new_format: str) -> None:
requests = dependencies["requests"]
assert requests.pretty_constraint == (">=2.18,<3.0" if new_format else "^2.18")
assert not requests.is_vcs()
assert not requests.allows_prereleases()
assert requests.allows_prereleases() is None
assert requests.is_optional()
assert requests.extras == frozenset({"security"})

Expand Down Expand Up @@ -930,6 +930,22 @@ def test_create_dependency_marker_variants(
assert str(dep.marker) == exp_marker


@pytest.mark.parametrize(
("constraint", "expected"),
[
("1", None),
({"version": "1"}, None),
({"version": "1", "allow-prereleases": False}, False),
({"version": "1", "allow-prereleases": True}, True),
],
)
def test_create_dependency_allow_prereleases(
constraint: str | dict[str, str], expected: bool | None
) -> None:
dep = Factory.create_dependency("foo", constraint)
assert dep.allows_prereleases() is expected


def test_all_classifiers_unique_even_if_classifiers_is_duplicated() -> None:
poetry = Factory().create_poetry(
fixtures_dir / "project_with_duplicated_classifiers"
Expand Down

0 comments on commit 61ae21b

Please sign in to comment.