Skip to content

Commit 241be47

Browse files
author
Sylvain MARIE
committed
Updated documentation for clarity.
1 parent 0835d72 commit 241be47

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

docs/index.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,46 @@
2222
A `VType` is a combination of :
2323

2424
- a type **name**, for example `'PositiveInt'`
25-
- one or several **base types**: for example `int`.
25+
- one or several **base types**: for example `int`. When several are provided, they all should match ("*and*" combination).
2626
- one or several **validators**: for example `lambda x: x >= 0`
2727

28-
For example we can create a positive int:
28+
A `VType` is constructed using `vtype(name, type, validators, ...)`. For example we can create a positive int:
2929

3030
```python
3131
from vtypes import vtype
32-
3332
PositiveInt = vtype('PositiveInt', int, {'should be positive': lambda x: x >= 0})
3433
```
3534

3635
A `VType`'s main purpose is to behave like a type (therefore to be compliant with `isinstance`) and to validate both type and values at the same time when `isinstance` is called:
3736

3837
```python
3938
assert isinstance(1, PositiveInt)
40-
assert not isinstance(-1, PositiveInt)
39+
assert not isinstance(-1, PositiveInt) # an int, but not >= 0
4140
```
4241

42+
An alternate way to define a `VType` is to define a python class inheriting from `VType`.
43+
44+
- **base types** can be either provided as superclass, or in the `__type__` class attribute,
45+
46+
- **validators** can be provided in the `__validators__` class attribute.
47+
48+
49+
So the following two classes are equivalent to our previous `PositiveInt` example:
50+
51+
```python
52+
from vtypes import VType
53+
54+
# type is provided in the ancestors
55+
class PositiveInt(int, VType):
56+
__validators__ = {'should be positive': lambda x: x >= 0}
57+
58+
# type is provided in __type__
59+
class PositiveInt2(VType):
60+
__type__ = int
61+
__validators__ = {'should be positive': lambda x: x >= 0}
62+
```
63+
64+
4365
### b - goodies
4466

4567
In addition to this primary feature, a `VType` provides a few handy methods:
@@ -95,7 +117,9 @@ If you wish to create even more compact callables, you may wish to look at [`min
95117

96118
### d - composition
97119

98-
You can combine types, for example a nonempty string can be obtained by mixing `NonEmpty` and `str`:
120+
You can combine types, for example a nonempty string can be obtained by mixing `NonEmpty` and `str`.
121+
122+
- *with the compact style*: simply put the classes to combine in a tuple in the second `vtype` argument:
99123

100124
```python
101125
NonEmpty = vtype('NonEmpty', (), {'should be non empty': lambda x: len(x) > 0})
@@ -105,33 +129,22 @@ NonEmptyStr = vtype('NonEmptyStr', (NonEmpty, str), ())
105129
"""A VType for non-empty strings"""
106130
```
107131

108-
109-
### e - alternate coding style
110-
111-
An alternate way to define `VType`s is to define a python class inheriting from `VType`.
112-
113-
- the validators can be provided as a class member named `__validators__`
114-
115-
- the base type(s) can be either provided as superclass(es), or as a class member named `__type__`.
116-
117-
This provides an alternate style that developers might find handy in particular for entering docstrings and for making `VTypes` composition appear "just like normal python inheritance".
132+
- *with the class style*: either use inheritance, or fill the `__type__` class attribute with a tuple
118133

119134
```python
120-
from vtypes import VType
121-
122135
class NonEmpty(VType):
123136
"""A VType describing non-empty containers, with strictly positive length."""
124137
__validators__ = {'should be non empty': lambda x: len(x) > 0}
125138

126139
class NonEmptyStr(NonEmpty, str):
127140
"""A VType for non-empty strings"""
128141

129-
class AlternateNonEmptyStr(VType):
142+
class NonEmptyStr2(VType):
130143
"""A VType for non-empty strings - alternate style"""
131144
__type__ = NonEmpty, str
132145
```
133146

134-
The vtypes work as expected:
147+
All these behave as expected:
135148

136149
```python
137150
assert isinstance('hoho', NonEmptyStr)

0 commit comments

Comments
 (0)