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
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.
426
433
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
429
438
- 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:
431
442
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`.
433
448
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.
435
450
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.
437
452
438
453
**Parameters**
439
454
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.
Copy file name to clipboardExpand all lines: docs/index.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -656,9 +656,7 @@ By default type checking is not enabled on the generated fields, but you can ena
656
656
657
657
#### b - `@autoclass`
658
658
659
-
Do you **also** wish to have `hash`, `dict`, `eq`, and `repr` views automatically created foryour class ? `pyfields` now works handin 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, enabletype 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.
!!!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
+
688
690
See [API reference](./api_reference.md#autoclass) for details.
0 commit comments