Skip to content

Commit 48b4246

Browse files
committed
Fixed the order of Option arguments
1 parent 7b8254a commit 48b4246

File tree

7 files changed

+57
-61
lines changed

7 files changed

+57
-61
lines changed

README.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,38 @@ An option manager object can then be obtained by instantiating the option manage
5858
class MyOptions(OptionManager):
5959
""" Options of some library. """
6060
61-
validate = Option(True, bool)
61+
validate = Option(bool, True)
6262
""" Whether to validate arguments to functions and methods. """
6363
64-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
64+
eq_atol = Option(float, 1e-8, lambda x: x >= 0)
6565
""" Absolute tolerance used for equality comparisons."""
6666
6767
scaling: Option(
68-
{"x": 1.0, "y": 2.0, "z": 1.0},
6968
Mapping[Literal["x", "y", "z"], float],
69+
{"x": 1.0, "y": 2.0, "z": 1.0},
7070
lambda scaling: all(v >= 0 for v in scaling.values())
7171
)
7272
""" Scaling for coordinate axes used in plots. """
7373
7474
options = MyOptions()
7575
76-
7776
Each option takes a default value, a type, and an optional validator function:
7877

7978
.. code-block:: python
8079
81-
validate = Option(True, bool)
82-
# default value ^^^^ ^^^^ option type
80+
validate = Option(bool, True)
81+
# option type ^^^^ ^^^^ default value
8382
84-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
83+
eq_atol = Option(float, 1e-8, lambda x: x >= 0)
8584
# optional validator ^^^^^^^^^^^^^^^^
8685
8786
Any type supported by the `typing-validation <https://github.com/hashberg-io/typing-validation>`_ library can be used for options, including `PEP 484 <https://peps.python.org/pep-0484/>`_ type hints:
8887

8988
.. code-block:: python
9089
9190
scaling: Option(
92-
{"x": 1.0, "y": 2.0, "z": 1.0},
9391
Mapping[Literal["x", "y", "z"], float], # <- type hints supported
92+
{"x": 1.0, "y": 2.0, "z": 1.0},
9493
lambda scaling: all(v >= 0 for v in scaling.values())
9594
)
9695
@@ -120,7 +119,7 @@ It is also possible to use the options object as a context manager, for temporar
120119
print(options.validate) # True
121120
print(options.eq_atol) # 0.00000001
122121
123-
All options can be reset to their default values by using the `reset <https://optmanage.readthedocs.io/en/latest/api/optmanage.manager.html#optmanage.manager.OptionManager.reset>`_ method of the ``options`` object:
122+
All options can be reset to their default values by using the `OptionManager.reset <https://optmanage.readthedocs.io/en/latest/api/optmanage.manager.html#optmanage.manager.OptionManager.reset>`_ method of the ``options`` object:
124123

125124
.. code-block:: python
126125
@@ -131,7 +130,7 @@ All options can be reset to their default values by using the `reset <https://op
131130
print(options.validate) # True
132131
print(options.eq_atol) # 0.00000001
133132
134-
An individual option can be reset to its default value by using the `reset <https://optmanage.readthedocs.io/en/latest/api/optmanage.option.html#optmanage.option.Option.reset>`_ method of the `Option <https://optmanage.readthedocs.io/en/latest/api/optmanage.option.html#option>`_ object, accessed from the option manager class:
133+
An individual option can be reset to its default value by using the `Option.reset <https://optmanage.readthedocs.io/en/latest/api/optmanage.option.html#optmanage.option.Option.reset>`_ method of the `Option <https://optmanage.readthedocs.io/en/latest/api/optmanage.option.html#option>`_ object, accessed from the option manager class:
135134

136135
.. code-block:: python
137136

docs/getting-started.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ An option manager object can then be obtained by instantiating the option manage
1919
class MyOptions(OptionManager):
2020
""" Options of some library. """
2121
22-
validate = Option(True, bool)
22+
validate = Option(bool, True)
2323
""" Whether to validate arguments to functions and methods. """
2424
25-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
25+
eq_atol = Option(float, 1e-8, lambda x: x >= 0)
2626
""" Absolute tolerance used for equality comparisons."""
2727
2828
scaling: Option(
29-
{"x": 1.0, "y": 2.0, "z": 1.0},
3029
Mapping[Literal["x", "y", "z"], float],
30+
{"x": 1.0, "y": 2.0, "z": 1.0},
3131
lambda scaling: all(v >= 0 for v in scaling.values())
3232
)
3333
""" Scaling for coordinate axes used in plots. """
@@ -39,19 +39,19 @@ Each option takes a default value, a type, and an optional validator function:
3939

4040
.. code-block:: python
4141
42-
validate = Option(True, bool)
43-
# default value ^^^^ ^^^^ option type
42+
validate = Option(bool, True)
43+
# option type ^^^^ ^^^^ default value
4444
45-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
45+
eq_atol = Option(float, 1e-8, lambda x: x >= 0)
4646
# optional validator ^^^^^^^^^^^^^^^^
4747
4848
Any type supported by the `typing-validation <https://github.com/hashberg-io/typing-validation>`_ library can be used for options, including `PEP 484 <https://peps.python.org/pep-0484/>`_ type hints:
4949

5050
.. code-block:: python
5151
5252
scaling: Option(
53-
{"x": 1.0, "y": 2.0, "z": 1.0},
5453
Mapping[Literal["x", "y", "z"], float], # <- type hints supported
54+
{"x": 1.0, "y": 2.0, "z": 1.0},
5555
lambda scaling: all(v >= 0 for v in scaling.values())
5656
)
5757

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ You can install the latest release from `PyPI <https://pypi.org/project/optmanag
1717
class MyOptions(OptionManager):
1818
""" Options of some library. """
1919
20-
validate = Option(True, bool)
20+
validate = Option(bool, True)
2121
""" Whether to validate arguments to functions and methods. """
2222
23-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
23+
eq_atol = Option(float, 1e-8, lambda x: x >= 0)
2424
""" Absolute tolerance used for equality comparisons."""
2525
2626
options = MyOptions()

optmanage/manager.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ class OptionManager:
2929
class MyOptions(OptionManager):
3030
''' Options of some library. '''
3131
32-
validate = Option(True, bool)
32+
validate = Option(bool, True)
3333
''' Whether to validate arguments to functions and methods. '''
3434
35-
eq_atol = Option(1e-08, float, lambda x: x >= 0)
35+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
3636
''' Absolute tolerance used for equality comparisons.'''
3737
38-
print_prec = Option(3, int, lambda x: x >= 0)
38+
print_prec = Option(int, 3, lambda x: x >= 0)
3939
''' Number of decimal digits to be displayed when printing. '''
4040
4141
Each option is defined as a class attribute of type :class:`Option`,
4242
passing a default value, a type, and optionally a validator function:
4343
4444
.. code-block:: python
4545
46-
validate = Option(True, bool)
47-
# default value ^^^^ ^^^^ option type
46+
validate = Option(bool, True)
47+
# option type ^^^^ ^^^^ default value
4848
49-
print_prec = Option(3, int, lambda x: x >= 0)
49+
print_prec = Option(int, 3, lambda x: x >= 0)
5050
# optional validator ^^^^^^^^^^^^^^^^
5151
5252
The option manager can then be instantiated as usual:

optmanage/option.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,23 @@ class Option(Generic[ValueT]):
7474

7575
def __new__(
7676
cls,
77+
ty: Any,
7778
default: ValueT,
78-
ty: Any = None,
7979
validator: Validator[ValueT] | None = None,
8080
) -> Self:
8181
"""
8282
Creates a new option descriptor.
8383
For usage examples, see :class:`OptionManager`.
8484
85+
:param ty: The type of the option.
8586
:param default: The default value for the option.
86-
:param ty: The type of the option. If not specified, defaults to the
87-
type of the default value.
8887
:param validator: A callable that takes a value and returns whether
8988
it is valid for this option. If not specified,
9089
defaults to :obj:`None` (no validation)
9190
9291
:meta public:
9392
"""
94-
if ty is None:
95-
ty = type(default)
96-
elif not can_validate(ty):
93+
if not can_validate(ty):
9794
raise TypeError(f"Cannot validate type {ty!r}.")
9895
if validator is not None and not callable(validator):
9996
raise TypeError(f"Expected callable validator, got {validator!r}.")

test/test_00_option.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from optmanage.option import Option
1010

1111
def test_option_attr_1() -> None:
12-
opt = Option([1.5, 2.], Sequence[float])
12+
opt = Option(Sequence[float], [1.5, 2.])
1313
assert opt.default == [1.5, 2.]
1414
assert opt.type == Sequence[float]
1515
assert opt.validator is None
@@ -19,28 +19,28 @@ def test_option_attr_1() -> None:
1919
opt.owner # pylint: disable = pointless-statement
2020

2121
def test_option_validation_1() -> None:
22-
opt = Option([1.5, 2.], Sequence[float])
22+
opt = Option(Sequence[float], [1.5, 2.])
2323
opt.validate([1e-8])
2424
with pytest.raises(TypeError):
2525
opt.validate(1e-8) # type: ignore
2626
with pytest.raises(TypeError):
27-
Option(1e-8, Sequence[float])
27+
Option(Sequence[float], 1e-8)
2828

2929
def test_option_attr_2() -> None:
3030
validator = lambda x: x>= 0
31-
opt = Option(1e8, float, validator)
31+
opt = Option(float, 1e8, validator)
3232
assert opt.validator == validator
3333

3434
def test_option_validation_2() -> None:
35-
opt = Option(1e8, float, lambda x: x>= 0)
35+
opt = Option(float, 1e8, lambda x: x>= 0)
3636
with pytest.raises(ValueError):
3737
opt.validate(-1.5)
3838
with pytest.raises(ValueError):
39-
Option(-1.5, float, lambda x: x>= 0)
39+
Option(float, -1.5, lambda x: x>= 0)
4040

4141
def test_option_assignment() -> None:
4242
class MyOptions(OptionManager):
43-
validate = Option(True, bool)
43+
validate = Option(bool, True)
4444
opt = MyOptions.validate
4545
assert opt.name == "validate"
4646
assert opt.owner == MyOptions

test/test_01_manager.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
def test_init() -> None:
1414
class MyOptions(OptionManager):
15-
validate = Option(True, bool)
16-
eq_atol = Option(1e-08, float, lambda x: x >= 0)
15+
validate = Option(bool, True)
16+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
1717
options = MyOptions()
1818
keys = ["validate", "eq_atol"]
1919
values = [True, 1e-8]
@@ -24,16 +24,16 @@ class MyOptions(OptionManager):
2424

2525
def test_get_default() -> None:
2626
class MyOptions(OptionManager):
27-
validate = Option(True, bool)
28-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
27+
validate = Option(bool, True)
28+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
2929
options = MyOptions()
3030
assert options.validate is True
3131
assert options.eq_atol == 1e-8
3232

3333
def test_option_set() -> None:
3434
class MyOptions(OptionManager):
35-
validate = Option(True, bool)
36-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
35+
validate = Option(bool, True)
36+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
3737
options = MyOptions()
3838
options.validate = False
3939
assert options.validate is False
@@ -42,8 +42,8 @@ class MyOptions(OptionManager):
4242

4343
def test_option_set_failure() -> None:
4444
class MyOptions(OptionManager):
45-
validate = Option(True, bool)
46-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
45+
validate = Option(bool, True)
46+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
4747
options = MyOptions()
4848
with pytest.raises(TypeError):
4949
options.validate = 2 # type: ignore
@@ -52,8 +52,8 @@ class MyOptions(OptionManager):
5252

5353
def test_set() -> None:
5454
class MyOptions(OptionManager):
55-
validate = Option(True, bool)
56-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
55+
validate = Option(bool, True)
56+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
5757
options = MyOptions()
5858
options.set(validate=False, eq_atol=1.5)
5959
assert options.validate is False
@@ -71,17 +71,17 @@ class MyOptions(OptionManager):
7171
@pytest.mark.parametrize(["kwargs", "error"], set_failures)
7272
def test_set_failure(kwargs: Mapping[str, Any], error: Type[Exception]) -> None:
7373
class MyOptions(OptionManager):
74-
validate = Option(True, bool)
75-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
74+
validate = Option(bool, True)
75+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
7676
options = MyOptions()
7777
with pytest.raises(error):
7878
options.set(**kwargs)
7979
assert options.validate is True and options.eq_atol == 1e-8
8080

8181
def test_option_reset() -> None:
8282
class MyOptions(OptionManager):
83-
validate = Option(True, bool)
84-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
83+
validate = Option(bool, True)
84+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
8585
options = MyOptions()
8686
options.validate = False
8787
MyOptions.validate.reset(options)
@@ -92,8 +92,8 @@ class MyOptions(OptionManager):
9292

9393
def test_reset() -> None:
9494
class MyOptions(OptionManager):
95-
validate = Option(True, bool)
96-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
95+
validate = Option(bool, True)
96+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
9797
options = MyOptions()
9898
options.validate = False
9999
options.eq_atol = 1.5
@@ -103,8 +103,8 @@ class MyOptions(OptionManager):
103103

104104
def test_temp_set() -> None:
105105
class MyOptions(OptionManager):
106-
validate = Option(True, bool)
107-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
106+
validate = Option(bool, True)
107+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
108108
options = MyOptions()
109109
with options(validate=False, eq_atol=1.5):
110110
assert options.validate is False
@@ -115,8 +115,8 @@ class MyOptions(OptionManager):
115115
@pytest.mark.parametrize(["kwargs", "error"], set_failures)
116116
def test_temp_set_failure(kwargs: Mapping[str, Any], error: Type[Exception]) -> None:
117117
class MyOptions(OptionManager):
118-
validate = Option(True, bool)
119-
eq_atol = Option(1e-8, float, lambda x: x >= 0)
118+
validate = Option(bool, True)
119+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
120120
options = MyOptions()
121121
with pytest.raises(error):
122122
with options(**kwargs):
@@ -128,15 +128,15 @@ def test_getting_started_example() -> None:
128128
class MyOptions(OptionManager):
129129
""" Options of some library. """
130130

131-
validate = Option(True, bool)
131+
validate = Option(bool, True)
132132
""" Whether to validate arguments to functions and methods. """
133133

134-
eq_atol = Option(1e-08, float, lambda x: x >= 0)
134+
eq_atol = Option(float, 1e-08, lambda x: x >= 0)
135135
""" Absolute tolerance used for equality comparisons."""
136136

137137
scaling = Option(
138-
{"x": 1.0, "y": 2.0, "z": 1.0},
139138
Mapping[Literal["x", "y", "z"], float],
139+
{"x": 1.0, "y": 2.0, "z": 1.0},
140140
lambda scaling: all(v >= 0 for v in scaling.values())
141141
)
142142
""" Scaling for coordinate axes used in plots. """

0 commit comments

Comments
 (0)