Skip to content

Commit 0759db6

Browse files
dimblebyneersighted
authored andcommitted
reinstate "prefer to avoid casts (#468)"
but this time with more relaxed type assertions where appropriate
1 parent 775d8e3 commit 0759db6

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

src/poetry/core/constraints/generic/union_constraint.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
from typing import cast
4-
53
from poetry.core.constraints.generic.base_constraint import BaseConstraint
64
from poetry.core.constraints.generic.constraint import Constraint
75
from poetry.core.constraints.generic.empty_constraint import EmptyConstraint
@@ -99,7 +97,7 @@ def intersect(self, other: BaseConstraint) -> BaseConstraint:
9997
new_constraints.append(intersection)
10098

10199
else:
102-
other = cast(MultiConstraint, other)
100+
assert isinstance(other, MultiConstraint)
103101

104102
for our_constraint in self._constraints:
105103
intersection = our_constraint

src/poetry/core/factory.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from typing import List
1010
from typing import Mapping
1111
from typing import Union
12-
from typing import cast
1312
from warnings import warn
1413

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

6059
# Load package
61-
name = cast(str, local_config["name"])
62-
version = cast(str, local_config["version"])
60+
name = local_config["name"]
61+
assert isinstance(name, str)
62+
version = local_config["version"]
63+
assert isinstance(version, str)
6364
package = self.get_package(name, version)
6465
package = self.configure_package(
6566
package, local_config, poetry_file.parent, with_groups=with_groups

src/poetry/core/packages/package.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from typing import Iterable
1111
from typing import Iterator
1212
from typing import TypeVar
13-
from typing import cast
1413

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

484483
dep: Dependency
485484
if self.source_type == "directory":
485+
assert self._source_url is not None
486486
dep = DirectoryDependency(
487487
self._name,
488-
Path(cast(str, self._source_url)),
488+
Path(self._source_url),
489489
groups=list(self._dependency_groups.keys()),
490490
optional=self.optional,
491491
base=self.root_dir,
492492
develop=self.develop,
493493
extras=self.features,
494494
)
495495
elif self.source_type == "file":
496+
assert self._source_url is not None
496497
dep = FileDependency(
497498
self._name,
498-
Path(cast(str, self._source_url)),
499+
Path(self._source_url),
499500
groups=list(self._dependency_groups.keys()),
500501
optional=self.optional,
501502
base=self.root_dir,
502503
extras=self.features,
503504
)
504505
elif self.source_type == "url":
506+
assert self._source_url is not None
505507
dep = URLDependency(
506508
self._name,
507-
cast(str, self._source_url),
509+
self._source_url,
508510
directory=self.source_subdirectory,
509511
groups=list(self._dependency_groups.keys()),
510512
optional=self.optional,
511513
extras=self.features,
512514
)
513515
elif self.source_type == "git":
516+
assert self._source_url is not None
514517
dep = VCSDependency(
515518
self._name,
516519
self.source_type,
517-
cast(str, self.source_url),
520+
self._source_url,
518521
rev=self.source_reference,
519522
resolved_rev=self.source_resolved_reference,
520523
directory=self.source_subdirectory,

src/poetry/core/pyproject/toml.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from typing import TYPE_CHECKING
44
from typing import Any
5-
from typing import cast
65

7-
from tomlkit.container import Container
6+
from tomlkit.api import table
87

98

109
if TYPE_CHECKING:
@@ -64,11 +63,15 @@ def build_system(self) -> BuildSystem:
6463
return self._build_system
6564

6665
@property
67-
def poetry_config(self) -> Container:
66+
def poetry_config(self) -> dict[str, Any]:
6867
from tomlkit.exceptions import NonExistentKey
6968

7069
try:
71-
return cast(Container, self.data["tool"]["poetry"])
70+
tool = self.data["tool"]
71+
assert isinstance(tool, dict)
72+
config = tool["poetry"]
73+
assert isinstance(config, dict)
74+
return config
7275
except NonExistentKey as e:
7376
from poetry.core.pyproject.exceptions import PyProjectException
7477

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

9497
def save(self) -> None:
95-
from tomlkit.container import Container
96-
9798
data = self.data
9899

99100
if self._build_system is not None:
100101
if "build-system" not in data:
101-
data["build-system"] = Container()
102+
data["build-system"] = table()
103+
104+
build_system = data["build-system"]
105+
assert isinstance(build_system, dict)
102106

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

src/poetry/core/utils/toml_file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class TomlFile(TOMLFile):
99
@classmethod
10-
def __new__(cls: type[TOMLFile], *args: Any, **kwargs: Any) -> TOMLFile:
10+
def __new__(cls: type[TOMLFile], *args: Any, **kwargs: Any) -> TomlFile:
1111
import warnings
1212

1313
this_import = f"{cls.__module__}.{cls.__name__}"

tests/constraints/version/test_helpers.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
from typing import cast
4-
53
import pytest
64

75
from poetry.core.constraints.version import Version
@@ -448,7 +446,7 @@ def test_constraints_keep_version_precision(input: str, expected: str) -> None:
448446
],
449447
)
450448
def test_versions_are_sortable(unsorted: list[str], sorted_: list[str]) -> None:
451-
unsorted_parsed = [cast(Version, parse_constraint(u)) for u in unsorted]
452-
sorted_parsed = [cast(Version, parse_constraint(s)) for s in sorted_]
449+
unsorted_parsed = [Version.parse(u) for u in unsorted]
450+
sorted_parsed = [Version.parse(s) for s in sorted_]
453451

454452
assert sorted(unsorted_parsed) == sorted_parsed

tests/pyproject/test_pyproject_toml.py

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_pyproject_toml_reload(pyproject_toml: Path, poetry_section: str) -> Non
8383
name_new = str(uuid.uuid4())
8484

8585
pyproject.poetry_config["name"] = name_new
86+
assert isinstance(pyproject.poetry_config["name"], str)
8687
assert pyproject.poetry_config["name"] == name_new
8788

8889
pyproject.reload()
@@ -106,6 +107,7 @@ def test_pyproject_toml_save(
106107

107108
pyproject = PyProjectTOML(pyproject_toml)
108109

110+
assert isinstance(pyproject.poetry_config["name"], str)
109111
assert pyproject.poetry_config["name"] == name
110112
assert pyproject.build_system.build_backend == build_backend
111113
assert build_requires in pyproject.build_system.requires

0 commit comments

Comments
 (0)