Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "prefer to avoid casts (#468)" #492

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/poetry/core/constraints/generic/union_constraint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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 @@ -97,7 +99,7 @@ def intersect(self, other: BaseConstraint) -> BaseConstraint:
new_constraints.append(intersection)

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

for our_constraint in self._constraints:
intersection = our_constraint
Expand Down
7 changes: 3 additions & 4 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -57,10 +58,8 @@ def create_poetry(
raise RuntimeError("The Poetry configuration is invalid:\n" + message)

# Load package
name = local_config["name"]
assert isinstance(name, str)
version = local_config["version"]
assert isinstance(version, str)
name = cast(str, local_config["name"])
version = cast(str, local_config["version"])
package = self.get_package(name, version)
package = self.configure_package(
package, local_config, poetry_file.parent, with_groups=with_groups
Expand Down
13 changes: 5 additions & 8 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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 @@ -482,42 +483,38 @@ def to_dependency(self) -> Dependency:

dep: Dependency
if self.source_type == "directory":
assert self._source_url is not None
dep = DirectoryDependency(
self._name,
Path(self._source_url),
Path(cast(str, 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(self._source_url),
Path(cast(str, 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,
self._source_url,
cast(str, 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,
self._source_url,
cast(str, self.source_url),
rev=self.source_reference,
resolved_rev=self.source_resolved_reference,
directory=self.source_subdirectory,
Expand Down
20 changes: 8 additions & 12 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.api import table
from tomlkit.items import Table
from tomlkit.container import Container


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

@property
def poetry_config(self) -> Table:
def poetry_config(self) -> Container:
from tomlkit.exceptions import NonExistentKey

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

Expand All @@ -96,15 +92,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"] = table()

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

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: 4 additions & 2 deletions tests/constraints/version/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import cast

import pytest

from poetry.core.constraints.version import Version
Expand Down Expand Up @@ -446,7 +448,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 = [Version.parse(u) for u in unsorted]
sorted_parsed = [Version.parse(s) for s in sorted_]
unsorted_parsed = [cast(Version, parse_constraint(u)) for u in unsorted]
sorted_parsed = [cast(Version, parse_constraint(s)) for s in sorted_]

assert sorted(unsorted_parsed) == sorted_parsed
2 changes: 0 additions & 2 deletions tests/pyproject/test_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ 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 @@ -107,7 +106,6 @@ 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