Skip to content

Commit

Permalink
Typing syntax fixes (python-poetry#1156)
Browse files Browse the repository at this point in the history
* Typing syntax fixes

* Fix some function signatures that were messing with ellipsis
  for example, `fun(a, ...)` is not valid, but `fun(...)` is

* Dict value types - is that legal? (puzzle/provider.py)

* Ignore assigning instance to classes
  (singletons like Infinity in version/utils.py)

* Add basic mypy config

This configuration has all common settings like:

* strict Optional in functions that returns None

* ignore missing imports - this is usual thing

* Check code that has no typing annotation but uses typed classes/functions
  • Loading branch information
kam1sh authored and sdispater committed Jun 30, 2019
1 parent f43ef05 commit 269e91c
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 27 deletions.
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mypy]
check_untyped_defs = True
ignore_errors = False
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
6 changes: 5 additions & 1 deletion poetry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def file(self):
def content(self):
return self._content

def setting(self, setting_name, default=None): # type: (str) -> Any
def setting(
self,
setting_name, # type: str
default=None,
):
"""
Retrieve a setting value.
"""
Expand Down
4 changes: 2 additions & 2 deletions poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ def handle(self):
f.write(content)

def _determine_requirements(
self, requires, allow_prereleases=False # type: List[str] # type: bool
): # type: (...) -> List[str]
self, requires, allow_prereleases=False
): # type: (List[str], bool) -> List[str]
if not requires:
requires = []

Expand Down
9 changes: 6 additions & 3 deletions poetry/console/config/application_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def configure(self):
self.add_event_listener(ConsoleEvents.PRE_HANDLE.value, self.set_env)

def register_command_loggers(
self, event, event_name, _
): # type: (PreHandleEvent, str, ...) -> None
self,
event, # type: PreHandleEvent
event_name, # type: str
_,
): # type: (...) -> None
command = event.command.config.handler
if not isinstance(command, Command):
return
Expand All @@ -56,7 +59,7 @@ def register_command_loggers(

logger.setLevel(level)

def set_env(self, event, event_name, _): # type: (PreHandleEvent, str, ...) -> None
def set_env(self, event, event_name, _): # type: (PreHandleEvent, str, _) -> None
from poetry.semver import parse_constraint
from poetry.utils.env import EnvManager

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


class PipInstaller(BaseInstaller):
def __init__(self, env, io, pool): # type: (Env, ...) -> None
def __init__(
self,
env, # type: Env
io,
pool,
): # type: (...) -> None
self._env = env
self._io = io
self._pool = pool
Expand Down
2 changes: 1 addition & 1 deletion poetry/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def convert_entry_points(self): # type: () -> dict
return dict(result)

@classmethod
def convert_author(cls, author): # type: () -> dict
def convert_author(cls, author): # type: (...) -> dict
m = AUTHOR_REGEX.match(author)

name = m.group("name")
Expand Down
4 changes: 3 additions & 1 deletion poetry/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ def find_nearest_pkg(rel_path):

@classmethod
def convert_dependencies(
cls, package, dependencies # type: Package # type: List[Dependency]
cls,
package, # type: Package
dependencies, # type: List[Dependency]
):
main = []
extras = defaultdict(list)
Expand Down
2 changes: 1 addition & 1 deletion poetry/masonry/utils/package_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def source(self): # type: () -> str
def is_package(self): # type: () -> bool
return self._is_package

def is_module(self): # type: ()
def is_module(self): # type: () -> bool
return self._is_module

def refresh(self): # type: () -> PackageInclude
Expand Down
2 changes: 1 addition & 1 deletion poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def locked_repository(

return packages

def set_lock_data(self, root, packages): # type: () -> bool
def set_lock_data(self, root, packages): # type: (...) -> bool
hashes = {}
packages = self._lock_packages(packages)
# Retrieving hashes
Expand Down
7 changes: 6 additions & 1 deletion poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ class Provider:

UNSAFE_PACKAGES = {"setuptools", "distribute", "pip"}

def __init__(self, package, pool, io): # type: (Package, Pool, ...) -> None
def __init__(
self,
package, # type: Package
pool, # type: Pool
io,
): # type: (...) -> None
self._package = package
self._pool = pool
self._io = io
Expand Down
7 changes: 4 additions & 3 deletions poetry/puzzle/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,13 @@ def _build_graph(

marker = intersection

childrens = [] # type: List[Dict[str, Any]]
graph = {
"name": package.name,
"category": category,
"optional": optional,
"marker": marker,
"children": [], # type: List[Dict[str, Any]]
"children": childrens,
}

if previous_dep and previous_dep is not dep and previous_dep.name == dep.name:
Expand Down Expand Up @@ -259,7 +260,7 @@ def _build_graph(
# If there is already a child with this name
# we merge the requirements
existing = None
for child in graph["children"]:
for child in childrens:
if (
child["name"] == pkg.name
and child["category"] == dependency.category
Expand All @@ -280,7 +281,7 @@ def _build_graph(
)
continue

graph["children"].append(child_graph)
childrens.append(child_graph)

return graph

Expand Down
18 changes: 9 additions & 9 deletions poetry/semver/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re

from typing import List
from typing import Union
from typing import Optional

from .empty_constraint import EmptyConstraint
from .exceptions import ParseVersionError
Expand All @@ -19,14 +19,14 @@ class Version(VersionRange):
def __init__(
self,
major, # type: int
minor=None, # type: Union[int, None]
patch=None, # type: Union[int, None]
rest=None, # type: Union[int, None]
pre=None, # type: Union[str, None]
build=None, # type: Union[str, None]
text=None, # type: Union[str, None]
precision=None, # type: Union[int, None]
): # type: () -> None
minor=None, # type: Optional[int]
patch=None, # type: Optional[int]
rest=None, # type: Optional[int]
pre=None, # type: Optional[str]
build=None, # type: Optional[str]
text=None, # type: Optional[str]
precision=None, # type: Optional[int]
): # type: (...) -> None
self._major = int(major)
self._precision = None
if precision is None:
Expand Down
3 changes: 2 additions & 1 deletion poetry/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@


def parse(
version, strict=False # type: str # type: bool
version, # type: str
strict=False, # type: bool
): # type:(...) -> Union[Version, LegacyVersion]
"""
Parse the given version string and return either a :class:`Version` object
Expand Down
4 changes: 2 additions & 2 deletions poetry/version/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __neg__(self):
return NegativeInfinity


Infinity = Infinity()
Infinity = Infinity() # type: ignore


class NegativeInfinity(object):
Expand Down Expand Up @@ -59,4 +59,4 @@ def __neg__(self):
return Infinity


NegativeInfinity = NegativeInfinity()
NegativeInfinity = NegativeInfinity() # type: ignore

0 comments on commit 269e91c

Please sign in to comment.