datumaro.components.merge.intersect_merge#
Classes
|
Merge several datasets with "intersect" policy: |
- class datumaro.components.merge.intersect_merge.IntersectMerge(conf=_Nothing.NOTHING)[source]#
Bases:
Merger
Merge several datasets with “intersect” policy:
If there are two or more dataset items whose (id, subset) pairs match each other,
we can consider this as having an intersection in our dataset. This method merges the annotations of the corresponding
DatasetItem
into oneDatasetItem
to handle this intersection. The rule to handle merging annotations is provided byAnnotationMerger
according to their annotation types. For example, DatasetItem(id=”item_1”, subset=”train”, annotations=[Bbox(0, 0, 1, 1)]) from Dataset-A and DatasetItem(id=”item_1”, subset=”train”, annotations=[Bbox(.5, .5, 1, 1)]) from Dataset-B can be merged into DatasetItem(id=”item_1”, subset=”train”, annotations=[Bbox(0, 0, 1, 1)]).Label categories are merged according to the union of their label names
(Same as UnionMerge). For example, if Dataset-A has {“car”, “cat”, “dog”} and Dataset-B has {“car”, “bus”, “truck”} labels, the merged dataset will have {“bust”, “car”, “cat”, “dog”, “truck”} labels.
This merge has configuration parameters (conf) to control the annotation merge behaviors.
For example,
```python merge = IntersectMerge(
- conf=IntersectMerge.Conf(
pairwise_dist=0.25, groups=[], output_conf_thresh=0.0, quorum=0,
)
)#
For more details for the parameters, please refer to
IntersectMerge.Conf
.Method generated by attrs for class IntersectMerge.
- class Conf(*, pairwise_dist=0.5, sigma=_Nothing.NOTHING, output_conf_thresh=0, quorum=0, ignored_attributes=_Nothing.NOTHING, groups=_Nothing.NOTHING, close_distance=0.75)[source]#
Bases:
object
- Parameters:
pairwise_dist – IoU match threshold for segments
sigma – Parameter for Object Keypoint Similarity metric (https://cocodataset.org/#keypoints-eval)
output_conf_thresh – Confidence threshold for output annotations
quorum – Minimum count for a label and attribute voting results to be counted
ignored_attributes – Attributes to be ignored in the merged
DatasetItem
groups – A comma-separated list of labels in annotation groups to check. ‘?’ postfix can be added to a label to make it optional in the group (repeatable)
close_distance – Distance threshold between annotations to decide their closeness. If they are decided to be close, it will be enrolled to the error tracker.
Method generated by attrs for class IntersectMerge.Conf.
- merge(sources: Sequence[IDataset]) DatasetItemStorage [source]#
- merge_items(items: Dict[int, DatasetItem]) DatasetItem [source]#
- class datumaro.components.merge.intersect_merge.AnnotationMerger(*, context: IMatcherContext | IMergerContext | None = None)[source]#
Bases:
AnnotationMatcher
Method generated by attrs for class AnnotationMerger.
- class datumaro.components.merge.intersect_merge.AnnotationType(value)[source]#
Bases:
IntEnum
An enumeration.
- unknown = 0#
- label = 1#
- mask = 2#
- points = 3#
- polygon = 4#
- polyline = 5#
- bbox = 6#
- cuboid_3d = 8#
- super_resolution_annotation = 9#
- depth_annotation = 10#
- ellipse = 11#
- hash_key = 12#
- feature_vector = 13#
- tabular = 14#
- rotated_bbox = 15#
- exception datumaro.components.merge.intersect_merge.AnnotationsTooCloseError(item_id, a, b, distance)[source]#
Bases:
DatasetQualityError
Method generated by attrs for class AnnotationsTooCloseError.
- item_id#
- a#
- b#
- distance#
- class datumaro.components.merge.intersect_merge.BboxMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,BboxMatcher
Method generated by attrs for class BboxMerger.
- class datumaro.components.merge.intersect_merge.CaptionsMerger(*, context: IMatcherContext | IMergerContext | None = None)[source]#
Bases:
AnnotationMerger
,CaptionsMatcher
Method generated by attrs for class CaptionsMerger.
- exception datumaro.components.merge.intersect_merge.ConflictingCategoriesError(msg=None, *, sources=None)[source]#
Bases:
DatasetMergeError
- sources#
- class datumaro.components.merge.intersect_merge.Cuboid3dMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,Cuboid3dMatcher
Method generated by attrs for class Cuboid3dMerger.
- class datumaro.components.merge.intersect_merge.DatasetItem(id: str, *, subset: str | None = None, media: str | MediaElement | None = None, annotations: List[Annotation] | None = None, attributes: Dict[str, Any] | None = None)[source]#
Bases:
object
- media: MediaElement | None#
- annotations: Annotations#
- class datumaro.components.merge.intersect_merge.DatasetItemStorage[source]#
Bases:
object
- put(item: DatasetItem) bool [source]#
- get(id: str | DatasetItem, subset: str | None = None, dummy: Any | None = None) DatasetItem | None [source]#
- class datumaro.components.merge.intersect_merge.DatasetItemStorageDatasetView(parent: DatasetItemStorage, infos: Dict[str, Any], categories: Dict[AnnotationType, Categories], media_type: Type[MediaElement] | None, ann_types: Set[AnnotationType] | None)[source]#
Bases:
IDataset
- class Subset(parent: DatasetItemStorageDatasetView, name: str)[source]#
Bases:
IDataset
- class datumaro.components.merge.intersect_merge.EllipseMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,ShapeMatcher
Method generated by attrs for class EllipseMerger.
- exception datumaro.components.merge.intersect_merge.FailedAttrVotingError(item_id, attr, votes, ann, *, sources=_Nothing.NOTHING)[source]#
Bases:
DatasetMergeError
Method generated by attrs for class FailedAttrVotingError.
- item_id#
- attr#
- votes#
- ann#
- class datumaro.components.merge.intersect_merge.FeatureVectorMerger(*, context: IMatcherContext | IMergerContext | None = None)[source]#
Bases:
AnnotationMerger
,FeatureVectorMatcher
Method generated by attrs for class FeatureVectorMerger.
- class datumaro.components.merge.intersect_merge.HashKeyMerger(*, context: IMatcherContext | IMergerContext | None = None)[source]#
Bases:
AnnotationMerger
,HashKeyMatcher
Method generated by attrs for class HashKeyMerger.
- class datumaro.components.merge.intersect_merge.IDataset[source]#
Bases:
object
- subsets() Dict[str, IDataset] [source]#
Enumerates subsets in the dataset. Each subset can be a dataset itself.
- categories() Dict[AnnotationType, Categories] [source]#
Returns metainfo about dataset labels.
- get(id: str, subset: str | None = None) DatasetItem | None [source]#
Provides random access to dataset items.
- media_type() Type[MediaElement] [source]#
Returns media type of the dataset items.
All the items are supposed to have the same media type. Supposed to be constant and known immediately after the object construction (i.e. doesn’t require dataset iteration).
- ann_types() List[AnnotationType] [source]#
Returns available task type from dataset annotation types.
- class datumaro.components.merge.intersect_merge.ImageAnnotationMerger(*, context: IMatcherContext | IMergerContext | None = None)[source]#
Bases:
AnnotationMerger
,ImageAnnotationMatcher
Method generated by attrs for class ImageAnnotationMerger.
- class datumaro.components.merge.intersect_merge.LabelCategories(items: List[str] = _Nothing.NOTHING, label_groups: List[LabelGroup] = _Nothing.NOTHING, *, attributes: Set[str] = _Nothing.NOTHING)[source]#
Bases:
Categories
Method generated by attrs for class LabelCategories.
- class Category(name, parent: str = '', attributes: Set[str] = _Nothing.NOTHING)[source]#
Bases:
object
Method generated by attrs for class LabelCategories.Category.
- class LabelGroup(name, labels: List[str] = [], group_type: GroupType = GroupType.EXCLUSIVE)[source]#
Bases:
object
Method generated by attrs for class LabelCategories.LabelGroup.
- label_groups: List[LabelGroup]#
- classmethod from_iterable(iterable: Iterable[str | Tuple[str] | Tuple[str, str] | Tuple[str, str, List[str]]]) LabelCategories [source]#
Creates a LabelCategories from iterable.
- Parameters:
iterable –
This iterable object can be:
a list of str - will be interpreted as list of Category names
a list of positional arguments - will generate Categories with these arguments
Returns: a LabelCategories object
- class datumaro.components.merge.intersect_merge.LabelMerger(*, context: IMatcherContext | IMergerContext | None = None, quorum=0)[source]#
Bases:
AnnotationMerger
,LabelMatcher
Method generated by attrs for class LabelMerger.
- class datumaro.components.merge.intersect_merge.LineMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,LineMatcher
Method generated by attrs for class LineMerger.
- class datumaro.components.merge.intersect_merge.MaskCategories(colormap: Dict[int, Tuple[int, int, int]] = _Nothing.NOTHING, inverse_colormap: Dict[Tuple[int, int, int], int] | None = None, *, attributes: Set[str] = _Nothing.NOTHING)[source]#
Bases:
Categories
Describes a color map for segmentation masks.
Method generated by attrs for class MaskCategories.
- classmethod generate(size: int = 255, include_background: bool = True) MaskCategories [source]#
Generates MaskCategories with the specified size.
- If include_background is True, the result will include the item
“0: (0, 0, 0)”, which is typically used as a background color.
- class datumaro.components.merge.intersect_merge.MaskMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,MaskMatcher
Method generated by attrs for class MaskMerger.
- class datumaro.components.merge.intersect_merge.Merger(**options)[source]#
Bases:
IMergerContext
,CliPlugin
Merge multiple datasets into one dataset
- static merge_categories(sources: Sequence[Dict[AnnotationType, Categories]]) Dict [source]#
- exception datumaro.components.merge.intersect_merge.NoMatchingAnnError(item_id, ann, *, sources=_Nothing.NOTHING)[source]#
Bases:
DatasetMergeError
Method generated by attrs for class NoMatchingAnnError.
- item_id#
- ann#
- exception datumaro.components.merge.intersect_merge.NoMatchingItemError(item_id, *, sources=_Nothing.NOTHING)[source]#
Bases:
DatasetMergeError
Method generated by attrs for class NoMatchingItemError.
- item_id#
- class datumaro.components.merge.intersect_merge.OrderedDict[source]#
Bases:
dict
Dictionary that remembers insertion order
- clear() None. Remove all items from od. #
- popitem(last=True)#
Remove and return a (key, value) pair from the dictionary.
Pairs are returned in LIFO order if last is true or FIFO order if false.
- move_to_end(key, last=True)#
Move an existing element to the end (or beginning if last is false).
Raise KeyError if the element does not exist.
- update([E, ]**F) None. Update D from dict/iterable E and F. #
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- keys() a set-like object providing a view on D's keys #
- items() a set-like object providing a view on D's items #
- values() an object providing a view on D's values #
- pop(key[, default]) v, remove specified key and return the corresponding value. #
If the key is not found, return the default if given; otherwise, raise a KeyError.
- setdefault(key, default=None)#
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- copy() a shallow copy of od #
- fromkeys(value=None)#
Create a new ordered dictionary with keys from iterable and values set to value.
- class datumaro.components.merge.intersect_merge.PointsCategories(items: Dict[int, Category] = _Nothing.NOTHING, *, attributes: Set[str] = _Nothing.NOTHING)[source]#
Bases:
Categories
Describes (key-)point metainfo such as point names and joints.
Method generated by attrs for class PointsCategories.
- class Category(labels: List[str] = _Nothing.NOTHING, joints: Set[Tuple[int, int]] = _Nothing.NOTHING)[source]#
Bases:
object
Method generated by attrs for class PointsCategories.Category.
- classmethod from_iterable(iterable: Tuple[int, List[str]] | Tuple[int, List[str], Set[Tuple[int, int]]]) PointsCategories [source]#
Create PointsCategories from an iterable.
- Parameters:
iterable –
An Iterable with the following elements:
a label id
a list of positional arguments for Categories
- Returns:
PointsCategories object
- Return type:
- class datumaro.components.merge.intersect_merge.PointsMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0, sigma: list | None = None, instance_map)[source]#
Bases:
_ShapeMerger
,PointsMatcher
Method generated by attrs for class PointsMerger.
- class datumaro.components.merge.intersect_merge.PolygonMerger(*, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,PolygonMatcher
Method generated by attrs for class PolygonMerger.
- class datumaro.components.merge.intersect_merge.RotatedBboxMerger(sigma: list | None = None, *, context: ~datumaro.components.abstracts.merger.IMatcherContext | ~datumaro.components.abstracts.merger.IMergerContext | None = None, pairwise_dist=0.9, cluster_dist=-1.0, match_segments=<function match_segments_pair>, quorum=0)[source]#
Bases:
_ShapeMerger
,RotatedBboxMatcher
Method generated by attrs for class RotatedBboxMerger.
- class datumaro.components.merge.intersect_merge.TabularMerger(*, context: IMatcherContext | IMergerContext | None = None)[source]#
Bases:
AnnotationMerger
,TabularMatcher
Method generated by attrs for class TabularMerger.
- exception datumaro.components.merge.intersect_merge.WrongGroupError(item_id, found, expected, group)[source]#
Bases:
DatasetQualityError
Method generated by attrs for class WrongGroupError.
- item_id#
- found#
- expected#
- group#
- datumaro.components.merge.intersect_merge.attrib(default=_Nothing.NOTHING, validator=None, repr=True, cmp=None, hash=None, init=True, metadata=None, type=None, converter=None, factory=None, kw_only=False, eq=None, order=None, on_setattr=None, alias=None)[source]#
Create a new attribute on a class.
Warning
Does not do anything unless the class is also decorated with attr.s / attrs.define / and so on!
Please consider using attrs.field in new code (
attr.ib
will never go away, though).- Parameters:
default –
A value that is used if an attrs-generated
__init__
is used and no value is passed while instantiating or the attribute is excluded usinginit=False
.If the value is an instance of attrs.Factory, its callable will be used to construct a new value (useful for mutable data types like lists or dicts).
If a default is not set (or set manually to attrs.NOTHING), a value must be supplied when instantiating; otherwise a TypeError will be raised.
The default can also be set using decorator notation as shown below.
See also
defaults
factory (callable) – Syntactic sugar for
default=attr.Factory(factory)
.validator (callable or a list of callables.) –
callable that is called by attrs-generated
__init__
methods after the instance has been initialized. They receive the initialized instance, theAttribute()
, and the passed value.The return value is not inspected so the validator has to throw an exception itself.
If a list is passed, its items are treated as validators and must all pass.
Validators can be globally disabled and re-enabled using attrs.validators.get_disabled / attrs.validators.set_disabled.
The validator can also be set using decorator notation as shown below.
See also
validators
repr (a bool or a callable to use a custom function.) – Include this attribute in the generated
__repr__
method. IfTrue
, include the attribute; ifFalse
, omit it. By default, the built-inrepr()
function is used. To override how the attribute value is formatted, pass acallable
that takes a single value and returns a string. Note that the resulting string is used as-is, i.e. it will be used directly instead of callingrepr()
(the default).eq (a bool or a callable.) –
If
True
(default), include this attribute in the generated__eq__
and__ne__
methods that check two instances for equality. To override how the attribute value is compared, pass acallable
that takes a single value and returns the value to be compared.See also
comparison
order (a bool or a callable.) –
If
True
(default), include this attributes in the generated__lt__
,__le__
,__gt__
and__ge__
methods. To override how the attribute value is ordered, pass acallable
that takes a single value and returns the value to be ordered.See also
comparison
cmp (a bool or a callable.) –
Setting cmp is equivalent to setting eq and order to the same value. Must not be mixed with eq or order.
See also
comparison
hash (bool | None) –
Include this attribute in the generated
__hash__
method. IfNone
(default), mirror eq’s value. This is the correct behavior according the Python spec. Setting this value to anything else thanNone
is discouraged.See also
hashing
init (bool) –
Include this attribute in the generated
__init__
method. It is possible to set this toFalse
and set a default value. In that case this attributed is unconditionally initialized with the specified default value or factory.See also
init
converter (callable) –
callable that is called by attrs-generated
__init__
methods to convert attribute’s value to the desired format. It is given the passed-in value, and the returned value will be used as the new value of the attribute. The value is converted before being passed to the validator, if any.See also
converters
metadata (dict | None) – An arbitrary mapping, to be used by third-party components. See extending-metadata.
type –
The type of the attribute. Nowadays, the preferred method to specify the type is using a variable annotation (see PEP 526). This argument is provided for backward compatibility. Regardless of the approach used, the type will be stored on
Attribute.type
.Please note that attrs doesn’t do anything with this metadata by itself. You can use it as part of your own code or for static type checking <types>.
kw_only (bool) – Make this attribute keyword-only in the generated
__init__
(ifinit
isFalse
, this parameter is ignored).on_setattr (callable, or a list of callables, or None, or attrs.setters.NO_OP) – Allows to overwrite the on_setattr setting from attr.s. If left None, the on_setattr value from attr.s is used. Set to attrs.setters.NO_OP to run no setattr hooks for this attribute – regardless of the setting in attr.s.
alias (str | None) – Override this attribute’s parameter name in the generated
__init__
method. If left None, default toname
stripped of leading underscores. See private-attributes.
New in version 15.2.0: convert
New in version 16.3.0: metadata
Changed in version 17.1.0: validator can be a
list
now.Changed in version 17.1.0: hash is
None
and therefore mirrors eq by default.New in version 17.3.0: type
Deprecated since version 17.4.0: convert
New in version 17.4.0: converter as a replacement for the deprecated convert to achieve consistency with other noun-based arguments.
New in version 18.1.0:
factory=f
is syntactic sugar fordefault=attr.Factory(f)
.New in version 18.2.0: kw_only
Changed in version 19.2.0: convert keyword argument removed.
Changed in version 19.2.0: repr also accepts a custom callable.
Deprecated since version 19.2.0: cmp Removal on or after 2021-06-01.
New in version 19.2.0: eq and order
New in version 20.1.0: on_setattr
Changed in version 20.3.0: kw_only backported to Python 2
Changed in version 21.1.0: eq, order, and cmp also accept a custom callable
Changed in version 21.1.0: cmp undeprecated
New in version 22.2.0: alias
- datumaro.components.merge.intersect_merge.attrs(maybe_cls=None, these=None, repr_ns=None, repr=None, cmp=None, hash=None, init=None, slots=False, frozen=False, weakref_slot=True, str=False, auto_attribs=False, kw_only=False, cache_hash=False, auto_exc=False, eq=None, order=None, auto_detect=False, collect_by_mro=False, getstate_setstate=None, on_setattr=None, field_transformer=None, match_args=True, unsafe_hash=None)[source]#
A class decorator that adds dunder methods according to the specified attributes using attr.ib or the these argument.
Please consider using attrs.define / attrs.frozen in new code (
attr.s
will never go away, though).- Parameters:
these (dict of str to attr.ib) –
A dictionary of name to attr.ib mappings. This is useful to avoid the definition of your attributes within the class body because you can’t (e.g. if you want to add
__repr__
methods to Django models) or don’t want to.If these is not
None
, attrs will not search the class body for attributes and will not remove any attributes from it.The order is deduced from the order of the attributes inside these.
repr_ns (str) – When using nested classes, there’s no way in Python 2 to automatically detect that. Therefore it’s possible to set the namespace explicitly for a more meaningful
repr
output.auto_detect (bool) –
Instead of setting the init, repr, eq, order, and hash arguments explicitly, assume they are set to
True
unless any of the involved methods for one of the arguments is implemented in the current class (i.e. it is not inherited from some base class).So for example by implementing
__eq__
on a class yourself, attrs will deduceeq=False
and will create neither__eq__
nor__ne__
(but Python classes come with a sensible__ne__
by default, so it should be enough to only implement__eq__
in most cases).Warning
If you prevent attrs from creating the ordering methods for you (
order=False
, e.g. by implementing__le__
), it becomes your responsibility to make sure its ordering is sound. The best way is to use the functools.total_ordering decorator.Passing
True
orFalse
to init, repr, eq, order, cmp, or hash overrides whatever auto_detect would determine.repr (bool) – Create a
__repr__
method with a human readable representation of attrs attributes..str (bool) – Create a
__str__
method that is identical to__repr__
. This is usually not necessary except for Exceptions.eq (bool | None) –
If
True
orNone
(default), add__eq__
and__ne__
methods that check two instances for equality.They compare the instances as if they were tuples of their attrs attributes if and only if the types of both classes are identical!
See also
comparison
order (bool | None) –
If
True
, add__lt__
,__le__
,__gt__
, and__ge__
methods that behave like eq above and allow instances to be ordered. IfNone
(default) mirror value of eq.See also
comparison
cmp (bool | None) –
Setting cmp is equivalent to setting eq and order to the same value. Must not be mixed with eq or order.
See also
comparison
unsafe_hash (bool | None) –
If
None
(default), the__hash__
method is generated according how eq and frozen are set.If both are True, attrs will generate a
__hash__
for you.If eq is True and frozen is False,
__hash__
will be set to None, marking it unhashable (which it is).If eq is False,
__hash__
will be left untouched meaning the__hash__
method of the base class will be used (if base class isobject
, this means it will fall back to id-based hashing.).
Although not recommended, you can decide for yourself and force attrs to create one (e.g. if the class is immutable even though you didn’t freeze it programmatically) by passing
True
or not. Both of these cases are rather special and should be used carefully.See also
Our documentation on hashing,
Python’s documentation on object.__hash__,
and the GitHub issue that led to the default behavior for more details.
hash (bool | None) – Alias for unsafe_hash. unsafe_hash takes precedence.
init (bool) –
Create a
__init__
method that initializes the attrs attributes. Leading underscores are stripped for the argument name. If a__attrs_pre_init__
method exists on the class, it will be called before the class is initialized. If a__attrs_post_init__
method exists on the class, it will be called after the class is fully initialized.If
init
isFalse
, an__attrs_init__
method will be injected instead. This allows you to define a custom__init__
method that can do pre-init work such assuper().__init__()
, and then call__attrs_init__()
and__attrs_post_init__()
.See also
init
slots (bool) – Create a slotted class that’s more memory-efficient. Slotted classes are generally superior to the default dict classes, but have some gotchas you should know about, so we encourage you to read the glossary entry.
frozen (bool) –
Make instances immutable after initialization. If someone attempts to modify a frozen instance, attrs.exceptions.FrozenInstanceError is raised.
Note
This is achieved by installing a custom
__setattr__
method on your class, so you can’t implement your own.True immutability is impossible in Python.
This does have a minor a runtime performance impact <how-frozen> when initializing new instances. In other words:
__init__
is slightly slower withfrozen=True
.If a class is frozen, you cannot modify
self
in__attrs_post_init__
or a self-written__init__
. You can circumvent that limitation by usingobject.__setattr__(self, "attribute_name", value)
.Subclasses of a frozen class are frozen too.
weakref_slot (bool) – Make instances weak-referenceable. This has no effect unless
slots
is also enabled.auto_attribs (bool) –
If
True
, collect PEP 526-annotated attributes from the class body.In this case, you must annotate every field. If attrs encounters a field that is set to an attr.ib but lacks a type annotation, an attr.exceptions.UnannotatedAttributeError is raised. Use
field_name: typing.Any = attr.ib(...)
if you don’t want to set a type.If you assign a value to those attributes (e.g.
x: int = 42
), that value becomes the default value like if it were passed usingattr.ib(default=42)
. Passing an instance of attrs.Factory also works as expected in most cases (see warning below).Attributes annotated as typing.ClassVar, and attributes that are neither annotated nor set to an attr.ib are ignored.
Warning
For features that use the attribute name to create decorators (e.g. validators), you still must assign attr.ib to them. Otherwise Python will either not find the name or try to use the default value to call e.g.
validator
on it.These errors can be quite confusing and probably the most common bug report on our bug tracker.
kw_only (bool) – Make all attributes keyword-only in the generated
__init__
(ifinit
isFalse
, this parameter is ignored).cache_hash (bool) – Ensure that the object’s hash code is computed only once and stored on the object. If this is set to
True
, hashing must be either explicitly or implicitly enabled for this class. If the hash code is cached, avoid any reassignments of fields involved in hash code computation or mutations of the objects those fields point to after object creation. If such changes occur, the behavior of the object’s hash code is undefined.auto_exc (bool) –
If the class subclasses BaseException (which implicitly includes any subclass of any exception), the following happens to behave like a well-behaved Python exceptions class:
the values for eq, order, and hash are ignored and the instances compare and hash by the instance’s ids (N.B. attrs will not remove existing implementations of
__hash__
or the equality methods. It just won’t add own ones.),all attributes that are either passed into
__init__
or have a default value are additionally available as a tuple in theargs
attribute,the value of str is ignored leaving
__str__
to base classes.
collect_by_mro (bool) –
Setting this to True fixes the way attrs collects attributes from base classes. The default behavior is incorrect in certain cases of multiple inheritance. It should be on by default but is kept off for backward-compatibility.
See also
Issue #428
getstate_setstate (bool | None) –
Note
This is usually only interesting for slotted classes and you should probably just set auto_detect to True.
If True,
__getstate__
and__setstate__
are generated and attached to the class. This is necessary for slotted classes to be pickleable. If left None, it’s True by default for slotted classes andFalse
for dict classes.If auto_detect is True, and getstate_setstate is left None, and either
__getstate__
or__setstate__
is detected directly on the class (i.e. not inherited), it is set to False (this is usually what you want).on_setattr (callable, or a list of callables, or None, or attrs.setters.NO_OP) –
A callable that is run whenever the user attempts to set an attribute (either by assignment like
i.x = 42
or by using setattr likesetattr(i, "x", 42)
). It receives the same arguments as validators: the instance, the attribute that is being modified, and the new value.If no exception is raised, the attribute is set to the return value of the callable.
If a list of callables is passed, they’re automatically wrapped in an attrs.setters.pipe.
field_transformer (callable | None) –
A function that is called with the original class object and all fields right before attrs finalizes the class. You can use this, e.g., to automatically add converters or validators to fields based on their types.
See also
transform-fields
match_args (bool) – If True (default), set
__match_args__
on the class to support PEP 634 (Structural Pattern Matching). It is a tuple of all non-keyword-only__init__
parameter names on Python 3.10 and later. Ignored on older Python versions.
New in version 16.0.0: slots
New in version 16.1.0: frozen
New in version 16.3.0: str
New in version 16.3.0: Support for
__attrs_post_init__
.Changed in version 17.1.0: hash supports
None
as value which is also the default now.New in version 17.3.0: auto_attribs
Changed in version 18.1.0: If these is passed, no attributes are deleted from the class body.
Changed in version 18.1.0: If these is ordered, the order is retained.
New in version 18.2.0: weakref_slot
Deprecated since version 18.2.0:
__lt__
,__le__
,__gt__
, and__ge__
now raise a DeprecationWarning if the classes compared are subclasses of each other.__eq
and__ne__
never tried to compared subclasses to each other.Changed in version 19.2.0:
__lt__
,__le__
,__gt__
, and__ge__
now do not consider subclasses comparable anymore.New in version 18.2.0: kw_only
New in version 18.2.0: cache_hash
New in version 19.1.0: auto_exc
Deprecated since version 19.2.0: cmp Removal on or after 2021-06-01.
New in version 19.2.0: eq and order
New in version 20.1.0: auto_detect
New in version 20.1.0: collect_by_mro
New in version 20.1.0: getstate_setstate
New in version 20.1.0: on_setattr
New in version 20.3.0: field_transformer
Changed in version 21.1.0:
init=False
injects__attrs_init__
Changed in version 21.1.0: Support for
__attrs_pre_init__
Changed in version 21.1.0: cmp undeprecated
New in version 21.3.0: match_args
New in version 22.2.0: unsafe_hash as an alias for hash (for PEP 681 compliance).