MCPcopy
hub / github.com/python-attrs/attrs / make_class

Function make_class

src/attr/_make.py:3206–3306  ·  src/attr/_make.py::make_class

r""" A quick way to create a new class called *name* with *attrs*. .. note:: ``make_class()`` is a thin wrapper around `attr.s`, not `attrs.define` which means that it doesn't come with some of the improved defaults. For example, if you want the same ``on_setattr``

(
    name, attrs, bases=(object,), class_body=None, **attributes_arguments
)

Source from the content-addressed store, hash-verified

3204
3205
3206def make_class(
3207 name, attrs, bases=(object,), class_body=None, **attributes_arguments
3208):
3209 rclass="st">"""
3210 A quick way to create a new class called *name* with *attrs*.
3211
3212 .. note::
3213
3214 ``make_class()`` is a thin wrapper around `attr.s`, not `attrs.define`
3215 which means that it doesn&class="cm">#x27;t come with some of the improved defaults.
3216
3217 For example, if you want the same ``on_setattr`` behavior as in
3218 `attrs.define`, you have to pass the hooks yourself: ``make_class(...,
3219 on_setattr=setters.pipe(setters.convert, setters.validate)``
3220
3221 .. warning::
3222
3223 It is *your* duty to ensure that the class name and the attribute names
3224 are valid identifiers. ``make_class()`` will *not* validate them for
3225 you.
3226
3227 Args:
3228 name (str): The name for the new class.
3229
3230 attrs (list | dict):
3231 A list of names or a dictionary of mappings of names to `attr.ib`\
3232 s / `attrs.field`\ s.
3233
3234 The order is deduced from the order of the names or attributes
3235 inside *attrs*. Otherwise the order of the definition of the
3236 attributes is used.
3237
3238 bases (tuple[type, ...]): Classes that the new class will subclass.
3239
3240 class_body (dict):
3241 An optional dictionary of class attributes for the new class.
3242
3243 attributes_arguments: Passed unmodified to `attr.s`.
3244
3245 Returns:
3246 type: A new class with *attrs*.
3247
3248 .. versionadded:: 17.1.0 *bases*
3249 .. versionchanged:: 18.1.0 If *attrs* is ordered, the order is retained.
3250 .. versionchanged:: 23.2.0 *class_body*
3251 .. versionchanged:: 25.2.0 Class names can now be unicode.
3252 class="st">"""
3253 class="cm"># Class identifiers are converted into the normal form NFKC while parsing
3254 name = unicodedata.normalize(class="st">"NFKC", name)
3255
3256 if isinstance(attrs, dict):
3257 cls_dict = attrs
3258 elif isinstance(attrs, (list, tuple)):
3259 cls_dict = {a: attrib() for a in attrs}
3260 else:
3261 msg = class="st">"attrs argument must be a dict or a list."
3262 raise TypeError(msg)
3263

Callers 15

simple_classFunction · 0.90
test_simpleMethod · 0.90
test_dictMethod · 0.90
test_attr_argsMethod · 0.90
test_basesMethod · 0.90
test_clean_classMethod · 0.90
test_converterMethod · 0.90

Calls 2

attribFunction · 0.85

Tested by 15

test_simpleMethod · 0.72
test_dictMethod · 0.72
test_attr_argsMethod · 0.72
test_basesMethod · 0.72
test_clean_classMethod · 0.72
test_converterMethod · 0.72
test_convert_propertyMethod · 0.72