Skip to content

Commit

Permalink
Make imports lazier (#3938)
Browse files Browse the repository at this point in the history
This change speeds up poetry command start time by 50ms compared to
current master and around 250ms compared to 1.1.5.

Relates-to: #3502
  • Loading branch information
abn authored Nov 11, 2021
1 parent 6e53b90 commit da40171
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions poetry/console/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from pathlib import Path

from poetry.core.pyproject.toml import PyProjectTOML
from poetry.factory import Factory

from .command import Command


Expand All @@ -12,6 +9,9 @@ class CheckCommand(Command):
description = "Checks the validity of the <comment>pyproject.toml</comment> file."

def handle(self) -> int:
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.factory import Factory

# Load poetry config and display errors, if any
poetry_file = Factory.locate(Path.cwd())
config = PyProjectTOML(poetry_file).poetry_config
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cleo.helpers import argument
from cleo.helpers import option

from .installer_command import InstallerCommand
from poetry.console.commands.installer_command import InstallerCommand


class RemoveCommand(InstallerCommand):
Expand Down
7 changes: 4 additions & 3 deletions poetry/console/commands/source/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

from poetry.config.source import Source
from poetry.console.commands.command import Command
from poetry.factory import Factory
from poetry.repositories import Pool


class SourceAddCommand(Command):
Expand Down Expand Up @@ -39,14 +37,17 @@ class SourceAddCommand(Command):
]

@staticmethod
def source_to_table(source: Source) -> Table:
def source_to_table(source: "Source") -> Table:
source_table: Table = table()
for key, value in source.to_dict().items():
source_table.add(key, value)
source_table.add(nl())
return source_table

def handle(self) -> Optional[int]:
from poetry.factory import Factory
from poetry.repositories import Pool

name = self.argument("name")
url = self.argument("url")
is_default = self.option("default")
Expand Down
3 changes: 2 additions & 1 deletion poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from poetry.packages.project_package import ProjectPackage
from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry
from poetry.repositories.pypi_repository import PyPiRepository


if TYPE_CHECKING:
Expand Down Expand Up @@ -156,6 +155,8 @@ def configure_sources(
if io.is_debug():
io.write_line("Deactivating the PyPI repository")
else:
from poetry.repositories.pypi_repository import PyPiRepository

default = not poetry.pool.has_primary_repositories()
poetry.pool.add_repository(PyPiRepository(), default, not default)

Expand Down
8 changes: 6 additions & 2 deletions poetry/installation/chooser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from typing import TYPE_CHECKING
from typing import List
from typing import Optional
from typing import Tuple
Expand All @@ -8,11 +9,14 @@

from poetry.core.packages.package import Package
from poetry.core.packages.utils.link import Link
from poetry.repositories.pool import Pool
from poetry.utils.env import Env
from poetry.utils.patterns import wheel_file_re


if TYPE_CHECKING:
from poetry.repositories.pool import Pool


class InvalidWheelName(Exception):
pass

Expand Down Expand Up @@ -49,7 +53,7 @@ class Chooser:
A Chooser chooses an appropriate release archive for packages.
"""

def __init__(self, pool: Pool, env: Env) -> None:
def __init__(self, pool: "Pool", env: Env) -> None:
self._pool = pool
self._env = env

Expand Down
4 changes: 2 additions & 2 deletions poetry/installation/pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from poetry.core.pyproject.toml import PyProjectTOML
from poetry.installation.base_installer import BaseInstaller
from poetry.repositories.pool import Pool
from poetry.utils._compat import encode
from poetry.utils.env import Env
from poetry.utils.helpers import safe_rmtree
Expand All @@ -22,10 +21,11 @@

if TYPE_CHECKING:
from poetry.core.packages.package import Package
from poetry.repositories.pool import Pool


class PipInstaller(BaseInstaller):
def __init__(self, env: Env, io: IO, pool: Pool) -> None:
def __init__(self, env: Env, io: IO, pool: "Pool") -> None:
self._env = env
self._io = io
self._pool = pool
Expand Down
13 changes: 6 additions & 7 deletions poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from tomlkit import table
from tomlkit.exceptions import TOMLKitError

import poetry.repositories

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package
from poetry.core.semver.helpers import parse_constraint
Expand All @@ -40,6 +38,8 @@
if TYPE_CHECKING:
from tomlkit.toml_document import TOMLDocument

from poetry.repositories import Repository

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -87,19 +87,18 @@ def is_fresh(self) -> bool:

return False

def locked_repository(
self, with_dev_reqs: bool = False
) -> poetry.repositories.Repository:
def locked_repository(self, with_dev_reqs: bool = False) -> "Repository":
"""
Searches and returns a repository of locked packages.
"""
from poetry.factory import Factory
from poetry.repositories import Repository

if not self.is_locked():
return poetry.repositories.Repository()
return Repository()

lock_data = self.lock_data
packages = poetry.repositories.Repository()
packages = Repository()

if with_dev_reqs:
locked_packages = lock_data["package"]
Expand Down
16 changes: 11 additions & 5 deletions poetry/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

from contextlib import contextmanager
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
from typing import Dict
from typing import Iterator
from typing import List
from typing import Optional

import requests

from poetry.config.config import Config
from poetry.core.packages.package import Package


try:
Expand All @@ -25,6 +23,12 @@
from collections import Mapping


if TYPE_CHECKING:
from requests import Session

from poetry.core.packages.package import Package


_canonicalize_regex = re.compile("[-_]+")


Expand Down Expand Up @@ -92,9 +96,11 @@ def merge_dicts(d1: Dict, d2: Dict) -> None:
def download_file(
url: str,
dest: str,
session: Optional[requests.Session] = None,
session: Optional["Session"] = None,
chunk_size: int = 1024,
) -> None:
import requests

get = requests.get if not session else session.get

with get(url, stream=True) as response:
Expand All @@ -107,7 +113,7 @@ def download_file(


def get_package_version_display_string(
package: Package, root: Optional[Path] = None
package: "Package", root: Optional[Path] = None
) -> str:
if package.source_type in ["file", "directory"] and root:
return "{} {}".format(
Expand Down

0 comments on commit da40171

Please sign in to comment.