Skip to content

Commit 550e352

Browse files
authored
Merge pull request #5 from d-chris/develop
bugfix for clicktypes.url
2 parents 1c55010 + ed1ae07 commit 550e352

File tree

10 files changed

+49
-47
lines changed

10 files changed

+49
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
docs/public/
2+
.vscode/**
23

34
# Byte-compiled / optimized / DLL files
45
__pycache__/

clicktypes/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ def url(
504504
505505
Returns a `click.ParamType` instance which wraps `validators.url`.
506506
"""
507-
return click_validatortype(validators.url)(**locals())
507+
508+
params = {k: v for k, v in locals().items() if k != "kwargs"}
509+
return click_validatortype(validators.url)(**params, **kwargs)
508510

509511

510512
def uuid() -> click.ParamType:

clicktypes/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import inspect
22
from pathlib import Path
3-
from typing import Any, Callable, Dict, List
3+
from typing import Any
4+
from typing import Callable
5+
from typing import Dict
6+
from typing import List
47

58
import black
69
import jinja2

clicktypes/decorator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
2-
from typing import Callable, Type
2+
from typing import Callable
3+
from typing import Type
34

45
import click
56

docs/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def main() -> int:
3434

3535
try:
3636
from pathlibutil import Path
37-
from pdoc import pdoc, render
37+
from pdoc import pdoc
38+
from pdoc import render
3839

3940
with Path(__file__).parent as cwd:
4041
print(f"\nrunning docs in {cwd=}...")

poetry.lock

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

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ exclude = [ "clicktypes/__main__.py" ]
1313
keywords = [ "click", "validators" ]
1414
license = "MIT"
1515
classifiers = [
16-
"Programming Language :: Python :: 3.8",
1716
"Programming Language :: Python :: 3.9",
1817
"Programming Language :: Python :: 3.10",
1918
"Programming Language :: Python :: 3.11",
@@ -62,6 +61,8 @@ priority = "explicit"
6261

6362
[tool.isort]
6463
profile = "black"
64+
force_single_line = true
65+
line_length = 88
6566

6667
[tool.pytest.ini_options]
6768
minversion = "6.0"

templates/__init__.py.jinja2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ def {{ validator.name }}(
2121

2222
Returns a `click.ParamType` instance which wraps `validators.{{ validator.name }}`.
2323
"""
24+
{% if "**kwargs" in validator.params %}
25+
params = {k: v for k, v in locals().items() if k != "kwargs"}
26+
return click_validatortype(validators.{{ validator.name }})(**params, **kwargs)
27+
{% else -%}
2428
return click_validatortype(validators.{{ validator.name }})(**locals())
29+
{% endif %}
2530
{% else %}
2631
def {{ validator.name }}() -> click.ParamType:
2732
"""

tests/__init__.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import ast
12
import inspect
23
import re
3-
from typing import Generator, Tuple
4+
from typing import Generator
5+
from typing import Tuple
46

57
import validators
68

@@ -13,50 +15,35 @@ def docstrings() -> Generator[str, None, None]:
1315
yield from (
1416
inspect.getdoc(getattr(validators, validator)) or ""
1517
for validator in clicktypes.__all__
16-
if validator not in ("base58", "country_code")
1718
)
1819

1920

2021
def testdata(doc: str) -> Generator[Tuple[str, str, int], None, None]:
2122

23+
if "ind_pan" in doc:
24+
pass
25+
26+
def parse_call(s: str):
27+
tree = ast.parse(s.strip(), mode="eval")
28+
if not isinstance(tree.body, ast.Call):
29+
raise ValueError("Not a function call")
30+
func_name = (
31+
ast.unparse(tree.body.func)
32+
if hasattr(ast, "unparse")
33+
else tree.body.func.id # type: ignore[attr-defined]
34+
)
35+
args = [ast.literal_eval(arg) for arg in tree.body.args]
36+
kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in tree.body.keywords}
37+
return func_name, tuple(args), kwargs
38+
2239
for match in re.findall(
23-
r">>> (\w+)\(\'(.*?)\'\)$\s+# \w+: (.*?)$",
40+
r">>>\s*(\w+\(.*?\))$\s+(.*?)$",
2441
doc,
2542
re.MULTILINE,
2643
):
27-
yield (match[0], match[1], 0 if match[2] == "True" else 2)
44+
yield (*parse_call(match[0]), 0 if match[1] == "True" else 2)
2845

2946

3047
def validator_data():
3148
for doc in docstrings():
32-
for validator, value, expected in testdata(doc):
33-
yield (validator, value, {}, expected)
34-
35-
for data, result in [
36-
("cUSECaVvAiV3srWbFRvVPzm5YzcXJwPSwZfE7veYPHoXmR9h6YMQ", 0),
37-
("18KToMF5ckjXBYt2HAj77qsG3GPeej3PZn", 0),
38-
("n4FFXRNNEW1aA2WPscSuzHTCjzjs4TVE2Z", 0),
39-
("38XzQ9dPGb1uqbZsjPtUajp7omy8aefjqj", 0),
40-
("ThisIsAReallyLongStringThatIsDefinitelyNotBase58Encoded", 2),
41-
("abcABC!@#", 2),
42-
("InvalidBase58!", 2),
43-
]:
44-
yield ("base58", data, {}, result)
45-
46-
for data, kwarg, expected in [
47-
("ISR", "auto", 0),
48-
("US", "alpha2", 0),
49-
("USA", "alpha3", 0),
50-
("840", "numeric", 0),
51-
("", "auto", 2),
52-
("123456", "auto", 2),
53-
("XY", "alpha2", 2),
54-
("PPP", "alpha3", 2),
55-
("123", "numeric", 2),
56-
("us", "auto", 2),
57-
("uSa", "auto", 2),
58-
("US ", "auto", 2),
59-
("U.S", "auto", 2),
60-
("1ND", "unknown", 2),
61-
]:
62-
yield ("country_code", data, {"iso_format": kwarg}, expected)
49+
yield from testdata(doc)

tests/test_validator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ def test_none(validator):
4141
sorted(
4242
validator_data(),
4343
),
44+
ids=repr,
4445
)
4546
def test_dataset(validator, value, kwargs, expected):
4647

4748
runner = CliRunner()
48-
result = runner.invoke(cli(validator, **kwargs), [value])
49+
result = runner.invoke(cli(validator, **kwargs), value)
4950
assert result.exit_code == expected

0 commit comments

Comments
 (0)