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

1# SPDX-License-Identifier: MIT 

2 

3""" 

4Commonly useful filters for `attr.asdict`. 

5""" 

6 

7from ._make import Attribute 

8 

9 

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 ) 

18 

19 

20def include(*what): 

21 """ 

22 Include *what*. 

23 

24 :param what: What to include. 

25 :type what: `list` of `type` or `attrs.Attribute`\\ s 

26 

27 :rtype: `callable` 

28 """ 

29 cls, attrs = _split_what(what) 

30 

31 def include_(attribute, value): 

32 return value.__class__ in cls or attribute in attrs 

33 

34 return include_ 

35 

36 

37def exclude(*what): 

38 """ 

39 Exclude *what*. 

40 

41 :param what: What to exclude. 

42 :type what: `list` of classes or `attrs.Attribute`\\ s. 

43 

44 :rtype: `callable` 

45 """ 

46 cls, attrs = _split_what(what) 

47 

48 def exclude_(attribute, value): 

49 return value.__class__ not in cls and attribute not in attrs 

50 

51 return exclude_