MCPcopy Index your code
hub / github.com/ipython/ipython / test_reload_class_attributes

Method test_reload_class_attributes

tests/test_zzz_autoreload.py:244–303  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

242 self.shell.run_code("p = pickle.dumps(test)")
243
244 def test_reload_class_attributes(self):
245 self.shell.magic_autoreload("2")
246 mod_name, mod_fn = self.new_module(
247 textwrap.dedent(
248 """
249 class MyClass:
250
251 def __init__(self, a=10):
252 self.a = a
253 self.b = 22
254 # self.toto = 33
255
256 def square(self):
257 print('compute square')
258 return self.a*self.a
259 """
260 )
261 )
262 self.shell.run_code("from %s import MyClass" % mod_name)
263 self.shell.run_code("first = MyClass(5)")
264 self.shell.run_code("first.square()")
265 with self.assertRaises(AttributeError):
266 self.shell.run_code("first.cube()")
267 with self.assertRaises(AttributeError):
268 self.shell.run_code("first.power(5)")
269 self.shell.run_code("first.b")
270 with self.assertRaises(AttributeError):
271 self.shell.run_code("first.toto")
272
273 # remove square, add power
274
275 self.write_file(
276 mod_fn,
277 textwrap.dedent(
278 """
279 class MyClass:
280
281 def __init__(self, a=10):
282 self.a = a
283 self.b = 11
284
285 def power(self, p):
286 print('compute power '+str(p))
287 return self.a**p
288 """
289 ),
290 )
291
292 self.shell.run_code("second = MyClass(5)")
293
294 for object_name in {"first", "second"}:
295 self.shell.run_code(f"{object_name}.power(5)")
296 with self.assertRaises(AttributeError):
297 self.shell.run_code(f"{object_name}.cube()")
298 with self.assertRaises(AttributeError):
299 self.shell.run_code(f"{object_name}.square()")
300 self.shell.run_code(f"{object_name}.b")
301 self.shell.run_code(f"{object_name}.a")

Callers

nothing calls this directly

Calls 4

magic_autoreloadMethod · 0.45
new_moduleMethod · 0.45
run_codeMethod · 0.45
write_fileMethod · 0.45

Tested by

no test coverage detected