Commit ddcf9489 by David Hotham Committed by Bjorn Neergaard

remove a lot of casts

Also avoid unnecessary imports when not typechecking
parent 63c86bf9
...@@ -14,7 +14,6 @@ from cleo.events.console_events import COMMAND ...@@ -14,7 +14,6 @@ from cleo.events.console_events import COMMAND
from cleo.events.event_dispatcher import EventDispatcher from cleo.events.event_dispatcher import EventDispatcher
from cleo.exceptions import CleoException from cleo.exceptions import CleoException
from cleo.formatters.style import Style from cleo.formatters.style import Style
from cleo.io.inputs.argv_input import ArgvInput
from cleo.io.null_io import NullIO from cleo.io.null_io import NullIO
from poetry.__version__ import __version__ from poetry.__version__ import __version__
...@@ -26,6 +25,7 @@ if TYPE_CHECKING: ...@@ -26,6 +25,7 @@ if TYPE_CHECKING:
from collections.abc import Callable from collections.abc import Callable
from cleo.events.console_command_event import ConsoleCommandEvent from cleo.events.console_command_event import ConsoleCommandEvent
from cleo.io.inputs.argv_input import ArgvInput
from cleo.io.inputs.definition import Definition from cleo.io.inputs.definition import Definition
from cleo.io.inputs.input import Input from cleo.io.inputs.input import Input
from cleo.io.io import IO from cleo.io.io import IO
...@@ -199,7 +199,7 @@ class Application(BaseApplication): # type: ignore[misc] ...@@ -199,7 +199,7 @@ class Application(BaseApplication): # type: ignore[misc]
if name == "run": if name == "run":
from poetry.console.io.inputs.run_argv_input import RunArgvInput from poetry.console.io.inputs.run_argv_input import RunArgvInput
input = cast(ArgvInput, io.input) input = cast("ArgvInput", io.input)
run_input = RunArgvInput([self._name or ""] + input._tokens) run_input = RunArgvInput([self._name or ""] + input._tokens)
# For the run command reset the definition # For the run command reset the definition
# with only the set options (i.e. the options given before the command) # with only the set options (i.e. the options given before the command)
...@@ -280,7 +280,7 @@ class Application(BaseApplication): # type: ignore[misc] ...@@ -280,7 +280,7 @@ class Application(BaseApplication): # type: ignore[misc]
) -> None: ) -> None:
from poetry.console.commands.env_command import EnvCommand from poetry.console.commands.env_command import EnvCommand
command: EnvCommand = cast(EnvCommand, event.command) command = event.command
if not isinstance(command, EnvCommand): if not isinstance(command, EnvCommand):
return return
...@@ -306,7 +306,7 @@ class Application(BaseApplication): # type: ignore[misc] ...@@ -306,7 +306,7 @@ class Application(BaseApplication): # type: ignore[misc]
) -> None: ) -> None:
from poetry.console.commands.installer_command import InstallerCommand from poetry.console.commands.installer_command import InstallerCommand
command: InstallerCommand = cast(InstallerCommand, event.command) command = event.command
if not isinstance(command, InstallerCommand): if not isinstance(command, InstallerCommand):
return return
......
...@@ -3,7 +3,6 @@ from __future__ import annotations ...@@ -3,7 +3,6 @@ from __future__ import annotations
import contextlib import contextlib
from typing import Any from typing import Any
from typing import cast
from cleo.helpers import argument from cleo.helpers import argument
from cleo.helpers import option from cleo.helpers import option
...@@ -250,7 +249,7 @@ The add command adds required packages to your <comment>pyproject.toml</> and in ...@@ -250,7 +249,7 @@ The add command adds required packages to your <comment>pyproject.toml</> and in
if self.option("lock"): if self.option("lock"):
self._installer.lock() self._installer.lock()
self._installer.whitelist([cast(str, r["name"]) for r in requirements]) self._installer.whitelist([r["name"] for r in requirements])
status = self._installer.run() status = self._installer.run()
......
from __future__ import annotations from __future__ import annotations
from typing import cast
from cleo.helpers import argument from cleo.helpers import argument
from cleo.helpers import option from cleo.helpers import option
from cleo.io.inputs.string_input import StringInput from cleo.io.inputs.string_input import StringInput
from cleo.io.io import IO from cleo.io.io import IO
from poetry.console.application import Application
from poetry.console.commands.init import InitCommand from poetry.console.commands.init import InitCommand
from poetry.console.commands.self.add import SelfAddCommand from poetry.console.commands.self.add import SelfAddCommand
...@@ -48,8 +45,8 @@ It works similarly to the <c1>add</c1> command: ...@@ -48,8 +45,8 @@ It works similarly to the <c1>add</c1> command:
def handle(self) -> int: def handle(self) -> int:
self.line_error(self.deprecation) self.line_error(self.deprecation)
application = cast(Application, self.application) application = self.get_application()
command: SelfAddCommand = cast(SelfAddCommand, application.find("self add")) command: SelfAddCommand = application.find("self add")
application.configure_installer_for_command(command, self.io) application.configure_installer_for_command(command, self.io)
argv: list[str] = ["add", *self.argument("plugins")] argv: list[str] = ["add", *self.argument("plugins")]
......
from __future__ import annotations from __future__ import annotations
from typing import cast from typing import TYPE_CHECKING
from cleo.helpers import argument from cleo.helpers import argument
from cleo.helpers import option from cleo.helpers import option
from cleo.io.inputs.string_input import StringInput from cleo.io.inputs.string_input import StringInput
from cleo.io.io import IO from cleo.io.io import IO
from poetry.console.application import Application
from poetry.console.commands.command import Command from poetry.console.commands.command import Command
from poetry.console.commands.self.remove import SelfRemoveCommand
if TYPE_CHECKING:
from poetry.console.commands.self.remove import SelfRemoveCommand
class PluginRemoveCommand(Command): class PluginRemoveCommand(Command):
...@@ -41,10 +43,8 @@ class PluginRemoveCommand(Command): ...@@ -41,10 +43,8 @@ class PluginRemoveCommand(Command):
def handle(self) -> int: def handle(self) -> int:
self.line_error(self.help) self.line_error(self.help)
application = cast(Application, self.application) application = self.get_application()
command: SelfRemoveCommand = cast( command: SelfRemoveCommand = application.find("self remove")
SelfRemoveCommand, application.find("self remove")
)
application.configure_installer_for_command(command, self.io) application.configure_installer_for_command(command, self.io)
argv: list[str] = ["remove", *self.argument("plugins")] argv: list[str] = ["remove", *self.argument("plugins")]
......
from __future__ import annotations from __future__ import annotations
from typing import cast from typing import TYPE_CHECKING
from cleo.io.inputs.string_input import StringInput from cleo.io.inputs.string_input import StringInput
from cleo.io.io import IO from cleo.io.io import IO
from poetry.console.application import Application
from poetry.console.commands.command import Command from poetry.console.commands.command import Command
from poetry.console.commands.self.show.plugins import SelfShowPluginsCommand
if TYPE_CHECKING:
from poetry.console.commands.self.show.plugins import SelfShowPluginsCommand
class PluginShowCommand(Command): class PluginShowCommand(Command):
...@@ -24,10 +26,8 @@ class PluginShowCommand(Command): ...@@ -24,10 +26,8 @@ class PluginShowCommand(Command):
def handle(self) -> int: def handle(self) -> int:
self.line_error(self.help) self.line_error(self.help)
application = cast(Application, self.application) application = self.get_application()
command: SelfShowPluginsCommand = cast( command: SelfShowPluginsCommand = application.find("self show plugins")
SelfShowPluginsCommand, application.find("self show plugins")
)
exit_code: int = command.run( exit_code: int = command.run(
IO( IO(
......
...@@ -2,7 +2,6 @@ from __future__ import annotations ...@@ -2,7 +2,6 @@ from __future__ import annotations
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import cast
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.project_package import ProjectPackage from poetry.core.packages.project_package import ProjectPackage
...@@ -11,13 +10,13 @@ from poetry.core.pyproject.toml import PyProjectTOML ...@@ -11,13 +10,13 @@ from poetry.core.pyproject.toml import PyProjectTOML
from poetry.__version__ import __version__ from poetry.__version__ import __version__
from poetry.console.commands.installer_command import InstallerCommand from poetry.console.commands.installer_command import InstallerCommand
from poetry.factory import Factory from poetry.factory import Factory
from poetry.poetry import Poetry
from poetry.utils.env import EnvManager from poetry.utils.env import EnvManager
from poetry.utils.env import SystemEnv from poetry.utils.env import SystemEnv
from poetry.utils.helpers import directory from poetry.utils.helpers import directory
if TYPE_CHECKING: if TYPE_CHECKING:
from poetry.poetry import Poetry
from poetry.utils.env import Env from poetry.utils.env import Env
...@@ -90,7 +89,9 @@ class SelfCommand(InstallerCommand): ...@@ -90,7 +89,9 @@ class SelfCommand(InstallerCommand):
def poetry(self) -> Poetry: def poetry(self) -> Poetry:
if self._poetry is None: if self._poetry is None:
self.reset_poetry() self.reset_poetry()
return cast(Poetry, self._poetry)
assert self._poetry is not None
return self._poetry
def _system_project_handle(self) -> int: def _system_project_handle(self) -> int:
""" """
...@@ -102,7 +103,8 @@ class SelfCommand(InstallerCommand): ...@@ -102,7 +103,8 @@ class SelfCommand(InstallerCommand):
The default implementations handles cases where a `self` command delegates The default implementations handles cases where a `self` command delegates
handling to an existing command. Eg: `SelfAddCommand(SelfCommand, AddCommand)`. handling to an existing command. Eg: `SelfAddCommand(SelfCommand, AddCommand)`.
""" """
return cast(int, super().handle()) return_code: int = super().handle()
return return_code
def reset(self) -> None: def reset(self) -> None:
""" """
......
from __future__ import annotations from __future__ import annotations
from typing import cast from typing import TYPE_CHECKING
from cleo.helpers import argument from cleo.helpers import argument
from cleo.helpers import option from cleo.helpers import option
from cleo.io.inputs.string_input import StringInput from cleo.io.inputs.string_input import StringInput
from cleo.io.io import IO from cleo.io.io import IO
from poetry.console.application import Application
from poetry.console.commands.add import AddCommand
from poetry.console.commands.self.self_command import SelfCommand from poetry.console.commands.self.self_command import SelfCommand
if TYPE_CHECKING:
from poetry.console.commands.add import AddCommand
class SelfUpdateCommand(SelfCommand): class SelfUpdateCommand(SelfCommand):
name = "self update" name = "self update"
description = "Updates Poetry to the latest version." description = "Updates Poetry to the latest version."
...@@ -37,8 +39,8 @@ environment. ...@@ -37,8 +39,8 @@ environment.
def _system_project_handle(self) -> int: def _system_project_handle(self) -> int:
self.write("<info>Updating Poetry version ...</info>\n\n") self.write("<info>Updating Poetry version ...</info>\n\n")
application = cast(Application, self.application) application = self.get_application()
add_command: AddCommand = cast(AddCommand, application.find("add")) add_command: AddCommand = application.find("add")
add_command.set_env(self.env) add_command.set_env(self.env)
application.configure_installer_for_command(add_command, self.io) application.configure_installer_for_command(add_command, self.io)
......
...@@ -14,7 +14,6 @@ from poetry.core.factory import Factory as BaseFactory ...@@ -14,7 +14,6 @@ from poetry.core.factory import Factory as BaseFactory
from poetry.core.packages.dependency_group import MAIN_GROUP from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.project_package import ProjectPackage from poetry.core.packages.project_package import ProjectPackage
from poetry.core.toml.file import TOMLFile from poetry.core.toml.file import TOMLFile
from tomlkit.toml_document import TOMLDocument
from poetry.config.config import Config from poetry.config.config import Config
from poetry.json import validate_object from poetry.json import validate_object
...@@ -29,6 +28,7 @@ if TYPE_CHECKING: ...@@ -29,6 +28,7 @@ if TYPE_CHECKING:
from cleo.io.io import IO from cleo.io.io import IO
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from tomlkit.toml_document import TOMLDocument
from poetry.repositories.legacy_repository import LegacyRepository from poetry.repositories.legacy_repository import LegacyRepository
from poetry.utils.dependency_specification import DependencySpec from poetry.utils.dependency_specification import DependencySpec
...@@ -293,7 +293,7 @@ class Factory(BaseFactory): ...@@ -293,7 +293,7 @@ class Factory(BaseFactory):
if extras_section: if extras_section:
content["extras"] = extras_section content["extras"] = extras_section
pyproject = cast(TOMLDocument, pyproject) pyproject = cast("TOMLDocument", pyproject)
pyproject.add(tomlkit.nl()) pyproject.add(tomlkit.nl())
if path: if path:
......
...@@ -11,7 +11,6 @@ import zipfile ...@@ -11,7 +11,6 @@ import zipfile
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Any from typing import Any
from typing import cast
import pkginfo import pkginfo
...@@ -171,12 +170,17 @@ class PackageInfo: ...@@ -171,12 +170,17 @@ class PackageInfo:
package.python_versions = self.requires_python or "*" package.python_versions = self.requires_python or "*"
package.files = self.files package.files = self.files
if root_dir or (self._source_type in {"directory"} and self._source_url): # If this is a local poetry project, we can extract "richer" requirement
# this is a local poetry project, this means we can extract "richer" # information, eg: development requirements etc.
# requirement information, eg: development requirements etc. if root_dir is not None:
poetry_package = self._get_poetry_package( path = root_dir
path=root_dir or Path(cast(str, self._source_url)) elif self._source_type == "directory" and self._source_url is not None:
) path = Path(self._source_url)
else:
path = None
if path is not None:
poetry_package = self._get_poetry_package(path=path)
if poetry_package: if poetry_package:
package.extras = poetry_package.extras package.extras = poetry_package.extras
for dependency in poetry_package.requires: for dependency in poetry_package.requires:
......
...@@ -13,11 +13,7 @@ from typing import Any ...@@ -13,11 +13,7 @@ from typing import Any
from typing import cast from typing import cast
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.semver.helpers import parse_constraint from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.version import Version from poetry.core.semver.version import Version
from poetry.core.toml.file import TOMLFile from poetry.core.toml.file import TOMLFile
...@@ -30,7 +26,6 @@ from tomlkit import item ...@@ -30,7 +26,6 @@ from tomlkit import item
from tomlkit import table from tomlkit import table
from tomlkit.exceptions import TOMLKitError from tomlkit.exceptions import TOMLKitError
from tomlkit.items import Array from tomlkit.items import Array
from tomlkit.items import Table
from poetry.packages import DependencyPackage from poetry.packages import DependencyPackage
from poetry.utils.extras import get_extra_package_names from poetry.utils.extras import get_extra_package_names
...@@ -41,7 +36,12 @@ if TYPE_CHECKING: ...@@ -41,7 +36,12 @@ if TYPE_CHECKING:
from collections.abc import Iterator from collections.abc import Iterator
from collections.abc import Sequence from collections.abc import Sequence
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.version.markers import BaseMarker from poetry.core.version.markers import BaseMarker
from tomlkit.items import Table
from tomlkit.toml_document import TOMLDocument from tomlkit.toml_document import TOMLDocument
from poetry.repositories import Repository from poetry.repositories import Repository
...@@ -455,7 +455,7 @@ class Locker: ...@@ -455,7 +455,7 @@ class Locker:
except TOMLKitError as e: except TOMLKitError as e:
raise RuntimeError(f"Unable to read the lock file ({e}).") raise RuntimeError(f"Unable to read the lock file ({e}).")
metadata = cast(Table, lock_data["metadata"]) metadata = cast("Table", lock_data["metadata"])
lock_version = Version.parse(metadata.get("lock-version", "1.0")) lock_version = Version.parse(metadata.get("lock-version", "1.0"))
current_version = Version.parse(self._VERSION) current_version = Version.parse(self._VERSION)
# We expect the locker to be able to read lock files # We expect the locker to be able to read lock files
...@@ -513,22 +513,22 @@ class Locker: ...@@ -513,22 +513,22 @@ class Locker:
constraint = inline_table() constraint = inline_table()
if dependency.is_directory(): if dependency.is_directory():
dependency = cast(DirectoryDependency, dependency) dependency = cast("DirectoryDependency", dependency)
constraint["path"] = dependency.path.as_posix() constraint["path"] = dependency.path.as_posix()
if dependency.develop: if dependency.develop:
constraint["develop"] = True constraint["develop"] = True
elif dependency.is_file(): elif dependency.is_file():
dependency = cast(FileDependency, dependency) dependency = cast("FileDependency", dependency)
constraint["path"] = dependency.path.as_posix() constraint["path"] = dependency.path.as_posix()
elif dependency.is_url(): elif dependency.is_url():
dependency = cast(URLDependency, dependency) dependency = cast("URLDependency", dependency)
constraint["url"] = dependency.url constraint["url"] = dependency.url
elif dependency.is_vcs(): elif dependency.is_vcs():
dependency = cast(VCSDependency, dependency) dependency = cast("VCSDependency", dependency)
constraint[dependency.vcs] = dependency.source constraint[dependency.vcs] = dependency.source
if dependency.branch: if dependency.branch:
......
...@@ -15,11 +15,7 @@ from typing import TYPE_CHECKING ...@@ -15,11 +15,7 @@ from typing import TYPE_CHECKING
from typing import cast from typing import cast
from cleo.ui.progress_indicator import ProgressIndicator from cleo.ui.progress_indicator import ProgressIndicator
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.utils.utils import get_python_constraint_from_marker from poetry.core.packages.utils.utils import get_python_constraint_from_marker
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.semver.empty_constraint import EmptyConstraint from poetry.core.semver.empty_constraint import EmptyConstraint
from poetry.core.semver.version import Version from poetry.core.semver.version import Version
from poetry.core.version.markers import AnyMarker from poetry.core.version.markers import AnyMarker
...@@ -47,8 +43,12 @@ if TYPE_CHECKING: ...@@ -47,8 +43,12 @@ if TYPE_CHECKING:
from cleo.io.io import IO from cleo.io.io import IO
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from poetry.core.packages.specification import PackageSpecification from poetry.core.packages.specification import PackageSpecification
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.semver.version_constraint import VersionConstraint from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.version.markers import BaseMarker from poetry.core.version.markers import BaseMarker
...@@ -227,19 +227,19 @@ class Provider: ...@@ -227,19 +227,19 @@ class Provider:
pass pass
elif dependency.is_vcs(): elif dependency.is_vcs():
dependency = cast(VCSDependency, dependency) dependency = cast("VCSDependency", dependency)
package = self._search_for_vcs(dependency) package = self._search_for_vcs(dependency)
elif dependency.is_file(): elif dependency.is_file():
dependency = cast(FileDependency, dependency) dependency = cast("FileDependency", dependency)
package = self._search_for_file(dependency) package = self._search_for_file(dependency)
elif dependency.is_directory(): elif dependency.is_directory():
dependency = cast(DirectoryDependency, dependency) dependency = cast("DirectoryDependency", dependency)
package = self._search_for_directory(dependency) package = self._search_for_directory(dependency)
elif dependency.is_url(): elif dependency.is_url():
dependency = cast(URLDependency, dependency) dependency = cast("URLDependency", dependency)
package = self._search_for_url(dependency) package = self._search_for_url(dependency)
else: else:
......
...@@ -14,13 +14,14 @@ from typing import Union ...@@ -14,13 +14,14 @@ from typing import Union
from typing import cast from typing import cast
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.vcs_dependency import VCSDependency
from tomlkit.items import InlineTable from tomlkit.items import InlineTable
from poetry.puzzle.provider import Provider from poetry.puzzle.provider import Provider
if TYPE_CHECKING: if TYPE_CHECKING:
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.utils.env import Env from poetry.utils.env import Env
...@@ -68,7 +69,8 @@ def _parse_dependency_specification_url( ...@@ -68,7 +69,8 @@ def _parse_dependency_specification_url(
if url_parsed.scheme in ["http", "https"]: if url_parsed.scheme in ["http", "https"]:
package = Provider.get_package_from_url(requirement) package = Provider.get_package_from_url(requirement)
return {"name": package.name, "url": cast(str, package.source_url)} assert package.source_url is not None
return {"name": package.name, "url": package.source_url}
return None return None
...@@ -153,14 +155,17 @@ def dependency_to_specification( ...@@ -153,14 +155,17 @@ def dependency_to_specification(
dependency: Dependency, specification: BaseSpec dependency: Dependency, specification: BaseSpec
) -> BaseSpec: ) -> BaseSpec:
if dependency.is_vcs(): if dependency.is_vcs():
dependency = cast(VCSDependency, dependency) dependency = cast("VCSDependency", dependency)
specification[dependency.vcs] = cast(str, dependency.source_url) assert dependency.source_url is not None
specification[dependency.vcs] = dependency.source_url
if dependency.reference: if dependency.reference:
specification["rev"] = dependency.reference specification["rev"] = dependency.reference
elif dependency.is_file() or dependency.is_directory(): elif dependency.is_file() or dependency.is_directory():
specification["path"] = cast(str, dependency.source_url) assert dependency.source_url is not None
specification["path"] = dependency.source_url
elif dependency.is_url(): elif dependency.is_url():
specification["url"] = cast(str, dependency.source_url) assert dependency.source_url is not None
specification["url"] = dependency.source_url
elif dependency.pretty_constraint != "*" and not dependency.constraint.is_empty(): elif dependency.pretty_constraint != "*" and not dependency.constraint.is_empty():
specification["version"] = dependency.pretty_constraint specification["version"] = dependency.pretty_constraint
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment