Skip to content

Commit

Permalink
non-package-mode: switch from mode ("package"/"non-package") to…
Browse files Browse the repository at this point in the history
… `package-mode` (`true`/`false`)
  • Loading branch information
radoering committed Dec 18, 2023
1 parent b132072 commit fb59dae
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,12 @@ def validate(
# which is quite vague, so we try to give a more precise error message
generic_error = "data cannot be validated by any definition"
if generic_error in validation_errors:
mode = config.get("mode", "package")
if mode not in {"package", "non-package"}:
package_mode = config.get("package-mode", True)
if not isinstance(package_mode, bool):
validation_errors[validation_errors.index(generic_error)] = (
f'Invalid mode "{mode}"'
' (allowed values are "package" and "non-package").'
f"Invalid value for package-mode: {package_mode}"
)
elif mode == "package":
elif package_mode:
required = {"name", "version", "description", "authors"}
if missing := required.difference(config):
validation_errors[validation_errors.index(generic_error)] = (
Expand Down
16 changes: 7 additions & 9 deletions src/poetry/core/json/schemas/poetry-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"anyOf": [
{
"required": [
"mode"
"package-mode"
],
"properties": {
"mode": {
"package-mode": {
"enum": [
"non-package"
false
]
}
}
Expand All @@ -26,12 +26,10 @@
}
],
"properties": {
"mode": {
"enum": [
"package",
"non-package"
],
"description": "Defines the operating mode of Poetry."
"package-mode": {
"type": "boolean",
"default": true,
"description": "Whether Poetry is operated in package mode or non-package mode."
},
"name": {
"type": "string",
Expand Down
6 changes: 3 additions & 3 deletions src/poetry/core/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def package(self) -> ProjectPackage:

@property
def is_package_mode(self) -> bool:
mode = self._local_config.get("mode", "package")
assert isinstance(mode, str)
return mode == "package"
package_mode = self._local_config["package-mode"]
assert isinstance(package_mode, bool)
return package_mode

@property
def local_config(self) -> dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/invalid_mode/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[tool.poetry]
mode = "invalid"
package-mode = "invalid"
2 changes: 1 addition & 1 deletion tests/fixtures/non_package_mode/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
mode = "non-package"
package-mode = false

[tool.poetry.dependencies]
python = "^3.8"
Expand Down
8 changes: 4 additions & 4 deletions tests/json/test_poetry_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_package_mode(
base_object: dict[str, Any], explicit: bool, missing_required: str
) -> None:
if explicit:
base_object["mode"] = "package"
base_object["package-mode"] = True
if missing_required:
del base_object[missing_required]
assert len(validate_object(base_object, "poetry-schema")) == 1
Expand All @@ -56,16 +56,16 @@ def test_package_mode(


def test_non_package_mode_no_metadata() -> None:
assert len(validate_object({"mode": "non-package"}, "poetry-schema")) == 0
assert len(validate_object({"package-mode": False}, "poetry-schema")) == 0


def test_non_package_mode_with_metadata(base_object: dict[str, Any]) -> None:
base_object["mode"] = "non-package"
base_object["package-mode"] = False
assert len(validate_object(base_object, "poetry-schema")) == 0


def test_invalid_mode() -> None:
assert len(validate_object({"mode": "foo"}, "poetry-schema")) == 1
assert len(validate_object({"package-mode": "foo"}, "poetry-schema")) == 1


def test_path_dependencies(base_object: dict[str, Any]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_create_poetry_fails_on_invalid_mode() -> None:

expected = """\
The Poetry configuration is invalid:
- Invalid mode "invalid" (allowed values are "package" and "non-package").
- Invalid value for package-mode: invalid
"""
assert str(e.value) == expected

Expand Down

0 comments on commit fb59dae

Please sign in to comment.