Skip to content

Commit

Permalink
prefer to avoid casts (#468)
Browse files Browse the repository at this point in the history
prefer to avoid `cast`.  If you're wrong, you never get to find out.

It turns out that some of these _were_ wrong, some tomlkit things are
apparently `Table` and not `Container`.
  • Loading branch information
dimbleby authored Sep 17, 2022
1 parent b2e2836 commit 743e09c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
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 poetry.core.utils.helpers import combine_unicode
Expand Down Expand Up @@ -56,8 +55,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
4 changes: 1 addition & 3 deletions src/poetry/core/packages/constraints/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.packages.constraints.base_constraint import BaseConstraint
from poetry.core.packages.constraints.constraint import Constraint
from poetry.core.packages.constraints.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
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.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.specification import PackageSpecification
Expand Down Expand Up @@ -482,38 +481,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
20 changes: 12 additions & 8 deletions src/poetry/core/pyproject/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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

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


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

@property
def poetry_config(self) -> Container:
def poetry_config(self) -> Table:
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, Table)
return config
except NonExistentKey as e:
from poetry.core.pyproject.exceptions import PyProjectException

Expand All @@ -92,15 +96,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, Table)

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
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
6 changes: 2 additions & 4 deletions tests/semver/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.semver.helpers import parse_constraint
Expand Down Expand Up @@ -415,7 +413,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

0 comments on commit 743e09c

Please sign in to comment.