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

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, str)), 

17 frozenset(cls for cls in what if isinstance(cls, Attribute)), 

18 ) 

19 

20 

21def include(*what): 

22 """ 

23 Include *what*. 

24 

25 :param what: What to include. 

26 :type what: `list` of classes `type`, field names `str` or 

27 `attrs.Attribute`\\ s 

28 

29 :rtype: `callable` 

30 

31 .. versionchanged:: 23.1.0 Accept strings with field names. 

32 """ 

33 cls, names, attrs = _split_what(what) 

34 

35 def include_(attribute, value): 

36 return ( 

37 value.__class__ in cls 

38 or attribute.name in names 

39 or attribute in attrs 

40 ) 

41 

42 return include_ 

43 

44 

45def exclude(*what): 

46 """ 

47 Exclude *what*. 

48 

49 :param what: What to exclude. 

50 :type what: `list` of classes `type`, field names `str` or 

51 `attrs.Attribute`\\ s. 

52 

53 :rtype: `callable` 

54 

55 .. versionchanged:: 23.3.0 Accept field name string as input argument 

56 """ 

57 cls, names, attrs = _split_what(what) 

58 

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 ) 

65 

66 return exclude_