Skip to content

Commit

Permalink
Merge pull request #3314 from finswimmer/improvement/type-hinting
Browse files Browse the repository at this point in the history
fix and add several type hints
  • Loading branch information
sdispater authored Jan 30, 2021
2 parents d2485b8 + 1346497 commit a104970
Show file tree
Hide file tree
Showing 88 changed files with 859 additions and 429 deletions.
10 changes: 5 additions & 5 deletions poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
_NOT_SET = object()


def boolean_validator(val):
def boolean_validator(val): # type: (str) -> bool
return val in {"true", "false", "1", "0"}


def boolean_normalizer(val):
def boolean_normalizer(val): # type: (str) -> bool
return val in ["true", "1"]


Expand Down Expand Up @@ -51,11 +51,11 @@ def __init__(
self._auth_config_source = DictConfigSource()

@property
def name(self):
def name(self): # type: () -> str
return str(self._file.path)

@property
def config(self):
def config(self): # type: () -> Dict
return self._config

@property
Expand All @@ -82,7 +82,7 @@ def merge(self, config): # type: (Dict[str, Any]) -> None
merge_dicts(self._config, config)

def all(self): # type: () -> Dict[str, Any]
def _all(config, parent_key=""):
def _all(config, parent_key=""): # type: (Dict, str) -> Dict
all_ = {}

for key in config:
Expand Down
5 changes: 4 additions & 1 deletion poetry/config/file_config_source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import contextmanager
from typing import TYPE_CHECKING
from typing import Any
from typing import Generator

from tomlkit import document
from tomlkit import table
Expand All @@ -9,6 +10,8 @@


if TYPE_CHECKING:
from tomlkit.toml_document import TOMLDocument # noqa

from poetry.core.toml.file import TOMLFile # noqa


Expand Down Expand Up @@ -56,7 +59,7 @@ def remove_property(self, key): # type: (str) -> None
current_config = current_config[key]

@contextmanager
def secure(self):
def secure(self): # type: () -> Generator["TOMLDocument"]
if self.file.exists():
initial_config = self.file.read()
config = self.file.read()
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .application import Application


def main():
def main(): # type: () -> int
return Application().run()
10 changes: 8 additions & 2 deletions poetry/console/application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys

from typing import TYPE_CHECKING

from cleo import Application as BaseApplication

from poetry.__version__ import __version__
Expand Down Expand Up @@ -29,8 +31,12 @@
from .config import ApplicationConfig


if TYPE_CHECKING:
from poetry.poetry import Poetry # noqa


class Application(BaseApplication):
def __init__(self):
def __init__(self): # type: () -> None
super(Application, self).__init__(
"poetry", __version__, config=ApplicationConfig("poetry", __version__)
)
Expand Down Expand Up @@ -59,7 +65,7 @@ def __init__(self):
self._preliminary_io.error_line("<fg=yellow>{}</>\n".format(message))

@property
def poetry(self):
def poetry(self): # type: () -> "Poetry"
from pathlib import Path

from poetry.factory import Factory
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AboutCommand(Command):

description = "Shows information about Poetry."

def handle(self):
def handle(self): # type: () -> None
self.line(
"""<info>Poetry - Package Management for Python</info>
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AddCommand(InstallerCommand, InitCommand):

loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"]

def handle(self):
def handle(self): # type: () -> int
from tomlkit import inline_table

from poetry.core.semver import parse_constraint
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BuildCommand(EnvCommand):
"poetry.core.masonry.builders.wheel",
]

def handle(self):
def handle(self): # type: () -> None
from poetry.core.masonry import Builder

fmt = "all"
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class CacheCommand(Command):

commands = [CacheClearCommand(), CacheListCommand()]

def handle(self):
def handle(self): # type: () -> int
return self.call("help", self._config.name)
2 changes: 1 addition & 1 deletion poetry/console/commands/cache/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CacheClearCommand(Command):
arguments = [argument("cache", description="The name of the cache to clear.")]
options = [option("all", description="Clear all entries in the cache.")]

def handle(self):
def handle(self): # type: () -> int
from cachy import CacheManager

from poetry.locations import REPOSITORY_CACHE_DIR
Expand Down
4 changes: 3 additions & 1 deletion poetry/console/commands/cache/list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from typing import Optional

from ..command import Command


Expand All @@ -8,7 +10,7 @@ class CacheListCommand(Command):
name = "list"
description = "List Poetry's caches."

def handle(self):
def handle(self): # type: () -> Optional[int]
from poetry.locations import REPOSITORY_CACHE_DIR

if os.path.exists(str(REPOSITORY_CACHE_DIR)):
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CheckCommand(Command):
name = "check"
description = "Checks the validity of the <comment>pyproject.toml</comment> file."

def handle(self):
def handle(self): # type: () -> int
# Load poetry config and display errors, if any
poetry_file = Factory.locate(Path.cwd())
config = PyProjectTOML(poetry_file).poetry_config
Expand Down
8 changes: 7 additions & 1 deletion poetry/console/commands/command.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from typing import TYPE_CHECKING

from cleo import Command as BaseCommand


if TYPE_CHECKING:
from poetry.poetry import Poetry # noqa


class Command(BaseCommand):

loggers = []

@property
def poetry(self):
def poetry(self): # type: () -> "Poetry"
return self.application.poetry

def reset_poetry(self): # type: () -> None
Expand Down
25 changes: 20 additions & 5 deletions poetry/console/commands/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import json
import re

from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple

from cleo import argument
from cleo import option

Expand All @@ -11,6 +18,10 @@
from .command import Command


if TYPE_CHECKING:
from poetry.config.config_source import ConfigSource # noqa


class ConfigCommand(Command):

name = "config"
Expand Down Expand Up @@ -40,7 +51,7 @@ class ConfigCommand(Command):
LIST_PROHIBITED_SETTINGS = {"http-basic", "pypi-token"}

@property
def unique_config_values(self):
def unique_config_values(self): # type: () -> Dict[str, Tuple[Any, Any, Any]]
from pathlib import Path

from poetry.config.config import boolean_normalizer
Expand Down Expand Up @@ -75,7 +86,7 @@ def unique_config_values(self):

return unique_config_values

def handle(self):
def handle(self): # type: () -> Optional[int]
from pathlib import Path

from poetry.config.file_config_source import FileConfigSource
Expand Down Expand Up @@ -253,7 +264,9 @@ def handle(self):

raise ValueError("Setting {} does not exist".format(self.argument("key")))

def _handle_single_value(self, source, key, callbacks, values):
def _handle_single_value(
self, source, key, callbacks, values
): # type: ("ConfigSource", str, Tuple[Any, Any, Any], List[Any]) -> int
validator, normalizer, _ = callbacks

if len(values) > 1:
Expand All @@ -267,7 +280,7 @@ def _handle_single_value(self, source, key, callbacks, values):

return 0

def _list_configuration(self, config, raw, k=""):
def _list_configuration(self, config, raw, k=""): # type: (Dict, Dict, str) -> None
orig_k = k
for key, value in sorted(config.items()):
if k + key in self.LIST_PROHIBITED_SETTINGS:
Expand Down Expand Up @@ -301,7 +314,9 @@ def _list_configuration(self, config, raw, k=""):

self.line(message)

def _get_setting(self, contents, setting=None, k=None, default=None):
def _get_setting(
self, contents, setting=None, k=None, default=None
): # type: (Dict, Optional[str], Optional[str], Optional[Any]) -> List[Tuple[str, str]]
orig_k = k

if setting and setting.split(".")[0] not in contents:
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/debug/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DebugInfoCommand(Command):
name = "info"
description = "Shows debug information."

def handle(self):
def handle(self): # type: () -> int
poetry_python_version = ".".join(str(s) for s in sys.version_info[:3])

self.line("")
Expand Down
4 changes: 3 additions & 1 deletion poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from cleo import argument
from cleo import option

Expand Down Expand Up @@ -27,7 +29,7 @@ class DebugResolveCommand(InitCommand):

loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"]

def handle(self):
def handle(self): # type: () -> Optional[int]
from poetry.core.packages.project_package import ProjectPackage
from poetry.factory import Factory
from poetry.io.null_io import NullIO
Expand Down
11 changes: 9 additions & 2 deletions poetry/console/commands/env/info.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from typing import TYPE_CHECKING
from typing import Optional

from cleo import option

from ..command import Command


if TYPE_CHECKING:
from poetry.utils.env import Env # noqa


class EnvInfoCommand(Command):

name = "info"
description = "Displays information about the current environment."

options = [option("path", "p", "Only display the environment's path.")]

def handle(self):
def handle(self): # type: () -> Optional[int]
from poetry.utils.env import EnvManager

env = EnvManager(self.poetry).get()
Expand All @@ -25,7 +32,7 @@ def handle(self):

self._display_complete_info(env)

def _display_complete_info(self, env):
def _display_complete_info(self, env): # type: ("Env") -> None
env_python_version = ".".join(str(s) for s in env.version_info[:3])
self.line("")
self.line("<b>Virtualenv</b>")
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/env/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EnvListCommand(Command):

options = [option("full-path", None, "Output the full paths of the virtualenvs.")]

def handle(self):
def handle(self): # type: () -> None
from poetry.utils.env import EnvManager

manager = EnvManager(self.poetry)
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/env/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EnvRemoveCommand(Command):
argument("python", "The python executable to remove the virtualenv for.")
]

def handle(self):
def handle(self): # type: () -> None
from poetry.utils.env import EnvManager

manager = EnvManager(self.poetry)
Expand Down
2 changes: 1 addition & 1 deletion poetry/console/commands/env/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EnvUseCommand(Command):

arguments = [argument("python", "The python executable to use.")]

def handle(self):
def handle(self): # type: () -> None
from poetry.utils.env import EnvManager

manager = EnvManager(self.poetry)
Expand Down
12 changes: 9 additions & 3 deletions poetry/console/commands/env_command.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
from typing import TYPE_CHECKING

from .command import Command


if TYPE_CHECKING:
from poetry.utils.env import VirtualEnv # noqa


class EnvCommand(Command):
def __init__(self):
def __init__(self): # type: () -> None
self._env = None

super(EnvCommand, self).__init__()

@property
def env(self):
def env(self): # type: () -> "VirtualEnv"
return self._env

def set_env(self, env):
def set_env(self, env): # type: ("VirtualEnv") -> None
self._env = env
2 changes: 1 addition & 1 deletion poetry/console/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ExportCommand(Command):
option("with-credentials", None, "Include credentials for extra indices."),
]

def handle(self):
def handle(self): # type: () -> None
fmt = self.option("format")

if fmt not in Exporter.ACCEPTED_FORMATS:
Expand Down
Loading

0 comments on commit a104970

Please sign in to comment.