-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parse.py
More file actions
61 lines (50 loc) · 1.61 KB
/
test_parse.py
File metadata and controls
61 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import Literal
import pytest
from parse import parse_parameters as p, NumericParameter, ChoiceParameter, InvalidParameterAnnotation
def test_no_params():
def f():
return []
assert p(f) == []
def test_three_params():
def f(a: int, b: float, c: Literal["c1", "c2"]):
return []
assert p(f) == [
NumericParameter(name="a", description="a", t=int, default=None),
NumericParameter(name="b", description="b", t=float, default=None),
ChoiceParameter(name="c", description="c", choices=["c1", "c2"], default=None),
]
def test_three_params_with_default():
def f(a: int=3, b: float=4.0, c: Literal["c1", "c2"]="c2"):
return []
assert p(f) == [
NumericParameter(name="a", description="a", t=int, default=3),
NumericParameter(name="b", description="b", t=float, default=4.0),
ChoiceParameter(name="c", description="c", choices=["c1", "c2"], default="c2"),
]
def assert_invalid(f):
with pytest.raises(InvalidParameterAnnotation):
p(f)
def test_invalid_default_int():
def f(a: int=3.0):
pass
def g(a: int="bla"):
pass
assert_invalid(f)
assert_invalid(g)
def test_invalid_default_float():
def f(a: float=3):
pass
def g(a: float="bla"):
pass
assert_invalid(f)
assert_invalid(g)
def test_invalid_default_choice():
def f(a: Literal["x", "y"]=3):
pass
def g(a: Literal["x", "y"]=3.0):
pass
def h(a: Literal["x", "y"]="a"):
pass
assert_invalid(f)
assert_invalid(g)
assert_invalid(h)