Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/numpy/_globals.py: 72%

31 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Module defining global singleton classes. 

3 

4This module raises a RuntimeError if an attempt to reload it is made. In that 

5way the identities of the classes defined here are fixed and will remain so 

6even if numpy itself is reloaded. In particular, a function like the following 

7will still work correctly after numpy is reloaded:: 

8 

9 def foo(arg=np._NoValue): 

10 if arg is np._NoValue: 

11 ... 

12 

13That was not the case when the singleton classes were defined in the numpy 

14``__init__.py`` file. See gh-7844 for a discussion of the reload problem that 

15motivated this module. 

16 

17""" 

18import enum 

19 

20__ALL__ = [ 

21 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', 

22 '_NoValue', '_CopyMode' 

23 ] 

24 

25 

26# Disallow reloading this module so as to preserve the identities of the 

27# classes defined here. 

28if '_is_loaded' in globals(): 28 ↛ 29line 28 didn't jump to line 29, because the condition on line 28 was never true

29 raise RuntimeError('Reloading numpy._globals is not allowed') 

30_is_loaded = True 

31 

32 

33class ModuleDeprecationWarning(DeprecationWarning): 

34 """Module deprecation warning. 

35 

36 The nose tester turns ordinary Deprecation warnings into test failures. 

37 That makes it hard to deprecate whole modules, because they get 

38 imported by default. So this is a special Deprecation warning that the 

39 nose tester will let pass without making tests fail. 

40 

41 """ 

42 

43 

44ModuleDeprecationWarning.__module__ = 'numpy' 

45 

46 

47class VisibleDeprecationWarning(UserWarning): 

48 """Visible deprecation warning. 

49 

50 By default, python will not show deprecation warnings, so this class 

51 can be used when a very visible warning is helpful, for example because 

52 the usage is most likely a user bug. 

53 

54 """ 

55 

56 

57VisibleDeprecationWarning.__module__ = 'numpy' 

58 

59 

60class _NoValueType: 

61 """Special keyword value. 

62 

63 The instance of this class may be used as the default value assigned to a 

64 keyword if no other obvious default (e.g., `None`) is suitable, 

65 

66 Common reasons for using this keyword are: 

67 

68 - A new keyword is added to a function, and that function forwards its 

69 inputs to another function or method which can be defined outside of 

70 NumPy. For example, ``np.std(x)`` calls ``x.std``, so when a ``keepdims`` 

71 keyword was added that could only be forwarded if the user explicitly 

72 specified ``keepdims``; downstream array libraries may not have added 

73 the same keyword, so adding ``x.std(..., keepdims=keepdims)`` 

74 unconditionally could have broken previously working code. 

75 - A keyword is being deprecated, and a deprecation warning must only be 

76 emitted when the keyword is used. 

77 

78 """ 

79 __instance = None 

80 def __new__(cls): 

81 # ensure that only one instance exists 

82 if not cls.__instance: 82 ↛ 84line 82 didn't jump to line 84, because the condition on line 82 was never false

83 cls.__instance = super().__new__(cls) 

84 return cls.__instance 

85 

86 # needed for python 2 to preserve identity through a pickle 

87 def __reduce__(self): 

88 return (self.__class__, ()) 

89 

90 def __repr__(self): 

91 return "<no value>" 

92 

93 

94_NoValue = _NoValueType() 

95 

96 

97class _CopyMode(enum.Enum): 

98 """ 

99 An enumeration for the copy modes supported 

100 by numpy.copy() and numpy.array(). The following three modes are supported, 

101 

102 - ALWAYS: This means that a deep copy of the input 

103 array will always be taken. 

104 - IF_NEEDED: This means that a deep copy of the input 

105 array will be taken only if necessary. 

106 - NEVER: This means that the deep copy will never be taken. 

107 If a copy cannot be avoided then a `ValueError` will be 

108 raised. 

109 

110 Note that the buffer-protocol could in theory do copies. NumPy currently 

111 assumes an object exporting the buffer protocol will never do this. 

112 """ 

113 

114 ALWAYS = True 

115 IF_NEEDED = False 

116 NEVER = 2 

117 

118 def __bool__(self): 

119 # For backwards compatibility 

120 if self == _CopyMode.ALWAYS: 

121 return True 

122 

123 if self == _CopyMode.IF_NEEDED: 

124 return False 

125 

126 raise ValueError(f"{self} is neither True nor False.") 

127 

128 

129_CopyMode.__module__ = 'numpy'