Return the ``attrs`` attribute values of *inst* as a tuple. Optionally recurse into other ``attrs``-decorated classes. :param inst: Instance of an ``attrs``-decorated class. :param bool recurse: Recurse into classes that are also ``attrs``-decorated. :param callable fi
(
inst,
recurse=True,
filter=None,
tuple_factory=tuple,
retain_collection_types=False,
)
| 190 | |
| 191 | |
| 192 | def astuple( |
| 193 | inst, |
| 194 | recurse=True, |
| 195 | filter=None, |
| 196 | tuple_factory=tuple, |
| 197 | retain_collection_types=False, |
| 198 | ): |
| 199 | """ |
| 200 | Return the ``attrs`` attribute values of *inst* as a tuple. |
| 201 | |
| 202 | Optionally recurse into other ``attrs``-decorated classes. |
| 203 | |
| 204 | :param inst: Instance of an ``attrs``-decorated class. |
| 205 | :param bool recurse: Recurse into classes that are also |
| 206 | ``attrs``-decorated. |
| 207 | :param callable filter: A callable whose return code determines whether an |
| 208 | attribute or element is included (``True``) or dropped (``False``). Is |
| 209 | called with the `attrs.Attribute` as the first argument and the |
| 210 | value as the second argument. |
| 211 | :param callable tuple_factory: A callable to produce tuples from. For |
| 212 | example, to produce lists instead of tuples. |
| 213 | :param bool retain_collection_types: Do not convert to ``list`` |
| 214 | or ``dict`` when encountering an attribute which type is |
| 215 | ``tuple``, ``dict`` or ``set``. Only meaningful if ``recurse`` is |
| 216 | ``True``. |
| 217 | |
| 218 | :rtype: return type of *tuple_factory* |
| 219 | |
| 220 | :raise attr.exceptions.NotAnAttrsClassError: If *cls* is not an ``attrs`` |
| 221 | class. |
| 222 | |
| 223 | .. versionadded:: 16.2.0 |
| 224 | """ |
| 225 | attrs = fields(inst.__class__) |
| 226 | rv = [] |
| 227 | retain = retain_collection_types # Very long. :/ |
| 228 | for a in attrs: |
| 229 | v = getattr(inst, a.name) |
| 230 | if filter is not None and not filter(a, v): |
| 231 | continue |
| 232 | if recurse is True: |
| 233 | if has(v.__class__): |
| 234 | rv.append( |
| 235 | astuple( |
| 236 | v, |
| 237 | recurse=True, |
| 238 | filter=filter, |
| 239 | tuple_factory=tuple_factory, |
| 240 | retain_collection_types=retain, |
| 241 | ) |
| 242 | ) |
| 243 | elif isinstance(v, (tuple, list, set, frozenset)): |
| 244 | cf = v.__class__ if retain is True else list |
| 245 | rv.append( |
| 246 | cf( |
| 247 | [ |
| 248 | astuple( |
| 249 | j, |