Skip to content

Commit f9fa5fd

Browse files
author
Sylvain MARIE
committed
@autoclass now provides an autofields argument to apply pyfields.autofields automatically before applying autoclass. Fixes #38
1 parent fcfc05b commit f9fa5fd

1 file changed

Lines changed: 37 additions & 24 deletions

File tree

autoclass/autoclass_.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from funcsigs import signature
1515

1616
try:
17-
from pyfields import get_fields, make_init
17+
from pyfields import get_fields, make_init, autofields as apply_autofields
1818
WITH_PYFIELDS = True
1919
except ImportError:
2020
WITH_PYFIELDS = False
@@ -32,16 +32,17 @@
3232

3333

3434
@class_decorator
35-
def autoclass(include=None, # type: Union[str, Tuple[str]]
36-
exclude=None, # type: Union[str, Tuple[str]]
37-
autoargs=AUTO, # type: bool
38-
autoprops=AUTO, # type: bool
39-
autodict=True, # type: bool
40-
autorepr=AUTO, # type: bool
41-
autoeq=AUTO, # type: bool
42-
autohash=True, # type: bool
43-
autoslots=False, # type: bool
44-
autoinit=AUTO, # type: bool
35+
def autoclass(include=None, # type: Union[str, Tuple[str]]
36+
exclude=None, # type: Union[str, Tuple[str]]
37+
autoargs=AUTO, # type: bool
38+
autoprops=AUTO, # type: bool
39+
autodict=True, # type: bool
40+
autorepr=AUTO, # type: bool
41+
autoeq=AUTO, # type: bool
42+
autohash=True, # type: bool
43+
autoslots=False, # type: bool
44+
autoinit=AUTO, # type: bool
45+
autofields=False, # type: bool
4546
cls=DECORATED
4647
):
4748
"""
@@ -67,11 +68,13 @@ class has `pyfields` fields and does not define an `__init__` method ; and `Fals
6768
:param autohash: a boolean to enable autohash on the class (default: True). By default it will be executed with
6869
`only_known_fields=True`.
6970
:param autoslots: a boolean to enable autoslots on the class (default: False).
71+
:param autofields: a boolean (default: False) to apply autofields automatically on the class before applying
72+
`autoclass` (see `pyfields` documentation for details)
7073
:return:
7174
"""
7275
return autoclass_decorate(cls, include=include, exclude=exclude, autoargs=autoargs, autoprops=autoprops,
7376
autodict=autodict, autohash=autohash, autoslots=autoslots, autoinit=autoinit,
74-
autorepr=autorepr, autoeq=autoeq)
77+
autorepr=autorepr, autoeq=autoeq, autofields=autofields)
7578

7679

7780
class NoCustomInitError(Exception):
@@ -88,17 +91,18 @@ def __str__(self):
8891
"custom `__init__`"
8992

9093

91-
def autoclass_decorate(cls, # type: Type[T]
92-
include=None, # type: Union[str, Tuple[str]]
93-
exclude=None, # type: Union[str, Tuple[str]]
94-
autoargs=AUTO, # type: bool
95-
autoprops=AUTO, # type: bool
96-
autoinit=AUTO, # type: bool
97-
autodict=True, # type: bool
98-
autorepr=AUTO, # type: bool
99-
autoeq=AUTO, # type: bool
100-
autohash=True, # type: bool
101-
autoslots=False, # type: bool
94+
def autoclass_decorate(cls, # type: Type[T]
95+
include=None, # type: Union[str, Tuple[str]]
96+
exclude=None, # type: Union[str, Tuple[str]]
97+
autoargs=AUTO, # type: bool
98+
autoprops=AUTO, # type: bool
99+
autoinit=AUTO, # type: bool
100+
autodict=True, # type: bool
101+
autorepr=AUTO, # type: bool
102+
autoeq=AUTO, # type: bool
103+
autohash=True, # type: bool
104+
autoslots=False, # type: bool
105+
autofields=False, # type: bool
102106
):
103107
# type: (...) -> Type[T]
104108
"""
@@ -124,18 +128,27 @@ class has `pyfields` fields and does not define an `__init__` method ; and `Fals
124128
:param autohash: a boolean to enable autohash on the class (default: True). By default it will be executed with
125129
`only_known_fields=True`.
126130
:param autoslots: a boolean to enable autoslots on the class (default: False).
131+
:param autofields: a boolean (default: False) to apply autofields automatically on the class before applying
132+
`autoclass` (see `pyfields` documentation for details)
127133
:return:
128134
"""
129135
# first check that we do not conflict with other known decorators
130136
check_known_decorators(cls, '@autoclass')
131137

138+
if autofields:
139+
if WITH_PYFIELDS:
140+
cls = apply_autofields(cls)
141+
else:
142+
raise ValueError("`autofields=True` can only be used when `pyfields` is installed. Please `pip install "
143+
"pyfields`")
144+
132145
# Get constructor
133146
init_fun, is_init_inherited = get_constructor(cls)
134147

135148
# Check for pyfields fields
136149
if WITH_PYFIELDS:
137150
all_pyfields = get_fields(cls)
138-
has_pyfields = len(all_pyfields) > 0
151+
has_pyfields = len(all_pyfields) > 0 # 'or autofields' ?: cant' find a use case where it would be needed.
139152
else:
140153
has_pyfields = False
141154

0 commit comments

Comments
 (0)