Skip to content

Commit e50bf51

Browse files
author
Sylvain MARIE
committed
Updated doc and api reference
1 parent 76c9d57 commit e50bf51

File tree

2 files changed

+58
-35
lines changed

2 files changed

+58
-35
lines changed

docs/api_reference.md

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -406,47 +406,68 @@ TypeError: __init__() got an unexpected keyword argument 'SENTENCE'
406406

407407
```python
408408
def autoclass(
409-
# from autofields
410-
check_types=False, # type: Union[bool, DecoratedClass]
411-
autofields_include_upper=False, # type: bool
412-
autofields_include_dunder=False, # type: bool
413-
# from both (merging the names)
414-
autoinit=True, # type: bool
415-
# from autoclass
416-
ac_include=None, # type: Union[str, Tuple[str]]
417-
ac_exclude=None, # type: Union[str, Tuple[str]]
418-
autodict=True, # type: bool
419-
autorepr=_AUTO, # type: bool
420-
autoeq=_AUTO, # type: bool
421-
autohash=True, # type: bool
409+
# --- autofields
410+
fields=True, # type: Union[bool, DecoratedClass]
411+
typecheck=False, # type: bool
412+
# --- constructor
413+
init=True, # type: bool
414+
# --- class methods
415+
dict=True, # type: bool
416+
dict_public_only=True, # type: bool
417+
repr=True, # type: bool
418+
repr_curly_mode=False, # type: bool
419+
repr_public_only=True, # type: bool
420+
eq=True, # type: bool
421+
eq_public_only=False, # type: bool
422+
hash=True, # type: bool
423+
hash_public_only=False, # type: bool
424+
# --- advanced
425+
af_include_upper=False, # type: bool
426+
af_include_dunder=False, # type: bool
427+
ac_include=None, # type: Union[str, Tuple[str]]
428+
ac_exclude=None, # type: Union[str, Tuple[str]]
422429
):
423430
```
424431

425-
A decorator to automate many things at once for your class. First it executes `@autofields` to generate fields from attribute defined at class level, and generate the init.
432+
A decorator to automate many things at once for your class.
426433

427-
- you can include attributes with dunder names or uppercase names with `autofields_include_dunder` and
428-
`autofields_include_upper` respectively
434+
First if `fields=True` (default) it executes `@autofields` to generate fields from attribute defined at class level.
435+
436+
- you can include attributes with dunder names or uppercase names with `af_include_dunder` and
437+
`af_include_upper` respectively
429438
- you can enable type checking on all fields at once by setting `check_types=True`
430-
- you can disable constructor (init) creation by setting `autoinit=False`
439+
- the constructor is not generated at this stage
440+
441+
Then it generates methods for the class:
431442

432-
Then it executes `@autoclass` to generate convenience methods for the class. By default the class gets a dictionary behaviour (including string representation and equality comparison). You can disable this behaviour by setting `autodict=False`. This will automatically enable an alternate string representation and equality comparison. If you wish to disable them you can further set `autorepr=False` and `autoeq=False` explicitly. All those methods have default include/exlude behaviours that you can override with `ac_include` or `ac_exclude` name lists.
443+
- if `init=True` (default) it generates the constructor based on all fields present, using `make_init()`.
444+
- if `dict=True` (default) it generates `to_dict` and `from_dict` methods. Only public fields are represented in `to_dict`, you can change this with `dict_public_only=False`.
445+
- if `repr=True` (default) it generates a `__repr__` method. Only public fields are represented, you can change this with `repr_public_only=False`.
446+
- if `eq=True` (default) it generates an `__eq__` method, so that instances can be compared to other instances and to dicts. All fields are compared by default, you can change this with `eq_public_only=True`.
447+
- if `hash=True` (default) it generates an `__hash__` method, so that instances can be inserted in sets or dict keys. All fields are hashed by default, you can change this with `hash_public_only=True`.
433448

434-
Finally by default the class gets a `hash` implementation so that its instances can be inserted in sets or dict keys. You can disable this with `autohash=False`.
449+
You can specify an explicit list of fields to include or exclude in the dict/repr/eq/hash methods with the `ac_include` and `ac_exclude` parameters.
435450

436-
See [autoclass documentation](https://smarie.github.io/python-autoclass/) for details on the lower-level methods.
451+
Note that this decorator is similar to the [autoclass library](https://smarie.github.io/python-autoclass/) but is reimplemented here. In particular the parameter names and dictionary behaviour are different.
437452

438453
**Parameters**
439454

440-
- `check_types`: boolean flag (default: False) indicating the value of `check_type` for created fields. Note that the type hint of each created field is copied from the type hint of the member it originates from.
441-
- `autofields_include_upper`: boolean flag (default: False) indicating whether upper-case class members should be also transformed to fields (usually such names are reserved for class constants, not for fields).
442-
- `autofields_include_dunder`: boolean flag (default: False) indicating whether dunder-named class members should be also transformed to fields. Note that even if you set this to True, members with reserved python dunder names will not be transformed. See `is_reserved_dunder` for the list of reserved names.
443-
- `autoinit`: boolean flag (default: True) indicating whether a constructor should be created for the class if no `__init__` method is already present. Such constructor will be created using `__init__ = make_init()`. This is the same behaviour than `make_init` in `@autofields`.
444-
- `ac_include`: a tuple of explicit attribute names to include in the autodict/repr/eq/hash (None means all)
445-
- `ac_exclude`: a tuple of explicit attribute names to exclude in the autodict/repr/eq/hash. In such case, include should be None.
446-
- `autodict`: a boolean to enable autodict on the class (default: True). By default it will be executed with `only_known_fields=True`.
447-
- `autorepr`: a boolean to enable autorepr on the class. By default it is `AUTO` and means "automatic configuration". In that case, it will be defined as `not autodict`.
448-
- `autoeq`: a boolean to enable autoeq on the class. By default it is `AUTO` and means "automatic configuration". In that case, it will be defined as `not autodict`.
449-
- `autohash`: a boolean to enable autohash on the class (default: True). By default it will be executed with `only_known_fields=True`.
455+
- `fields`: boolean flag (default: True) indicating whether to create fields automatically. See `@autofields` for details
456+
- `typecheck`: boolean flag (default: False) used when fields=True indicating the value of `check_type` for created fields. Note that the type hint of each created field is copied from the type hint of the member it originates from.
457+
- `init`: boolean flag (default: True) indicating whether a constructor should be created for the class if no `__init__` method is already present. Such constructor will be created using `__init__ = make_init()`. This is the same behaviour than `make_init` in `@autofields`. Note that this is *not* automatically disabled if you set `fields=False`.
458+
- `dict`: a boolean to automatically create `cls.from_dict(dct)` and `obj.to_dict()` methods on the class (default: True).
459+
- `dict_public_only`: a boolean (default: True) to indicate if only public fields should be exposed in the dictionary view created by `to_dict` when `dict=True`.
460+
- `repr`: a boolean (default: True) to indicate if `__repr__` and `__str__` should be created for the class if not explicitly present.
461+
- `repr_curly_mode`: a boolean (default: False) to turn on an alternate string representation when `repr=True`, using curly braces.
462+
- `repr_public_only`: a boolean (default: True) to indicate if only public fields should be exposed in the string representation when `repr=True`.
463+
- `eq`: a boolean (default: True) to indicate if `__eq__` should be created for the class if not explicitly present.
464+
- `eq_public_only`: a boolean (default: False) to indicate if only public fields should be compared in the equality method created when `eq=True`.
465+
- `hash`: a boolean (default: True) to indicate if `__hash__` should be created for the class if not explicitly present.
466+
- `hash_public_only`: a boolean (default: False) to indicate if only public fields should be hashed in the hash method created when `hash=True`.
467+
- `af_include_upper`: boolean flag (default: False) used when autofields=True indicating whether upper-case class members should be also transformed to fields (usually such names are reserved for class constants, not for fields).
468+
- `af_include_dunder`: boolean flag (default: False) used when autofields=True indicating whether dunder-named class members should be also transformed to fields. Note that even if you set this to True, members with reserved python dunder names will not be transformed. See `is_reserved_dunder` for the list of reserved names.
469+
- `ac_include`: a tuple of explicit attribute names to include in dict/repr/eq/hash (None means all)
470+
- `ac_exclude`: a tuple of explicit attribute names to exclude in dict/repr/eq/hash. In such case, include should be None.
450471

451472
## API
452473

docs/index.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,7 @@ By default type checking is not enabled on the generated fields, but you can ena
656656
657657
#### b - `@autoclass`
658658
659-
Do you **also** wish to have `hash`, `dict`, `eq`, and `repr` views automatically created for your class ? `pyfields` now works hand in hand with `autoclass`. That way, your field definitions can directly be reused for most of the class behaviour. From version `1.5` on, a dedicated `@autoclass` decorator is directly exported from `pyfields`.
660-
661-
This decorator combines `@autofields` (above) and `@autoclass` into one, with options that are relevant to `pyfields`. You can create fields, create the constructor, enable type checking, create a dictionary views, etc. all in one call.
659+
Do you **also** wish to have `hash`, `dict`, `eq`, and `repr` views automatically created for your class ? From version `1.6` on, `pyfields` provides an `@autoclass` decorator. That way, your field definitions can directly be reused for most of the class behaviour.
662660
663661
```python
664662
from pyfields import field, autoclass
@@ -671,8 +669,9 @@ class Foo:
671669
672670
foo = Foo(msg='hello')
673671
674-
print(foo) # automatic string representation
675-
print(dict(foo)) # automatic dict view
672+
print(foo) # automatic string representation
673+
print(foo.to_dict()) # automatic from/to dict
674+
assert foo == Foo.from_dict(foo.to_dict())
676675
677676
assert foo == Foo(msg='hello', age=12, height=50) # automatic equality comparison
678677
assert foo == {'msg': 'hello', 'age': 12, 'height': 50} # automatic eq comparison with dicts
@@ -685,6 +684,9 @@ Foo(msg='hello', age=12, height=50)
685684
{'msg': 'hello', 'age': 12, 'height': 50}
686685
```
687686
687+
!!!warning "use pyfields `@autoclass` over the one in `autoclass` library"
688+
This decorator combines `@autofields` and `@autoclass` into one, with options that are relevant to `pyfields`. Therefore it is now the recommended one to use.
689+
688690
See [API reference](./api_reference.md#autoclass) for details.
689691
690692
#### c - `VType`s

0 commit comments

Comments
 (0)