Skip to content

Commit 2fb85bb

Browse files
authored
Merge pull request #334 from yozachar/workshop
maint: fix `between` & `length` validators
2 parents 46eb1b8 + d1a5b22 commit 2fb85bb

File tree

12 files changed

+32
-56
lines changed

12 files changed

+32
-56
lines changed

.github/workflows/docs.yaml.bkp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
pip install .
2828
pip install -r package/requirements.mkdocs.txt
2929
- name: Build documentation
30-
run: python src/export docs
30+
run: python package/export docs
3131
# set up Pages
3232
- name: Set up Pages
3333
uses: actions/configure-pages@v3

.github/workflows/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- name: Build package
3535
run: |
3636
. ./.venv/bin/activate
37-
python src/export package
37+
python package/export pkg
3838
deactivate
3939
# upload package as artifact
4040
- name: Upload artifact

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ include .gitignore
77
# global-include
88

99
recursive-include tests *
10-
recursive-include src/export *
1110

1211
# graft
1312

@@ -16,6 +15,7 @@ recursive-include src/export *
1615
# global-exclude
1716

1817
recursive-exclude docs *.rst
18+
recursive-exclude docs/references *.md
1919

2020
prune docs/_build
2121
prune **/__pycache__

mkdocs.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,3 @@ copyright: Copyright © 2013 - 2024 Konsta Vesterinen
6464

6565
nav:
6666
- Home: index.md
67-
- API:
68-
- references/between.md
69-
- references/btc_address.md
70-
- references/card.md
71-
- references/country_code.md
72-
- references/domain.md
73-
- references/email.md
74-
- references/hashes.md
75-
- references/hostname.md
76-
- references/i18n.md
77-
- references/iban.md
78-
- references/ip_address.md
79-
- references/length.md
80-
- references/mac_address.md
81-
- references/slug.md
82-
- references/url.md
83-
- references/utils.md
84-
- references/uuid.md
File renamed without changes.

src/export/__main__.py renamed to package/export/__main__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def _parse_package(source: Path):
4040

4141
def _generate_reference(source: Path, destination: Path, ext: str):
4242
"""Generate reference."""
43-
nav_items: Dict[str, List[str]] = {"Code Reference": []}
43+
nav_items: Dict[str, List[str]] = {"API": []}
4444
# generate reference content
4545
for module_name, aliases in _parse_package(source):
4646
for alias in aliases:
4747
_write_ref_content(destination / f"{module_name}.{ext}", module_name, alias.name)
4848
if ext == "md":
49-
nav_items["Code Reference"].append(f"references/{module_name}.md")
49+
nav_items["API"].append(f"references/{module_name}.md")
5050
return nav_items
5151

5252

@@ -65,6 +65,9 @@ def _update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str,
6565

6666
def _gen_md_docs(source: Path, refs_path: Path):
6767
"""Generate Markdown docs."""
68+
# remove existing markdown files
69+
for md_files in (source / "docs/references").glob("*.md"):
70+
md_files.unlink()
6871
nav_items = _generate_reference(source / "src/validators/__init__.py", refs_path, "md")
6972
# backup mkdocs config
7073
_update_mkdocs_config(source / "mkdocs.yaml", source / "mkdocs.bak.yaml", nav_items)
@@ -166,7 +169,7 @@ def package(source: Path):
166169
if len(argv) != 2:
167170
quit(exit_code)
168171

169-
if argv[1] == "package":
172+
if argv[1] == "pkg":
170173
exit_code = package(project_root)
171174
if argv[1] == "docs":
172175
exit_code = generate_documentation(

package/roll.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ if ($IsLinux || $IsMacOS) {
4646
. $venv_dir\$bin_path\Activate.ps1
4747

4848
# Run export script
49-
python src/export package
49+
python package/export pkg
5050

5151
# Deactivate virtual environment
5252
deactivate

package/roll.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $venv_dir/bin/pip install build
4040
. $venv_dir/bin/activate
4141

4242
# Run export script
43-
python src/export package
43+
python package/export pkg
4444

4545
# Deactivate virtual environment
4646
deactivate

src/validators/between.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,42 +57,25 @@ def between(
5757
If `value` is not in between the given conditions.
5858
5959
Raises:
60-
ValueError: If both `min_val` and `max_val` are `None`,
61-
or if `min_val` is greater than `max_val`.
62-
TypeError: If there's a type mismatch before comparison.
60+
(ValueError): If `min_val` is greater than `max_val`.
61+
(TypeError): If there's a type mismatch during comparison.
6362
6463
Note:
6564
- `PossibleValueTypes` = `TypeVar("PossibleValueTypes", int, float, str, datetime)`
66-
- Either one of `min_val` or `max_val` must be provided.
67-
68-
> *New in version 0.2.0*.
65+
- If neither `min_val` nor `max_val` is provided, result will always be `True`.
6966
"""
7067
if value is None:
7168
return False
7269

73-
if min_val is max_val is None:
74-
raise ValueError("At least one of either `min_val` or `max_val` must be specified")
75-
7670
if max_val is None:
7771
max_val = AbsMax()
7872
if min_val is None:
7973
min_val = AbsMin()
8074

81-
if isinstance(min_val, AbsMin):
82-
if type(value) is type(max_val):
83-
return min_val <= value <= max_val
84-
raise TypeError("`value` and `max_val` must be of same type")
85-
86-
if isinstance(max_val, AbsMax):
87-
if type(value) is type(min_val):
88-
return min_val <= value <= max_val
89-
raise TypeError("`value` and `min_val` must be of same type")
90-
91-
if type(min_val) is type(max_val):
75+
try:
9276
if min_val > max_val:
93-
raise ValueError("`min_val` cannot be more than `max_val`")
94-
if type(value) is type(min_val): # or is type(max_val)
95-
return min_val <= value <= max_val
96-
raise TypeError("`value` and (`min_val` or `max_val`) must be of same type")
77+
raise ValueError("`min_val` cannot be greater than `max_val`")
78+
except TypeError as err:
79+
raise TypeError("Comparison type mismatch") from err
9780

98-
raise TypeError("`value` and `min_val` and `max_val` must be of same type")
81+
return min_val <= value <= max_val

src/validators/length.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""Length."""
22

3+
# standard
4+
from typing import Union
5+
36
# local
47
from .between import between
58
from .utils import validator
69

710

811
@validator
9-
def length(value: str, /, *, min_val: int = 0, max_val: int = 0):
12+
def length(value: str, /, *, min_val: Union[int, None] = None, max_val: Union[int, None] = None):
1013
"""Return whether or not the length of given string is within a specified range.
1114
1215
Examples:
@@ -33,6 +36,12 @@ def length(value: str, /, *, min_val: int = 0, max_val: int = 0):
3336
(ValidationError):
3437
If `len(value)` is not in between the given conditions.
3538
36-
> *New in version 0.2.0*.
39+
Raises:
40+
(ValueError): If either `min_val` or `max_val` is negative.
3741
"""
38-
return between(len(value), min_val=min_val, max_val=max_val) if value else False
42+
if min_val is not None and min_val < 0:
43+
raise ValueError("Length cannot be negative. `min_val` is less than zero.")
44+
if max_val is not None and max_val < 0:
45+
raise ValueError("Length cannot be negative. `max_val` is less than zero.")
46+
47+
return bool(between(len(value), min_val=min_val, max_val=max_val))

tests/test_between.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T):
2828
(None, 13, 14),
2929
(12, 13, 14),
3030
(12, None, 11),
31-
(12, None, None),
3231
(12, 13, None),
3332
(12, "13.5", datetime(1970, 1, 1)),
3433
("12", 20.5, "None"),

tests/test_length.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@pytest.mark.parametrize(
1111
("value", "min_val", "max_val"),
12-
[("password", 2, 10), ("password", 0, 10), ("password", 8, 8)],
12+
[("password", 2, None), ("password", None, None), ("password", 0, 10), ("password", 8, 8)],
1313
)
1414
def test_returns_true_on_valid_length(value: str, min_val: int, max_val: int):
1515
"""Test returns true on valid length."""

0 commit comments

Comments
 (0)