Coverage for /opt/homebrew/lib/python3.11/site-packages/attr/filters.py: 36%
14 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-05-04 13:14 +0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-05-04 13:14 +0700
1# SPDX-License-Identifier: MIT
3"""
4Commonly useful filters for `attr.asdict`.
5"""
7from ._make import Attribute
10def _split_what(what):
11 """
12 Returns a tuple of `frozenset`s of classes and attributes.
13 """
14 return (
15 frozenset(cls for cls in what if isinstance(cls, type)),
16 frozenset(cls for cls in what if isinstance(cls, Attribute)),
17 )
20def include(*what):
21 """
22 Include *what*.
24 :param what: What to include.
25 :type what: `list` of `type` or `attrs.Attribute`\\ s
27 :rtype: `callable`
28 """
29 cls, attrs = _split_what(what)
31 def include_(attribute, value):
32 return value.__class__ in cls or attribute in attrs
34 return include_
37def exclude(*what):
38 """
39 Exclude *what*.
41 :param what: What to exclude.
42 :type what: `list` of classes or `attrs.Attribute`\\ s.
44 :rtype: `callable`
45 """
46 cls, attrs = _split_what(what)
48 def exclude_(attribute, value):
49 return value.__class__ not in cls and attribute not in attrs
51 return exclude_