You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`fields`: list of fields to initialize before entering the decorated `__init__` method. For each of these fields a corresponding argument will be added in the method's signature. If an empty list is provided, all fields from the class will be used including inherited fields following the mro.
354
+
355
+
356
+
## API
357
+
358
+
### `has_fields`
359
+
360
+
```python
361
+
defhas_fields(cls,
362
+
include_inherited=True# type:bool
363
+
)
364
+
```
365
+
366
+
Returns True if class `cls` defines at least one `pyfields` field.
367
+
If `include_inherited` is `True` (default), the method will return `True` if at least a field is defined in the class or one of its ancestors. If `False`, the fields need to be defined on the class itself.
368
+
369
+
### `get_fields`
370
+
371
+
```python
372
+
defget_fields(cls,
373
+
include_inherited=True, # type:bool
374
+
remove_duplicates=True, # type:bool
375
+
ancestors_first=True, # type:bool
376
+
container_type=tuple, # type:Type[T]
377
+
)
378
+
```
379
+
380
+
Utility method to collect all fields defined in a class, including all inherited or not.
381
+
By default duplicates are removed and ancestor fields are included and appear first. If a field is overridden, it will appear at the position of the overridden field in the order.
382
+
383
+
### `yield_fields`
384
+
385
+
```python
386
+
defyield_fields(cls,
387
+
include_inherited=True, # type:bool
388
+
remove_duplicates=True, # type:bool
389
+
ancestors_first=True, # type:bool
390
+
)
391
+
```
392
+
393
+
Similar to `get_fields` but as a generator.
394
+
395
+
### `get_field`
396
+
397
+
```python
398
+
defget_field(cls, name)
399
+
```
400
+
401
+
Utility method to return the field member with name `name` in class `cls`.
402
+
If the member is not a field, a `NotAFieldError` is raised.
Copy file name to clipboardExpand all lines: docs/index.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@
6
6
7
7
[](https://smarie.github.io/python-pyfields/)[](https://pypi.python.org/pypi/pyfields/)[](https://pepy.tech/project/pyfields)[](https://pepy.tech/project/pyfields)[](https://github.com/smarie/python-pyfields/stargazers)
8
8
9
+
!!! success "`pyfields` is now automatically supported by `autoclass` ! See [here](https://smarie.github.io/python-autoclass/#pyfields-combo) for details."
9
10
10
11
`pyfields` provides a simple and elegant way to define fields in python classes. With `pyfields` you explicitly define all aspects of a field (default value, type, documentation...) in a single place, and can refer to it from other places.
11
12
@@ -27,7 +28,9 @@ It provides **many optional features** that will make your object-oriented devel
27
28
28
29
- initializing fields in your *constructor* is very easy and highly customizable
29
30
30
-
If your first reaction is "what about `attrs` / `dataclasses` / `pydantic` / `characteristic` / `traits` / `traitlets` / `autoclass` / ...", please have a look [here](why.md).
31
+
Finally, it offers an API that other libraries can leverage to get the list of fields. For example `autoclass` now leverages `pyfields` to automatically add hash/dict/eq/repr to your class.
32
+
33
+
If your first reaction is "what about `attrs` / `dataclasses` / `pydantic` / `characteristic` / `traits` / `traitlets` / ...", please have a look [here](why.md).
31
34
32
35
33
36
## Installing
@@ -603,6 +606,14 @@ Note on the order of arguments in the resulting `__init__` signature: as you can
603
606
604
607
### 3. Misc.
605
608
609
+
#### API
610
+
611
+
`pyfields` offers an API so that other libraries can inspect the fields: `get_fields`, `yield_fields`, `has_fields`, `get_field`. See [API reference](https://smarie.github.io/python-pyfields/api_reference/#api) for details.
612
+
613
+
#### hash, dict, eq, repr
614
+
615
+
`autoclass` is now compliant with `pyfields`. So you can use `@autoclass`, or `@autorepr`, `@autohash`, `@autodict`... on the decorated class. That way, your fields definition is directly reused for most of the class behaviour. See [here](https://smarie.github.io/python-autoclass/#pyfields-combo) for details.
616
+
606
617
#### Slots
607
618
608
619
You can use `pyfields`if your class has `__slots__`. You will simply have to use an underscore in the slot name corresponding to a field: `_<field_name>`. For example:
0 commit comments