Skip to content

Commit

Permalink
Typehints for protected methods (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
penguinolog committed Apr 25, 2018
1 parent ae0bbec commit efe3e5d
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 20 deletions.
7 changes: 2 additions & 5 deletions logwrap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016-2017 Alexey Stepanov aka penguinolog
# Copyright 2016-2018 Alexey Stepanov aka penguinolog
#
# Copyright 2016 Mirantis, Inc.
#
Expand Down Expand Up @@ -62,8 +62,5 @@
'Dennis Dmitriev': 'dis-xcom@gmail.com',
}
__url__ = 'https://github.com/python-useful-helpers/logwrap'
__description__ = (
"Decorator for logging function arguments and "
"return value by human-readable way"
)
__description__ = "Decorator for logging function arguments and return value by human-readable way"
__license__ = "Apache License, Version 2.0"
8 changes: 8 additions & 0 deletions logwrap/_class_decorator.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import abc
import typing

PY3: bool

class BaseDecorator:
__wrapped__: typing.Optional[typing.Callable] = ...
def __init__(self, func: typing.Optional[typing.Callable]=...) -> None: ...

@property
def _func(self) -> typing.Optional[typing.Callable]: ...

@abc.abstractmethod
def _get_function_wrapper(self, func: typing.Callable) -> typing.Callable: ...

def __call__(self, *args: typing.Any, **kwargs: typing.Any) -> typing.Any: ...
2 changes: 1 addition & 1 deletion logwrap/_log_wrap2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016-2017 Alexey Stepanov aka penguinolog
# Copyright 2016-2018 Alexey Stepanov aka penguinolog

# Copyright 2016 Mirantis, Inc.

Expand Down
3 changes: 2 additions & 1 deletion logwrap/_log_wrap2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import logging
import typing
from . import _log_wrap_shared

class LogWrap(_log_wrap_shared.BaseLogWrap): ...
class LogWrap(_log_wrap_shared.BaseLogWrap):
def _get_function_wrapper(self, func: typing.Callable) -> typing.Callable: ...

def logwrap(
log: typing.Union[logging.Logger, typing.Callable]=...,
Expand Down
4 changes: 2 additions & 2 deletions logwrap/_log_wrap3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016-2017 Alexey Stepanov aka penguinolog
# Copyright 2016-2018 Alexey Stepanov aka penguinolog

# Copyright 2016 Mirantis, Inc.

Expand Down Expand Up @@ -37,7 +37,7 @@


class LogWrap(_log_wrap_shared.BaseLogWrap):
"""Python 3.3+ version of LogWrap."""
"""Python 3.4+ version of LogWrap."""

__slots__ = ()

Expand Down
3 changes: 2 additions & 1 deletion logwrap/_log_wrap3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import logging
import typing
from . import _log_wrap_shared

class LogWrap(_log_wrap_shared.BaseLogWrap): ...
class LogWrap(_log_wrap_shared.BaseLogWrap):
def _get_function_wrapper(self, func: typing.Callable) -> typing.Callable: ...

def logwrap(
log: typing.Union[logging.Logger, typing.Callable]=...,
Expand Down
14 changes: 8 additions & 6 deletions logwrap/_log_wrap_shared.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016-2017 Alexey Stepanov aka penguinolog
# Copyright 2016-2018 Alexey Stepanov aka penguinolog

# Copyright 2016 Mirantis, Inc.

Expand All @@ -24,6 +24,8 @@
import logging
import typing # noqa # pylint: disable=unused-import

import six # noqa # pylint: disable=unused-import

import logwrap as core
from . import _class_decorator

Expand All @@ -40,7 +42,7 @@
comment = "\n{spc:<{indent}}# {{kind!s}}:".format(spc='', indent=indent).format


def _check_type(expected): # type: (type) -> typing.Callable
def _check_type(expected): # type: (typing.Type) -> typing.Callable
"""Check type before assign.
:type expected: type
Expand Down Expand Up @@ -320,7 +322,7 @@ def _get_func_args_repr(
sig, # type: inspect.Signature
args, # type: typing.Tuple
kwargs # type: typing.Dict[str, typing.Any]
): # type: (...) -> str
): # type: (...) -> six.text_type
"""Internal helper for reducing complexity of decorator code.
:type sig: inspect.Signature
Expand Down Expand Up @@ -360,7 +362,7 @@ def _make_done_record(
self,
func_name, # type: str
result # type: typing.Any
):
): # type: (...) -> None
"""Construct success record.
:type func_name: str
Expand All @@ -385,7 +387,7 @@ def _make_calling_record(
name, # type: str
arguments, # type: str
method='Calling' # type: str
):
): # type: (...) -> None
"""Make log record before execution.
:type name: str
Expand All @@ -405,7 +407,7 @@ def _make_exc_record(
self,
name, # type: str
arguments # type: str
):
): # type: (...) -> None
"""Make log record if exception raised.
:type name: str
Expand Down
38 changes: 38 additions & 0 deletions logwrap/_log_wrap_shared.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import inspect
import logging
import typing

import six

from . import _class_decorator

logger: logging.Logger

def _check_type(expected: typing.Type) -> typing.Callable: ...

class BaseLogWrap(_class_decorator.BaseDecorator):
def __init__(
self,
Expand Down Expand Up @@ -60,3 +66,35 @@ class BaseLogWrap(_class_decorator.BaseDecorator):

@log_result_obj.setter
def log_result_obj(self, val: bool) -> None: ...

@property
def _logger(self) -> logging.Logger: ...

@property
def _spec(self) -> typing.Callable: ...

def _get_func_args_repr(
self,
sig: inspect.Signature,
args: typing.Tuple,
kwargs: typing.Dict[str, typing.Any]
) -> six.text_type: ...

def _make_done_record(
self,
func_name: str,
result: typing.Any
) -> None: ...

def _make_calling_record(
self,
name: str,
arguments: str,
method: str='Calling',
) -> None: ...

def _make_exc_record(
self,
name: str,
arguments: str
) -> None: ...
9 changes: 7 additions & 2 deletions logwrap/_repr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import abc
import types
import typing # noqa # pylint: disable=unused-import

Expand Down Expand Up @@ -143,6 +144,7 @@ def next_indent(self, indent, multiplier=1): # type: (int, int) -> int
"""
return indent + multiplier * self.indent_step

@abc.abstractmethod
def _repr_callable(
self,
src, # type: typing.Union[types.FunctionType, types.MethodType]
Expand All @@ -154,8 +156,9 @@ def _repr_callable(
:type indent: int
:rtype: str
"""
raise NotImplementedError # pragma: no cover
raise NotImplementedError() # pragma: no cover

@abc.abstractmethod
def _repr_simple(
self,
src, # type: typing.Any
Expand All @@ -174,6 +177,7 @@ def _repr_simple(
"""
raise NotImplementedError() # pragma: no cover

@abc.abstractmethod
def _repr_dict_items(
self,
src, # type: typing.Dict
Expand Down Expand Up @@ -236,12 +240,13 @@ def _repr_iterable_items(
) + ','

@property
@abc.abstractmethod
def _magic_method_name(self): # type: () -> six.text_type
"""Magic method name.
:rtype: str
"""
raise NotImplementedError # pragma: no cover
raise NotImplementedError() # pragma: no cover

def process_element(
self,
Expand Down
123 changes: 121 additions & 2 deletions logwrap/_repr_utils.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import abc
import types
import typing

import six
Expand All @@ -18,12 +20,129 @@ class PrettyFormat:

def next_indent(self, indent: int, multiplier: int=...) -> int: ...

@abc.abstractmethod
def _repr_callable(
self,
src: typing.Union[types.FunctionType, types.MethodType],
indent: int=0
) -> six.text_type: ...

@abc.abstractmethod
def _repr_simple(
self,
src: typing.Any,
indent: int=0,
no_indent_start: bool=False
) -> six.text_type: ...

@abc.abstractmethod
def _repr_dict_items(
self,
src: typing.Dict,
indent: int=0
) -> typing.Iterator[str]: ...

@staticmethod
def _repr_iterable_item(
nl: bool,
obj_type: str,
prefix: str,
indent: int,
result: str,
suffix: str,
) -> six.text_type: ...

def _repr_iterable_items(
self,
src: typing.Iterable,
indent: int=0
) -> typing.Iterator[str]: ...

@property
@abc.abstractmethod
def _magic_method_name(self) -> six.text_type: ...

def process_element(self, src: typing.Any, indent: int=..., no_indent_start: bool=...) -> six.text_type: ...

def __call__(self, src: typing.Any, indent: int=..., no_indent_start: bool=...) -> typing.Union[six.text_type, str]: ...

class PrettyRepr(PrettyFormat): ...
class PrettyStr(PrettyFormat): ...
class PrettyRepr(PrettyFormat):
@property
def _magic_method_name(self) -> six.text_type: ...

@staticmethod
def _strings_repr(
indent: int,
val: typing.Union[six.binary_type, six.text_type]
) -> six.text_type: ...

def _repr_simple(
self,
src: typing.Any,
indent: int=0,
no_indent_start: bool=False
) -> six.text_type: ...

def _repr_dict_items(
self,
src: typing.Dict,
indent: int=0
) -> typing.Iterator[str]: ...

def _repr_callable(
self,
src: typing.Union[types.FunctionType, types.MethodType],
indent: int=0
) -> six.text_type: ...

@staticmethod
def _repr_iterable_item(
nl: bool,
obj_type: str,
prefix: str,
indent: int,
result: str,
suffix: str,
) -> six.text_type: ...

class PrettyStr(PrettyFormat):
@property
def _magic_method_name(self) -> six.text_type: ...

@staticmethod
def _strings_str(
indent: int,
val: typing.Union[six.binary_type, six.text_type]
) -> six.text_type: ...

def _repr_simple(
self,
src: typing.Any,
indent: int=0,
no_indent_start: bool=False
) -> six.text_type: ...

def _repr_dict_items(
self,
src: typing.Dict,
indent: int=0
) -> typing.Iterator[str]: ...

def _repr_callable(
self,
src: typing.Union[types.FunctionType, types.MethodType],
indent: int=0
) -> six.text_type: ...

@staticmethod
def _repr_iterable_item(
nl: bool,
obj_type: str,
prefix: str,
indent: int,
result: str,
suffix: str,
) -> six.text_type: ...

def pretty_repr(
src: typing.Any,
Expand Down

0 comments on commit efe3e5d

Please sign in to comment.