Skip to content

Commit c9bd3dc

Browse files
author
Sylvain MARIE
committed
Added autorepr support in @autoclass. Fixed #31
1 parent ddf273a commit c9bd3dc

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

autoclass/autoclass_.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from autoclass.autoargs_ import _autoargs_decorate
2424
from autoclass.autoprops_ import execute_autoprops_on_class
2525
from autoclass.autodict_ import execute_autodict_on_class
26+
from autoclass.autorepr_ import execute_autorepr_on_class
2627
from autoclass.autohash_ import execute_autohash_on_class
2728
from autoclass.autoslots_ import autoslots_decorate
2829

@@ -35,6 +36,7 @@ def autoclass(include=None, # type: Union[str, Tuple[str]]
3536
autoargs=AUTO, # type: bool
3637
autoprops=AUTO, # type: bool
3738
autodict=True, # type: bool
39+
autorepr=AUTO, # type: bool
3840
autohash=True, # type: bool
3941
autoslots=False, # type: bool
4042
autoinit=AUTO, # type: bool
@@ -51,16 +53,21 @@ class defines an `__init__` method and has no `pyfields` fields ; and `False` ot
5153
:param autoprops: a boolean to enable autoprops on the class. By default it is `AUTO` and means "automatic
5254
configuration". In that case, the behaviour will depend on the class: it will be equivalent to `True` if the
5355
class defines an `__init__` method and has no `pyfields` fields ; and `False` otherwise.
54-
:param autodict: a boolean to enable autodict on the class (default: True)
55-
:param autohash: a boolean to enable autohash on the class (default: True)
56-
:param autoslots: a boolean to enable autoslots on the class (default: False).
5756
:param autoinit: a boolean to enable autoinit on the class. By default it is `AUTO` and means "automatic
5857
configuration". In that case, the behaviour will depend on the class: it will be equivalent to `True` if the
5958
class has `pyfields` fields and does not define an `__init__` method ; and `False` otherwise.
59+
:param autodict: a boolean to enable autodict on the class (default: True). By default it will be executed with
60+
`only_known_fields=True`.
61+
:param autorepr: a boolean to enable autorepr on the class. By default it is `AUTO` and means "automatic
62+
configuration". In that case, it will be defined as `not autodict`.
63+
:param autohash: a boolean to enable autohash on the class (default: True). By default it will be executed with
64+
`only_known_fields=True`.
65+
:param autoslots: a boolean to enable autoslots on the class (default: False).
6066
:return:
6167
"""
6268
return autoclass_decorate(cls, include=include, exclude=exclude, autoargs=autoargs, autoprops=autoprops,
63-
autodict=autodict, autohash=autohash, autoslots=autoslots, autoinit=autoinit)
69+
autodict=autodict, autohash=autohash, autoslots=autoslots, autoinit=autoinit,
70+
autorepr=autorepr)
6471

6572

6673
class NoCustomInitError(Exception):
@@ -84,6 +91,7 @@ def autoclass_decorate(cls, # type: Type[T]
8491
autoprops=AUTO, # type: bool
8592
autoinit=AUTO, # type: bool
8693
autodict=True, # type: bool
94+
autorepr=AUTO, # type: bool
8795
autohash=True, # type: bool
8896
autoslots=False, # type: bool
8997
):
@@ -98,12 +106,14 @@ def autoclass_decorate(cls, # type: Type[T]
98106
class defines an `__init__` method and has no `pyfields` fields ; and `False` otherwise.
99107
:param autoprops: a boolean to enable autoprops on the class. By default it is `AUTO` and means "automatic
100108
configuration". In that case, the behaviour will depend on the class: it will be equivalent to `True` if the
101-
class defines has no `pyfields` fields ; and `False` otherwise.
109+
class defines an `__init__` method and has no `pyfields` fields ; and `False` otherwise.
102110
:param autoinit: a boolean to enable autoinit on the class. By default it is `AUTO` and means "automatic
103111
configuration". In that case, the behaviour will depend on the class: it will be equivalent to `True` if the
104112
class has `pyfields` fields and does not define an `__init__` method ; and `False` otherwise.
105113
:param autodict: a boolean to enable autodict on the class (default: True). By default it will be executed with
106114
`only_known_fields=True`.
115+
:param autorepr: a boolean to enable autorepr on the class. By default it is `AUTO` and means "automatic
116+
configuration". In that case, it will be defined as `not autodict`.
107117
:param autohash: a boolean to enable autohash on the class (default: True). By default it will be executed with
108118
`only_known_fields=True`.
109119
:param autoslots: a boolean to enable autoslots on the class (default: False).
@@ -179,10 +189,16 @@ class has `pyfields` fields and does not define an `__init__` method ; and `Fals
179189
# noinspection PyUnboundLocalVariable
180190
cls.__init__ = make_init(*selected_fields)
181191

182-
# @autodict
192+
# @autodict or @autorepr
183193
if autodict:
194+
if autorepr is not AUTO and autorepr:
195+
raise ValueError("`autorepr` can not be set to `True` simultaneously with `autodict`. Please set "
196+
"`autodict=False`.")
184197
# By default execute with the known list of fields, so equivalent of `only_known_fields=True`.
185198
execute_autodict_on_class(cls, selected_names=selected_names)
199+
elif autorepr is AUTO or autorepr:
200+
# By default execute with the known list of fields, so equivalent of `only_known_fields=True`.
201+
execute_autorepr_on_class(cls, selected_names=selected_names)
186202

187203
# @autohash
188204
if autohash:

0 commit comments

Comments
 (0)