Skip to content

Commit dab3a1f

Browse files
authored
Merge pull request #224 from joe733/workshop
maint: follows google's python style guide for docstrings
2 parents 899dd78 + 79f5f79 commit dab3a1f

File tree

6 files changed

+78
-28
lines changed

6 files changed

+78
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ True
2020
- [Issue Tracker](http://github.com/kvesteri/validators/issues)
2121
- [Code](http://github.com/kvesteri/validators/)
2222

23-
[bs-badge]: https://github.com/kvesteri/validators/workflows/GH/badge.svg
24-
[bs-link]: https://github.com/kvesteri/validators/actions/workflows/main.yml
23+
[bs-badge]: https://github.com/python-validators/validators/actions/workflows/main.yml/badge.svg
24+
[bs-link]: https://github.com/python-validators/validators/actions/workflows/main.yml
2525
[vs-badge]: https://img.shields.io/pypi/v/validators.svg
2626
[vs-link]: https://pypi.python.org/pypi/validators/
2727
[dw-badge]: https://img.shields.io/pypi/dm/validators.svg

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Resources
2626
- `Code <http://github.com/kvesteri/validators/>`_
2727

2828

29-
.. |Build Status| image:: https://github.com/kvesteri/validators/workflows/GH/badge.svg
30-
:target: https://github.com/kvesteri/validators/actions/workflows/main.yml
29+
.. |Build Status| image:: https://github.com/python-validators/validators/actions/workflows/main.yml/badge.svg
30+
:target: https://github.com/python-validators/validators/actions/workflows/main.yml
3131
.. |Version Status| image:: https://img.shields.io/pypi/v/validators.svg
3232
:target: https://pypi.python.org/pypi/validators/
3333
.. |Downloads| image:: https://img.shields.io/pypi/dm/validators.svg

poetry.lock

Lines changed: 47 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ python = "^3.9"
3131
bandit = "^1.7.4"
3232
black = "^23.1.0"
3333
flake8 = "^6.0.0"
34+
flake8-docstrings = "^1.7.0"
3435
pyright = "^1.1.293"
3536
pytest = "^7.2.1"
3637
setuptools = "^67.2.0"

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
[flake8]
55
max-line-length = 100
6+
docstring-convention = google
67

78
# [isort]
89
# known_first_party = sqlalchemy_utils,tests

validators/utils.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""
2-
Utils.py
3-
--------
4-
"""
1+
"""Utils."""
52
# -*- coding: utf-8 -*-
63

74
# standard
@@ -11,37 +8,31 @@
118

129

1310
class ValidationFailure(Exception):
14-
"""
15-
Exception class when validation failure occurs
16-
"""
11+
"""Exception class when validation failure occurs."""
1712

1813
def __init__(self, function: Callable[..., Any], arg_dict: Dict[str, Any]):
14+
"""Initialize Validation Failure."""
1915
self.func = function
2016
self.__dict__.update(arg_dict)
2117

2218
def __repr__(self) -> str:
19+
"""Repr Validation Failure."""
2320
return (
2421
f"ValidationFailure(func={self.func.__name__}, "
2522
+ f"args={({k: v for (k, v) in self.__dict__.items() if k != 'func'})})"
2623
)
2724

2825
def __str__(self) -> str:
26+
"""Str Validation Failure."""
2927
return repr(self)
3028

3129
def __bool__(self) -> Literal[False]:
30+
"""Bool Validation Failure."""
3231
return False
3332

3433

3534
def _func_args_as_dict(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Dict[str, Any]:
36-
"""
37-
Return given function's positional and key value arguments as an ordered
38-
dictionary.
39-
40-
:param func: function to decorate
41-
:param args: positional function arguments
42-
:param kwargs: key value function arguments
43-
"""
44-
35+
"""Return function's positional and key value arguments as an ordered dictionary."""
4536
# TODO: find more efficient way to do it
4637
return dict(
4738
list(zip(dict.fromkeys(chain(getfullargspec(func)[0], kwargs.keys())), args))
@@ -50,8 +41,7 @@ def _func_args_as_dict(func: Callable[..., Any], *args: Any, **kwargs: Any) -> D
5041

5142

5243
def validator(func: Callable[..., Any]) -> Callable[..., Union[Literal[True], ValidationFailure]]:
53-
"""
54-
A decorator that makes given function validator.
44+
"""A decorator that makes given function validator.
5545
5646
Whenever the given function is called and returns ``False`` value
5747
this decorator returns :class:`ValidationFailure` object.
@@ -63,14 +53,16 @@ def validator(func: Callable[..., Any]) -> Callable[..., Union[Literal[True], Va
6353
... return not (value % 2)
6454
6555
>>> even(4)
66-
True
56+
# Output: True
6757
6858
>>> even(5)
69-
ValidationFailure(func=even, args={'value': 5})
59+
# Output: ValidationFailure(func=even, args={'value': 5})
60+
61+
Args:
62+
`func`: function which is to be decorated.
7063
71-
:param func: function to decorate
72-
:param args: positional function arguments
73-
:param kwargs: key value function arguments
64+
Returns:
65+
Wrapper function as a decorator.
7466
"""
7567

7668
def wrapper(*args: Any, **kwargs: Any) -> Union[Literal[True], ValidationFailure]:
@@ -79,5 +71,15 @@ def wrapper(*args: Any, **kwargs: Any) -> Union[Literal[True], ValidationFailure
7971
if func(*args, **kwargs)
8072
else ValidationFailure(func, _func_args_as_dict(func, *args, **kwargs))
8173
)
74+
# try:
75+
# return (
76+
# True
77+
# if func(*args, **kwargs)
78+
# else ValidationFailure(func, _func_args_as_dict(func, *args, **kwargs))
79+
# )
80+
# except (AssertionError, TypeError) as err:
81+
# print(err)
82+
# finally:
83+
# return ValidationFailure(func, _func_args_as_dict(func, *args, **kwargs))
8284

8385
return wrapper

0 commit comments

Comments
 (0)