Skip to content

Commit

Permalink
reinstate "prefer to avoid casts (#468)"
Browse files Browse the repository at this point in the history
but this time with more relaxed type assertions where appropriate
  • Loading branch information
dimbleby authored and neersighted committed Oct 12, 2022
1 parent 775d8e3 commit 0759db6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/poetry/core/constraints/generic/union_constraint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from typing import cast

from poetry.core.constraints.generic.base_constraint import BaseConstraint
from poetry.core.constraints.generic.constraint import Constraint
from poetry.core.constraints.generic.empty_constraint import EmptyConstraint
Expand Down Expand Up @@ -99,7 +97,7 @@ def intersect(self, other: BaseConstraint) -> BaseConstraint:
new_constraints.append(intersection)

else:
other = cast(MultiConstraint, other)
assert isinstance(other, MultiConstraint)

for our_constraint in self._constraints:
intersection = our_constraint
Expand Down
7 changes: 4 additions & 3 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from typing import List
from typing import Mapping
from typing import Union
from typing import cast
from warnings import warn

from packaging.utils import canonicalize_name
Expand Down Expand Up @@ -58,8 +57,10 @@ def create_poetry(
raise RuntimeError("The Poetry configuration is invalid:\n" + message)

# Load package
name = cast(str, local_config["name"])
version = cast(str, local_config["version"])
name = local_config["name"]
assert isinstance(name, str)
version = local_config["version"]
assert isinstance(version, str)
package = self.get_package(name, version)
package = self.configure_package(
package, local_config, poetry_file.parent, with_groups=with_groups
Expand Down
13 changes: 8 additions & 5 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import Iterable
from typing import Iterator
from typing import TypeVar
from typing import cast

from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.dependency_group import MAIN_GROUP
Expand Down Expand Up @@ -483,38 +482,42 @@ def to_dependency(self) -> Dependency:

dep: Dependency
if self.source_type == "directory":
assert self._source_url is not None
dep = DirectoryDependency(
self._name,
Path(cast(str, self._source_url)),
Path(self._source_url),
groups=list(self._dependency_groups.keys()),
optional=self.optional,
base=self.root_dir,
develop=self.develop,
extras=self.features,
)
elif self.source_type == "file":
assert self._source_url is not None
dep = FileDependency(
self._name,
Path(cast(str, self._source_url)),
Path(self._source_url),
groups=list(self._dependency_groups.keys()),
optional=self.optional,
base=self.root_dir,
extras=self.features,
)
elif self.source_type == "url":
assert self._source_url is not None
dep = URLDependency(
self._name,
cast(str, self._source_url),
self._source_url,
directory=self.source_subdirectory,
groups=list(self._dependency_groups.keys()),
optional=self.optional,
extras=self.features,
)
elif self.source_type == "git":
assert self._source_url is not None
dep = VCSDependency(
self._name,
self.source_type,
cast(str, self.source_url),
self._source_url,
rev=self.source_reference,
resolved_rev=self.source_resolved_reference,
directory=self.source_subdirectory,
Expand Down
19 changes: 11 additions & 8 deletions src/poetry/core/pyproject/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from typing import TYPE_CHECKING
from typing import Any
from typing import cast

from tomlkit.container import Container
from tomlkit.api import table


if TYPE_CHECKING:
Expand Down Expand Up @@ -64,11 +63,15 @@ def build_system(self) -> BuildSystem:
return self._build_system

@property
def poetry_config(self) -> Container:
def poetry_config(self) -> dict[str, Any]:
from tomlkit.exceptions import NonExistentKey

try:
return cast(Container, self.data["tool"]["poetry"])
tool = self.data["tool"]
assert isinstance(tool, dict)
config = tool["poetry"]
assert isinstance(config, dict)
return config
except NonExistentKey as e:
from poetry.core.pyproject.exceptions import PyProjectException

Expand All @@ -92,15 +95,15 @@ def __getattr__(self, item: str) -> Any:
return getattr(self.data, item)

def save(self) -> None:
from tomlkit.container import Container

data = self.data

if self._build_system is not None:
if "build-system" not in data:
data["build-system"] = Container()
data["build-system"] = table()

build_system = data["build-system"]
assert isinstance(build_system, dict)

build_system = cast(Container, data["build-system"])
build_system["requires"] = self._build_system.requires
build_system["build-backend"] = self._build_system.build_backend

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/utils/toml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TomlFile(TOMLFile):
@classmethod
def __new__(cls: type[TOMLFile], *args: Any, **kwargs: Any) -> TOMLFile:
def __new__(cls: type[TOMLFile], *args: Any, **kwargs: Any) -> TomlFile:
import warnings

this_import = f"{cls.__module__}.{cls.__name__}"
Expand Down
6 changes: 2 additions & 4 deletions tests/constraints/version/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from typing import cast

import pytest

from poetry.core.constraints.version import Version
Expand Down Expand Up @@ -448,7 +446,7 @@ def test_constraints_keep_version_precision(input: str, expected: str) -> None:
],
)
def test_versions_are_sortable(unsorted: list[str], sorted_: list[str]) -> None:
unsorted_parsed = [cast(Version, parse_constraint(u)) for u in unsorted]
sorted_parsed = [cast(Version, parse_constraint(s)) for s in sorted_]
unsorted_parsed = [Version.parse(u) for u in unsorted]
sorted_parsed = [Version.parse(s) for s in sorted_]

assert sorted(unsorted_parsed) == sorted_parsed
2 changes: 2 additions & 0 deletions tests/pyproject/test_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def test_pyproject_toml_reload(pyproject_toml: Path, poetry_section: str) -> Non
name_new = str(uuid.uuid4())

pyproject.poetry_config["name"] = name_new
assert isinstance(pyproject.poetry_config["name"], str)
assert pyproject.poetry_config["name"] == name_new

pyproject.reload()
Expand All @@ -106,6 +107,7 @@ def test_pyproject_toml_save(

pyproject = PyProjectTOML(pyproject_toml)

assert isinstance(pyproject.poetry_config["name"], str)
assert pyproject.poetry_config["name"] == name
assert pyproject.build_system.build_backend == build_backend
assert build_requires in pyproject.build_system.requires

0 comments on commit 0759db6

Please sign in to comment.