Skip to content

Commit

Permalink
enable strictness flags for mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed May 2, 2022
1 parent bf04e20 commit ed6378a
Show file tree
Hide file tree
Showing 37 changed files with 132 additions and 144 deletions.
8 changes: 2 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,9 @@ force-exclude = '''


[tool.mypy]
check_untyped_defs = true
ignore_missing_imports = true
show_error_codes = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unused_ignores = true
files = "src"
show_error_codes = true
strict = true

# The following whitelist is used to allow for incremental adoption
# of Mypy. Modules should be removed from this whitelist as and when
Expand Down
12 changes: 8 additions & 4 deletions src/poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
self._auth_config_source: ConfigSource = DictConfigSource()

@property
def config(self) -> dict:
def config(self) -> dict[str, Any]:
return self._config

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

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

for key in config:
Expand Down Expand Up @@ -130,10 +130,14 @@ def process(self, value: Any) -> Any:
if not isinstance(value, str):
return value

return re.sub(r"{(.+?)}", lambda m: self.get(m.group(1)), value)
return re.sub(
r"{(.+?)}",
lambda m: self.get(m.group(1)), # type: ignore[no-any-return]
value,
)

@staticmethod
def _get_normalizer(name: str) -> Callable:
def _get_normalizer(name: str) -> Callable[[str], Any]:
if name in {
"virtualenvs.create",
"virtualenvs.in-project",
Expand Down
18 changes: 11 additions & 7 deletions src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
from poetry.poetry import Poetry


def load_command(name: str) -> Callable:
def load_command(name: str) -> Callable[[], type[Command]]:
def _load() -> type[Command]:
words = name.split(" ")
module = import_module("poetry.console.commands." + ".".join(words))
command_class = getattr(module, "".join(c.title() for c in words) + "Command")
return command_class()
command_type: type[Command] = command_class()
return command_type

return _load

Expand Down Expand Up @@ -89,7 +90,7 @@ def _load() -> type[Command]:
]


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

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

@property
def command_loader(self) -> CommandLoader:
return self._command_loader
command_loader: CommandLoader = self._command_loader
return command_loader

def reset_poetry(self) -> None:
self._poetry = None
Expand Down Expand Up @@ -171,7 +173,8 @@ def _run(self, io: IO) -> int:

self._load_plugins(io)

return super()._run(io)
exit_code: int = super()._run(io)
return exit_code

def _configure_io(self, io: IO) -> None:
# We need to check if the command being run
Expand Down Expand Up @@ -207,7 +210,7 @@ def _configure_io(self, io: IO) -> None:

io.set_input(run_input)

return super()._configure_io(io)
super()._configure_io(io)

def register_command_loggers(
self, event: ConsoleCommandEvent, event_name: str, _: Any
Expand Down Expand Up @@ -365,7 +368,8 @@ def _get_solution_provider_repository(self) -> SolutionProviderRepository:


def main() -> int:
return Application().run()
exit_code: int = Application().run()
return exit_code


if __name__ == "__main__":
Expand Down
11 changes: 9 additions & 2 deletions src/poetry/console/command_loader.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import Callable

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


class CommandLoader(FactoryCommandLoader):
def register_factory(self, command_name: str, factory: Callable) -> None:
if TYPE_CHECKING:
from poetry.console.commands.command import Command


class CommandLoader(FactoryCommandLoader): # type: ignore[misc]
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.')

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def handle(self) -> int:
return status

def get_existing_packages_from_input(
self, packages: list[str], section: dict
self, packages: list[str], section: dict[str, Any]
) -> list[str]:
existing_packages = []

Expand Down
5 changes: 3 additions & 2 deletions src/poetry/console/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from poetry.poetry import Poetry


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

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

def get_application(self) -> Application:
return self.application
application: Application = self.application
return application

def reset_poetry(self) -> None:
self.get_application().reset_poetry()
Expand Down
50 changes: 2 additions & 48 deletions src/poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ def _list_configuration(

if isinstance(value, dict):
k += f"{key}."
self._list_configuration(value, cast(dict, raw_val), k=k)
raw_val = cast("dict[str, Any]", raw_val)
self._list_configuration(value, raw_val, k=k)
k = orig_k

continue
Expand All @@ -328,50 +329,3 @@ def _list_configuration(
message = f"<c1>{k + key}</c1> = <c2>{json.dumps(value)}</c2>"

self.line(message)

def _get_setting(
self,
contents: dict,
setting: str | None = None,
k: str | None = None,
default: Any | None = None,
) -> list[tuple[str, str]]:
orig_k = k

if setting and setting.split(".")[0] not in contents:
value = json.dumps(default)

return [((k or "") + setting, value)]
else:
values = []
for key, value in contents.items():
if setting and key != setting.split(".")[0]:
continue

if isinstance(value, dict) or key == "repositories" and k is None:
if k is None:
k = ""

k += re.sub(r"^config\.", "", key + ".")
if setting and len(setting) > 1:
setting = ".".join(setting.split(".")[1:])

values += self._get_setting(
cast(dict, value), k=k, setting=setting, default=default
)
k = orig_k

continue

if isinstance(value, list):
value = ", ".join(
json.dumps(val) if isinstance(val, list) else val
for val in value
)
value = f"[{value}]"

value = json.dumps(value)

values.append(((k or "") + key, value))

return values
3 changes: 2 additions & 1 deletion src/poetry/console/commands/debug/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ def handle(self) -> int:
)
command = self.application.get("env info")

return command.run(self._io)
exit_code: int = command.run(self._io)
return exit_code
2 changes: 1 addition & 1 deletion src/poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def handle(self) -> int:
from poetry.core.packages.project_package import ProjectPackage

from poetry.factory import Factory
from poetry.puzzle import Solver
from poetry.puzzle.solver import Solver
from poetry.repositories.pool import Pool
from poetry.repositories.repository import Repository
from poetry.utils.env import EnvManager
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class InstallCommand(InstallerCommand):
def handle(self) -> int:
from poetry.core.masonry.utils.module import ModuleOrPackageNotFound

from poetry.masonry.builders import EditableBuilder
from poetry.masonry.builders.editable import EditableBuilder

self._installer.use_executor(
self.poetry.config.get("experimental.new-installer", False)
Expand Down
5 changes: 3 additions & 2 deletions src/poetry/console/commands/plugin/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,17 @@ def handle(self) -> int:
if self.option("dry-run"):
argv.append("--dry-run")

return update_command.run(
exit_code: int = update_command.run(
IO(
StringInput(" ".join(argv)),
self._io.output,
self._io.error_output,
)
)
return exit_code

def get_existing_packages_from_input(
self, packages: list[str], poetry_content: dict, target_section: str
self, packages: list[str], poetry_content: dict[str, Any], target_section: str
) -> list[str]:
existing_packages = []

Expand Down
3 changes: 2 additions & 1 deletion src/poetry/console/commands/plugin/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ def handle(self) -> int:
if self.option("dry-run"):
argv.append("--dry-run")

return remove_command.run(
exit_code: int = remove_command.run(
IO(
StringInput(" ".join(argv)),
self._io.output,
self._io.error_output,
)
)
return exit_code
2 changes: 1 addition & 1 deletion src/poetry/console/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from cleo.exceptions import CleoSimpleException


class PoetrySimpleConsoleException(CleoSimpleException):
class PoetrySimpleConsoleException(CleoSimpleException): # type: ignore[misc]

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):
class RunArgvInput(ArgvInput): # type: ignore[misc]
def __init__(
self,
argv: list[str] | None = None,
Expand Down
9 changes: 7 additions & 2 deletions src/poetry/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
import os

from typing import Any

import jsonschema


Expand All @@ -14,7 +16,7 @@ class ValidationError(ValueError):
pass


def validate_object(obj: dict, schema_name: str) -> list[str]:
def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]:
schema = os.path.join(SCHEMA_DIR, f"{schema_name}.json")

if not os.path.exists(schema):
Expand All @@ -24,7 +26,10 @@ def validate_object(obj: dict, schema_name: str) -> list[str]:
schema = json.loads(f.read())

validator = jsonschema.Draft7Validator(schema)
validation_errors = sorted(validator.iter_errors(obj), key=lambda e: e.path)
validation_errors = sorted(
validator.iter_errors(obj),
key=lambda e: e.path, # type: ignore[no-any-return]
)

errors = []

Expand Down
7 changes: 5 additions & 2 deletions src/poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ def get_package_include(self) -> InlineTable | None:
package = inline_table()

include = self._package_path_relative.parts[0]
package.append("include", include)
package.append("include", include) # type: ignore[no-untyped-call]

if self.basedir != Path():
package.append("from", self.basedir.as_posix())
package.append(
"from",
self.basedir.as_posix(),
) # type: ignore[no-untyped-call]
else:
if include == self._project:
# package include and package name are the same,
Expand Down
Loading

0 comments on commit ed6378a

Please sign in to comment.