Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/attr/filters.py: 36%
14 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
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, str)),
17 frozenset(cls for cls in what if isinstance(cls, Attribute)),
18 )
21def include(*what):
22 """
23 Include *what*.
25 :param what: What to include.
26 :type what: `list` of classes `type`, field names `str` or
27 `attrs.Attribute`\\ s
29 :rtype: `callable`
31 .. versionchanged:: 23.1.0 Accept strings with field names.
32 """
33 cls, names, attrs = _split_what(what)
35 def include_(attribute, value):
36 return (
37 value.__class__ in cls
38 or attribute.name in names
39 or attribute in attrs
40 )
42 return include_
45def exclude(*what):
46 """
47 Exclude *what*.
49 :param what: What to exclude.
50 :type what: `list` of classes `type`, field names `str` or
51 `attrs.Attribute`\\ s.
53 :rtype: `callable`
55 .. versionchanged:: 23.3.0 Accept field name string as input argument
56 """
57 cls, names, attrs = _split_what(what)
59 def exclude_(attribute, value):
60 return not (
61 value.__class__ in cls
62 or attribute.name in names
63 or attribute in attrs
64 )
66 return exclude_