Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing type hints - Part 1 #4775

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
47c0603
add missing type hints to application code
finswimmer Nov 16, 2021
343428e
add typehints to tests.config, tests.console.commands.debug, tests.co…
finswimmer Nov 16, 2021
d9f6d10
add typehints to tests.console.commands.plugin
finswimmer Nov 16, 2021
f6ebbab
add typehints to tests.self
finswimmer Nov 16, 2021
31cdfc5
add typehints to tests.console.commands.source
finswimmer Nov 17, 2021
df27a71
add typehints to tests.console.commands.about
finswimmer Nov 17, 2021
d509203
add typehints tests.console.commands.add
finswimmer Nov 17, 2021
d6a392f
add typehints to tests.console.commands.test_cache
finswimmer Nov 17, 2021
e2acde4
add typehints to tests.console.commands.test_check
finswimmer Nov 17, 2021
321160f
add typehints to tests.console.commands.test_config
finswimmer Nov 17, 2021
df5583d
add typehints to tests.console.commands.test_export
finswimmer Nov 17, 2021
3f16a9f
add typehints to tests.console.commands.test_init
finswimmer Nov 17, 2021
0f536a9
add typehints to tests.console.commands.test_install
finswimmer Nov 17, 2021
b84fab9
add typehints to tests.console.commands.test_lock
finswimmer Nov 17, 2021
f90258b
add typehints to tests.console.commands.test_new
finswimmer Nov 17, 2021
2c2b7b8
add typehints to tests.console.commands.test_publish
finswimmer Nov 17, 2021
32afc8b
add typehints to tests.console.commands.test_remove
finswimmer Nov 17, 2021
67da19d
add typehints to tests.console.commands.test_run
finswimmer Nov 17, 2021
18e930d
add typehints to tests.console.commands.test_search
finswimmer Nov 17, 2021
c2e0141
add typehints to tests.console.commands.test_show
finswimmer Nov 17, 2021
55e5ba2
add typehints to tests.console.commands.test_version
finswimmer Nov 17, 2021
bc6d92d
add typehints to tests.console.test_application
finswimmer Nov 17, 2021
6d3aafe
add typehints to tests.console.conftest
finswimmer Nov 17, 2021
12a24f6
add typehints to tests.console.conftest
finswimmer Nov 17, 2021
d90bb45
add python version constraint for typing-extension
finswimmer Nov 17, 2021
1ed885a
fix type hint for http fixture
finswimmer Nov 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pytest-sugar = "^0.9"
httpretty = "^1.0"
zipp = { version = "^3.4", python = "<3.8"}
deepdiff = "^5.0"
typing-extensions = {version = "^4.0.0", python = "<3.8"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but we are doing spaces around the braces.

(Side note: why doesn't poetry honor with with poetry add?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is how poetry add inserted it, I would prefer leaving it like it is.


[tool.poetry.scripts]
poetry = "poetry.console.application:main"
Expand Down
8 changes: 7 additions & 1 deletion src/poetry/plugins/application_plugin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from typing import TYPE_CHECKING

from poetry.plugins.base_plugin import BasePlugin


if TYPE_CHECKING:
from poetry.console.application import Application


class ApplicationPlugin(BasePlugin):
"""
Base class for plugins.
"""

type = "application.plugin"

def activate(self, application):
def activate(self, application: "Application") -> None:
raise NotImplementedError()
10 changes: 9 additions & 1 deletion src/poetry/plugins/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from typing import TYPE_CHECKING

from poetry.plugins.base_plugin import BasePlugin


if TYPE_CHECKING:
from cleo.io.io import IO

from poetry.poetry import Poetry


class Plugin(BasePlugin):
"""
Generic plugin not related to the console application.
Expand All @@ -10,5 +18,5 @@ class Plugin(BasePlugin):

type = "plugin"

def activate(self, poetry, io):
def activate(self, poetry: "Poetry", io: "IO") -> None:
raise NotImplementedError()
3 changes: 2 additions & 1 deletion src/poetry/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from typing import Any
from typing import List

import entrypoints
Expand Down Expand Up @@ -41,7 +42,7 @@ def add_plugin(self, plugin: Plugin) -> None:

self._plugins.append(plugin)

def activate(self, *args, **kwargs):
def activate(self, *args: Any, **kwargs: Any) -> None:
for plugin in self._plugins:
plugin.activate(*args, **kwargs)

Expand Down
11 changes: 7 additions & 4 deletions src/poetry/utils/_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import sys

from typing import List
from typing import Optional


try:
from importlib import metadata
Expand All @@ -10,7 +13,7 @@
WINDOWS = sys.platform == "win32"


def decode(string, encodings=None):
def decode(string: str, encodings: Optional[List[str]] = None) -> str:
if not isinstance(string, bytes):
return string

Expand All @@ -25,7 +28,7 @@ def decode(string, encodings=None):
return string.decode(encodings[0], errors="ignore")


def encode(string, encodings=None):
def encode(string: str, encodings: Optional[List[str]] = None) -> bytes:
if isinstance(string, bytes):
return string

Expand All @@ -40,11 +43,11 @@ def encode(string, encodings=None):
return string.encode(encodings[0], errors="ignore")


def to_str(string):
def to_str(string: str) -> str:
return decode(string)


def list_to_shell_command(cmd):
def list_to_shell_command(cmd: List[str]) -> str:
return " ".join(
f'"{token}"' if " " in token and token[0] not in {"'", '"'} else token
for token in cmd
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ def find_executables(self) -> None:

self._pip_executable = pip_executable

def get_embedded_wheel(self, distribution):
def get_embedded_wheel(self, distribution: str) -> Path:
return get_embed_wheel(
distribution, f"{self.version_info[0]}.{self.version_info[1]}"
).path
Expand Down Expand Up @@ -1785,7 +1785,7 @@ def _bin(self, bin: str) -> str:

@contextmanager
def ephemeral_environment(
executable=None,
executable: Optional[Union[str, Path]] = None,
flags: Dict[str, bool] = None,
with_pip: bool = False,
with_wheel: Optional[bool] = None,
Expand Down
5 changes: 5 additions & 0 deletions tests/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
import zipp
except ImportError:
import zipfile as zipp # noqa: F401, TC002

try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol # noqa: F401, TC002
30 changes: 24 additions & 6 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import os
import re

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

import pytest

from poetry.config.config import Config


def get_boolean_options(config=None):
if TYPE_CHECKING:
from pathlib import Path


def get_boolean_options(config: Optional[Dict[str, Any]] = None) -> str:
if config is None:
config = Config.default_config

Expand All @@ -21,27 +32,34 @@ def get_boolean_options(config=None):
@pytest.mark.parametrize(
("name", "value"), [("installer.parallel", True), ("virtualenvs.create", True)]
)
def test_config_get_default_value(config, name, value):
def test_config_get_default_value(config: Config, name: str, value: bool):
assert config.get(name) is value


def test_config_get_processes_depended_on_values(config, config_cache_dir):
def test_config_get_processes_depended_on_values(
config: Config, config_cache_dir: "Path"
):
assert str(config_cache_dir / "virtualenvs") == config.get("virtualenvs.path")


def generate_environment_variable_tests():
def generate_environment_variable_tests() -> Iterator[Tuple[str, str, str, bool]]:
for env_value, value in [("true", True), ("false", False)]:
for name in get_boolean_options():
env_var = "POETRY_{}".format(re.sub("[.-]+", "_", name).upper())
yield (name, env_var, env_value, value)
yield name, env_var, env_value, value


@pytest.mark.parametrize(
("name", "env_var", "env_value", "value"),
list(generate_environment_variable_tests()),
)
def test_config_get_from_environment_variable(
config, environ, name, env_var, env_value, value
config: Config,
environ: Iterator[None],
name: str,
env_var: str,
env_value: str,
value: bool,
):
os.environ[env_var] = env_value
assert config.get(name) is value
Loading