Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

""" 

Test for nested class Parent 

 

This file contains a discussion, examples, and tests about nested 

classes and parents. It is kept in a separate file to avoid import 

loops. 

 

EXAMPLES: 

 

Currently pickling fails for parents using nested classes (typically 

for categories), but deriving only from Parent:: 

 

sage: from sage.misc.nested_class_test import TestParent1, TestParent2, TestParent3, TestParent4 

sage: P = TestParent1() 

sage: TestSuite(P).run() 

Failure ... 

The following tests failed: _test_elements, _test_pickling 

 

They actually need to be in the NestedClassMetaclass. However, due to 

a technical detail, this is currently not directly supported:: 

 

sage: P = TestParent2() 

Traceback (most recent call last): 

... 

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases 

sage: TestSuite(P).run() # not tested 

 

Instead, the easiest is to inherit from UniqueRepresentation, which is 

what you want to do anyway most of the time:: 

 

sage: P = TestParent3() 

sage: TestSuite(P).run() 

 

This is what all Sage's parents using categories currently do. An 

alternative is to use ClasscallMetaclass as metaclass:: 

 

sage: P = TestParent4() 

sage: TestSuite(P).run() 

 

""" 

#***************************************************************************** 

# Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#****************************************************************************** 

from __future__ import print_function, absolute_import 

from six import add_metaclass 

 

__all__ = [] # Don't document any parents 

 

from sage.structure.parent import Parent 

from sage.structure.element_wrapper import ElementWrapper 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.misc.classcall_metaclass import ClasscallMetaclass 

from sage.misc.nested_class import NestedClassMetaclass 

 

class TestParent1(Parent): 

def __init__(self): 

""" 

EXAMPLES:: 

 

sage: sage.misc.nested_class_test.TestParent1() 

<sage.misc.nested_class_test.TestParent1_with_category object at ...> 

""" 

from sage.categories.all import Sets 

Parent.__init__(self, category = Sets()) 

 

class Element(ElementWrapper): 

pass 

 

 

@add_metaclass(NestedClassMetaclass) 

class TestParent2(Parent): 

def __init__(self): 

""" 

EXAMPLES:: 

 

sage: sage.misc.nested_class_test.TestParent2() 

Traceback (most recent call last): 

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases 

""" 

from sage.categories.all import Sets 

Parent.__init__(self, category = Sets()) 

 

class Element(ElementWrapper): 

pass 

 

class TestParent3(UniqueRepresentation, Parent): 

 

def __init__(self): 

""" 

EXAMPLES:: 

 

sage: sage.misc.nested_class_test.TestParent3() 

<sage.misc.nested_class_test.TestParent3_with_category object at ...> 

""" 

from sage.categories.all import Sets 

Parent.__init__(self, category = Sets()) 

 

class Element(ElementWrapper): 

pass 

 

 

@add_metaclass(ClasscallMetaclass) 

class TestParent4(Parent): 

def __init__(self): 

""" 

EXAMPLES:: 

 

sage: sage.misc.nested_class_test.TestParent4() 

<sage.misc.nested_class_test.TestParent4_with_category object at ...> 

""" 

from sage.categories.all import Sets 

Parent.__init__(self, category = Sets()) 

 

def __eq__(self, other): 

""" 

EXAMPLES:: 

 

sage: from sage.misc.nested_class_test import TestParent4 

sage: TestParent4() == TestParent4() 

True 

""" 

return self.__class__ == other.__class__ 

 

def __ne__(self, other): 

""" 

EXAMPLES:: 

 

sage: from sage.misc.nested_class_test import TestParent4 

sage: TestParent4() != TestParent4() 

False 

""" 

return self.__class__ != other.__class__ 

 

class Element(ElementWrapper): 

pass 

 

 

# Class for tests: 

class B(object): 

""" 

A normal external class. 

""" 

pass 

 

class ABB(object): 

class B(object): 

""" 

This class is broken and can't be pickled. 

A warning is emmited during compilation. 

""" 

pass 

 

class ABL(object): 

""" 

There is no problem here. 

""" 

B=B 

 

class ALB(object): 

""" 

There is a nested class just below. Which can't be properly sphinxed. 

""" 

class C(object): 

""" 

Internal C class. 

 

Thanks to the links below this class is pickled ok. 

But it is sphixed wrong: It is typeset as a link to an outer class. 

""" 

pass 

 

C = ALB.C 

 

 

@add_metaclass(NestedClassMetaclass) 

class ABBMeta(object): 

class B(object): 

""" 

B interne 

""" 

pass 

 

 

@add_metaclass(NestedClassMetaclass) 

class ABLMeta(object): 

B = B 

 

 

@add_metaclass(NestedClassMetaclass) 

class ALBMeta(object): 

""" 

There is a nested class just below which is properly sphinxed. 

""" 

class CMeta(object): 

""" 

B interne 

""" 

pass 

 

CMeta = ALBMeta.CMeta 

 

class TestNestedParent(UniqueRepresentation, Parent): 

""" 

This is a dummy for testing source inspection of nested classes. 

 

EXAMPLES:: 

 

sage: from sage.misc.nested_class_test import TestNestedParent 

sage: from sage.misc.sageinspect import sage_getsource 

sage: P = TestNestedParent() 

sage: E = P.element_class 

sage: E.__bases__ 

(<class sage.misc.nested_class_test.TestNestedParent.Element at ...>, 

<class 'sage.categories.sets_cat.Sets.element_class'>) 

sage: print(sage_getsource(E)) 

class Element: 

"This is a dummy element class" 

pass 

 

""" 

class Element: 

"This is a dummy element class" 

pass