Skip to content

Commit

Permalink
Cleo 2.0 (#7070)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored Nov 22, 2022
1 parent ba97fea commit 14b7f1e
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 79 deletions.
134 changes: 112 additions & 22 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ poetry-core = "^1.3.2"
poetry-plugin-export = "^1.2.0"
"backports.cached-property" = { version = "^1.0.2", python = "<3.8" }
cachecontrol = { version = "^0.12.9", extras = ["filecache"] }
cleo = "^1.0.0a5"
crashtest = "^0.3.0"
cleo = "^2.0.0"
crashtest = "^0.4.1"
dulwich = "^0.20.46"
filelock = "^3.8.0"
html5lib = "^1.0"
Expand Down Expand Up @@ -168,9 +168,6 @@ warn_unused_ignores = false
[[tool.mypy.overrides]]
module = [
'cachecontrol.*',
'cachy.*',
'cleo.*',
'crashtest.*',
'lockfile.*',
'pexpect.*',
'pkginfo.*',
Expand Down
27 changes: 14 additions & 13 deletions src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from contextlib import suppress
from importlib import import_module
from typing import TYPE_CHECKING
from typing import Any
from typing import cast

from cleo.application import Application as BaseApplication
from cleo.events.console_command_event import ConsoleCommandEvent
from cleo.events.console_events import COMMAND
from cleo.events.event_dispatcher import EventDispatcher
from cleo.exceptions import CleoException
from cleo.exceptions import CleoError
from cleo.formatters.style import Style
from cleo.io.null_io import NullIO

Expand All @@ -24,7 +24,7 @@
if TYPE_CHECKING:
from collections.abc import Callable

from cleo.events.console_command_event import ConsoleCommandEvent
from cleo.events.event import Event
from cleo.io.inputs.argv_input import ArgvInput
from cleo.io.inputs.definition import Definition
from cleo.io.inputs.input import Input
Expand Down Expand Up @@ -93,7 +93,7 @@ def _load() -> Command:
]


class Application(BaseApplication): # type: ignore[misc]
class Application(BaseApplication):
def __init__(self) -> None:
super().__init__("poetry", __version__)

Expand Down Expand Up @@ -137,8 +137,8 @@ def poetry(self) -> Poetry:

@property
def command_loader(self) -> CommandLoader:
command_loader: CommandLoader | None = self._command_loader
assert command_loader is not None
command_loader = self._command_loader
assert isinstance(command_loader, CommandLoader)
return command_loader

def reset_poetry(self) -> None:
Expand Down Expand Up @@ -194,7 +194,7 @@ def _configure_io(self, io: IO) -> None:
# We need to check if the command being run
# is the "run" command.
definition = self.definition
with suppress(CleoException):
with suppress(CleoError):
io.input.bind(definition)

name = io.input.first_argument
Expand All @@ -215,7 +215,7 @@ def _configure_io(self, io: IO) -> None:
for shortcut in shortcuts:
run_input.add_parameter_option("-" + shortcut.lstrip("-"))

with suppress(CleoException):
with suppress(CleoError):
run_input.bind(definition)

for option_name, value in input.options.items():
Expand All @@ -227,12 +227,13 @@ def _configure_io(self, io: IO) -> None:
super()._configure_io(io)

def register_command_loggers(
self, event: ConsoleCommandEvent, event_name: str, _: Any
self, event: Event, event_name: str, _: EventDispatcher
) -> None:
from poetry.console.logging.filters import POETRY_FILTER
from poetry.console.logging.io_formatter import IOFormatter
from poetry.console.logging.io_handler import IOHandler

assert isinstance(event, ConsoleCommandEvent)
command = event.command
if not isinstance(command, Command):
return
Expand Down Expand Up @@ -277,12 +278,11 @@ def register_command_loggers(

logger.setLevel(_level)

def configure_env(
self, event: ConsoleCommandEvent, event_name: str, _: Any
) -> None:
def configure_env(self, event: Event, event_name: str, _: EventDispatcher) -> None:
from poetry.console.commands.env_command import EnvCommand
from poetry.console.commands.self.self_command import SelfCommand

assert isinstance(event, ConsoleCommandEvent)
command = event.command
if not isinstance(command, EnvCommand) or isinstance(command, SelfCommand):
return
Expand All @@ -305,10 +305,11 @@ def configure_env(

@classmethod
def configure_installer_for_event(
cls, event: ConsoleCommandEvent, event_name: str, _: Any
cls, event: Event, event_name: str, _: EventDispatcher
) -> None:
from poetry.console.commands.installer_command import InstallerCommand

assert isinstance(event, ConsoleCommandEvent)
command = event.command
if not isinstance(command, InstallerCommand):
return
Expand Down
6 changes: 3 additions & 3 deletions src/poetry/console/command_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import TYPE_CHECKING

from cleo.exceptions import LogicException
from cleo.exceptions import CleoLogicError
from cleo.loaders.factory_command_loader import FactoryCommandLoader


Expand All @@ -12,11 +12,11 @@
from cleo.commands.command import Command


class CommandLoader(FactoryCommandLoader): # type: ignore[misc]
class CommandLoader(FactoryCommandLoader):
def register_factory(
self, command_name: str, factory: Callable[[], Command]
) -> None:
if command_name in self._factories:
raise LogicException(f'The command "{command_name}" already exists.')
raise CleoLogicError(f'The command "{command_name}" already exists.')

self._factories[command_name] = factory
11 changes: 7 additions & 4 deletions src/poetry/console/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from typing import Any

from cleo.commands.command import Command as BaseCommand
from cleo.exceptions import ValueException
from cleo.exceptions import CleoValueError


if TYPE_CHECKING:
from poetry.console.application import Application
from poetry.poetry import Poetry


class Command(BaseCommand): # type: ignore[misc]
class Command(BaseCommand):
loggers: list[str] = []

_poetry: Poetry | None = None
Expand All @@ -28,7 +28,10 @@ def set_poetry(self, poetry: Poetry) -> None:
self._poetry = poetry

def get_application(self) -> Application:
application: Application = self.application
from poetry.console.application import Application

application = self.application
assert isinstance(application, Application)
return application

def reset_poetry(self) -> None:
Expand All @@ -37,5 +40,5 @@ def reset_poetry(self) -> None:
def option(self, name: str, default: Any = None) -> Any:
try:
return super().option(name)
except ValueException:
except CleoValueError:
return default
2 changes: 1 addition & 1 deletion src/poetry/console/commands/debug/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def handle(self) -> int:
]
)
)
command = self.application.get("env info")
command = self.get_application().get("env info")

exit_code: int = command.run(self.io)
return exit_code
10 changes: 8 additions & 2 deletions src/poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cleo.helpers import argument
from cleo.helpers import option
from cleo.io.outputs.output import Verbosity
Expand All @@ -8,6 +10,10 @@
from poetry.console.commands.show import ShowCommand


if TYPE_CHECKING:
from cleo.ui.table import Rows


class DebugResolveCommand(InitCommand):
name = "debug resolve"
description = "Debugs dependency resolution."
Expand Down Expand Up @@ -86,7 +92,7 @@ def handle(self) -> int:
self.line("")

if self.option("tree"):
show_command = self.application.find("show")
show_command = self.get_application().find("show")
assert isinstance(show_command, ShowCommand)
show_command.init_styles(self.io)

Expand All @@ -103,7 +109,7 @@ def handle(self) -> int:

table = self.table(style="compact")
table.style.set_vertical_border_chars("", " ")
rows = []
rows: Rows = []

if self.option("install"):
env = EnvManager(self.poetry).get()
Expand Down
12 changes: 6 additions & 6 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def handle(self) -> int:
self._determine_requirements(self.option("dependency"))
)

question = "Would you like to define your main dependencies interactively?"
question_text = "Would you like to define your main dependencies interactively?"
help_message = """\
You can specify a package in the following forms:
- A single name (<b>requests</b>): this will search for matches on PyPI
Expand All @@ -190,7 +190,7 @@ def handle(self) -> int:
"""

help_displayed = False
if self.confirm(question, True):
if self.confirm(question_text, True):
if self.io.is_interactive():
self.line(help_message)
help_displayed = True
Expand All @@ -206,10 +206,10 @@ def handle(self) -> int:
self._determine_requirements(self.option("dev-dependency"))
)

question = (
question_text = (
"Would you like to define your development dependencies interactively?"
)
if self.confirm(question, True):
if self.confirm(question_text, True):
if self.io.is_interactive() and not help_displayed:
self.line(help_message)

Expand Down Expand Up @@ -338,8 +338,8 @@ def _determine_requirements(
"Enter the version constraint to require "
"(or leave blank to use the latest version):"
)
question.attempts = 3
question.validator = lambda x: (x or "").strip() or False
question.set_max_attempts(3)
question.set_validator(lambda x: (x or "").strip() or None)

package_constraint = self.ask(question)

Expand Down
3 changes: 2 additions & 1 deletion src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

if TYPE_CHECKING:
from cleo.io.io import IO
from cleo.ui.table import Rows
from packaging.utils import NormalizedName
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package
Expand Down Expand Up @@ -160,7 +161,7 @@ def _display_single_package_information(

return 0

rows = [
rows: Rows = [
["<info>name</>", f" : <c1>{pkg.pretty_name}</>"],
["<info>version</>", f" : <b>{pkg.pretty_version}</b>"],
["<info>description</>", f" : {pkg.description}"],
Expand Down
8 changes: 7 additions & 1 deletion src/poetry/console/commands/source/show.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cleo.helpers import argument

from poetry.console.commands.command import Command


if TYPE_CHECKING:
from cleo.ui.table import Rows


class SourceShowCommand(Command):
name = "source show"
description = "Show information about sources configured for the project."
Expand Down Expand Up @@ -40,7 +46,7 @@ def handle(self) -> int:
continue

table = self.table(style="compact")
rows = [
rows: Rows = [
["<info>name</>", f" : <c1>{source.name}</>"],
["<info>url</>", f" : {source.url}"],
[
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/console/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from cleo.exceptions import CleoSimpleException
from cleo.exceptions import CleoError


class PoetrySimpleConsoleException(CleoSimpleException): # type: ignore[misc]
class PoetryConsoleError(CleoError):
pass
2 changes: 1 addition & 1 deletion src/poetry/console/io/inputs/run_argv_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from cleo.io.inputs.definition import Definition


class RunArgvInput(ArgvInput): # type: ignore[misc]
class RunArgvInput(ArgvInput):
def __init__(
self,
argv: list[str] | None = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

from crashtest.contracts.has_solutions_for_exception import HasSolutionsForException

from poetry.puzzle.exceptions import SolverProblemError


if TYPE_CHECKING:
from crashtest.contracts.solution import Solution

from poetry.puzzle.exceptions import SolverProblemError


class PythonRequirementSolutionProvider(HasSolutionsForException): # type: ignore[misc]
class PythonRequirementSolutionProvider(HasSolutionsForException):
def can_solve(self, exception: Exception) -> bool:
from poetry.puzzle.exceptions import SolverProblemError

if not isinstance(exception, SolverProblemError):
return False

Expand All @@ -28,9 +26,10 @@ def can_solve(self, exception: Exception) -> bool:

return bool(m)

def get_solutions(self, exception: SolverProblemError) -> list[Solution]:
def get_solutions(self, exception: Exception) -> list[Solution]:
from poetry.mixology.solutions.solutions.python_requirement_solution import (
PythonRequirementSolution,
)

assert isinstance(exception, SolverProblemError)
return [PythonRequirementSolution(exception)]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from poetry.puzzle.exceptions import SolverProblemError


class PythonRequirementSolution(Solution): # type: ignore[misc]
class PythonRequirementSolution(Solution):
def __init__(self, exception: SolverProblemError) -> None:
from poetry.core.constraints.version import parse_constraint

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
logger = logging.getLogger(__name__)


class Indicator(ProgressIndicator): # type: ignore[misc]
class Indicator(ProgressIndicator):
CONTEXT: str | None = None

@staticmethod
Expand Down
Loading

0 comments on commit 14b7f1e

Please sign in to comment.