test_configurable.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. # encoding: utf-8
  2. """Tests for traitlets.config.configurable"""
  3. # Copyright (c) IPython Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import logging
  6. from unittest import TestCase
  7. from pytest import mark
  8. from traitlets.config.configurable import (
  9. Configurable,
  10. LoggingConfigurable,
  11. SingletonConfigurable,
  12. )
  13. from traitlets.traitlets import (
  14. Integer, Float, Unicode, List, Dict, Set,
  15. _deprecations_shown,
  16. )
  17. from traitlets.config.loader import Config
  18. from six import PY3
  19. from ...tests._warnings import expected_warnings
  20. class MyConfigurable(Configurable):
  21. a = Integer(1, help="The integer a.").tag(config=True)
  22. b = Float(1.0, help="The integer b.").tag(config=True)
  23. c = Unicode('no config')
  24. mc_help=u"""MyConfigurable options
  25. ----------------------
  26. --MyConfigurable.a=<Integer>
  27. Default: 1
  28. The integer a.
  29. --MyConfigurable.b=<Float>
  30. Default: 1.0
  31. The integer b."""
  32. mc_help_inst=u"""MyConfigurable options
  33. ----------------------
  34. --MyConfigurable.a=<Integer>
  35. Current: 5
  36. The integer a.
  37. --MyConfigurable.b=<Float>
  38. Current: 4.0
  39. The integer b."""
  40. # On Python 3, the Integer trait is a synonym for Int
  41. if PY3:
  42. mc_help = mc_help.replace(u"<Integer>", u"<Int>")
  43. mc_help_inst = mc_help_inst.replace(u"<Integer>", u"<Int>")
  44. class Foo(Configurable):
  45. a = Integer(0, help="The integer a.").tag(config=True)
  46. b = Unicode('nope').tag(config=True)
  47. class Bar(Foo):
  48. b = Unicode('gotit', help="The string b.").tag(config=False)
  49. c = Float(help="The string c.").tag(config=True)
  50. class TestConfigurable(TestCase):
  51. def test_default(self):
  52. c1 = Configurable()
  53. c2 = Configurable(config=c1.config)
  54. c3 = Configurable(config=c2.config)
  55. self.assertEqual(c1.config, c2.config)
  56. self.assertEqual(c2.config, c3.config)
  57. def test_custom(self):
  58. config = Config()
  59. config.foo = 'foo'
  60. config.bar = 'bar'
  61. c1 = Configurable(config=config)
  62. c2 = Configurable(config=c1.config)
  63. c3 = Configurable(config=c2.config)
  64. self.assertEqual(c1.config, config)
  65. self.assertEqual(c2.config, config)
  66. self.assertEqual(c3.config, config)
  67. # Test that copies are not made
  68. self.assertTrue(c1.config is config)
  69. self.assertTrue(c2.config is config)
  70. self.assertTrue(c3.config is config)
  71. self.assertTrue(c1.config is c2.config)
  72. self.assertTrue(c2.config is c3.config)
  73. def test_inheritance(self):
  74. config = Config()
  75. config.MyConfigurable.a = 2
  76. config.MyConfigurable.b = 2.0
  77. c1 = MyConfigurable(config=config)
  78. c2 = MyConfigurable(config=c1.config)
  79. self.assertEqual(c1.a, config.MyConfigurable.a)
  80. self.assertEqual(c1.b, config.MyConfigurable.b)
  81. self.assertEqual(c2.a, config.MyConfigurable.a)
  82. self.assertEqual(c2.b, config.MyConfigurable.b)
  83. def test_parent(self):
  84. config = Config()
  85. config.Foo.a = 10
  86. config.Foo.b = "wow"
  87. config.Bar.b = 'later'
  88. config.Bar.c = 100.0
  89. f = Foo(config=config)
  90. with expected_warnings(['`b` not recognized']):
  91. b = Bar(config=f.config)
  92. self.assertEqual(f.a, 10)
  93. self.assertEqual(f.b, 'wow')
  94. self.assertEqual(b.b, 'gotit')
  95. self.assertEqual(b.c, 100.0)
  96. def test_override1(self):
  97. config = Config()
  98. config.MyConfigurable.a = 2
  99. config.MyConfigurable.b = 2.0
  100. c = MyConfigurable(a=3, config=config)
  101. self.assertEqual(c.a, 3)
  102. self.assertEqual(c.b, config.MyConfigurable.b)
  103. self.assertEqual(c.c, 'no config')
  104. def test_override2(self):
  105. config = Config()
  106. config.Foo.a = 1
  107. config.Bar.b = 'or' # Up above b is config=False, so this won't do it.
  108. config.Bar.c = 10.0
  109. with expected_warnings(['`b` not recognized']):
  110. c = Bar(config=config)
  111. self.assertEqual(c.a, config.Foo.a)
  112. self.assertEqual(c.b, 'gotit')
  113. self.assertEqual(c.c, config.Bar.c)
  114. with expected_warnings(['`b` not recognized']):
  115. c = Bar(a=2, b='and', c=20.0, config=config)
  116. self.assertEqual(c.a, 2)
  117. self.assertEqual(c.b, 'and')
  118. self.assertEqual(c.c, 20.0)
  119. def test_help(self):
  120. self.assertEqual(MyConfigurable.class_get_help(), mc_help)
  121. def test_help_inst(self):
  122. inst = MyConfigurable(a=5, b=4)
  123. self.assertEqual(MyConfigurable.class_get_help(inst), mc_help_inst)
  124. class TestSingletonConfigurable(TestCase):
  125. def test_instance(self):
  126. class Foo(SingletonConfigurable): pass
  127. self.assertEqual(Foo.initialized(), False)
  128. foo = Foo.instance()
  129. self.assertEqual(Foo.initialized(), True)
  130. self.assertEqual(foo, Foo.instance())
  131. self.assertEqual(SingletonConfigurable._instance, None)
  132. def test_inheritance(self):
  133. class Bar(SingletonConfigurable): pass
  134. class Bam(Bar): pass
  135. self.assertEqual(Bar.initialized(), False)
  136. self.assertEqual(Bam.initialized(), False)
  137. bam = Bam.instance()
  138. bam == Bar.instance()
  139. self.assertEqual(Bar.initialized(), True)
  140. self.assertEqual(Bam.initialized(), True)
  141. self.assertEqual(bam, Bam._instance)
  142. self.assertEqual(bam, Bar._instance)
  143. self.assertEqual(SingletonConfigurable._instance, None)
  144. class MyParent(Configurable):
  145. pass
  146. class MyParent2(MyParent):
  147. pass
  148. class TestParentConfigurable(TestCase):
  149. def test_parent_config(self):
  150. cfg = Config({
  151. 'MyParent' : {
  152. 'MyConfigurable' : {
  153. 'b' : 2.0,
  154. }
  155. }
  156. })
  157. parent = MyParent(config=cfg)
  158. myc = MyConfigurable(parent=parent)
  159. self.assertEqual(myc.b, parent.config.MyParent.MyConfigurable.b)
  160. def test_parent_inheritance(self):
  161. cfg = Config({
  162. 'MyParent' : {
  163. 'MyConfigurable' : {
  164. 'b' : 2.0,
  165. }
  166. }
  167. })
  168. parent = MyParent2(config=cfg)
  169. myc = MyConfigurable(parent=parent)
  170. self.assertEqual(myc.b, parent.config.MyParent.MyConfigurable.b)
  171. def test_multi_parent(self):
  172. cfg = Config({
  173. 'MyParent2' : {
  174. 'MyParent' : {
  175. 'MyConfigurable' : {
  176. 'b' : 2.0,
  177. }
  178. },
  179. # this one shouldn't count
  180. 'MyConfigurable' : {
  181. 'b' : 3.0,
  182. },
  183. }
  184. })
  185. parent2 = MyParent2(config=cfg)
  186. parent = MyParent(parent=parent2)
  187. myc = MyConfigurable(parent=parent)
  188. self.assertEqual(myc.b, parent.config.MyParent2.MyParent.MyConfigurable.b)
  189. def test_parent_priority(self):
  190. cfg = Config({
  191. 'MyConfigurable' : {
  192. 'b' : 2.0,
  193. },
  194. 'MyParent' : {
  195. 'MyConfigurable' : {
  196. 'b' : 3.0,
  197. }
  198. },
  199. 'MyParent2' : {
  200. 'MyConfigurable' : {
  201. 'b' : 4.0,
  202. }
  203. }
  204. })
  205. parent = MyParent2(config=cfg)
  206. myc = MyConfigurable(parent=parent)
  207. self.assertEqual(myc.b, parent.config.MyParent2.MyConfigurable.b)
  208. def test_multi_parent_priority(self):
  209. cfg = Config({
  210. 'MyConfigurable' : {
  211. 'b' : 2.0,
  212. },
  213. 'MyParent' : {
  214. 'MyConfigurable' : {
  215. 'b' : 3.0,
  216. }
  217. },
  218. 'MyParent2' : {
  219. 'MyConfigurable' : {
  220. 'b' : 4.0,
  221. }
  222. },
  223. 'MyParent2' : {
  224. 'MyParent' : {
  225. 'MyConfigurable' : {
  226. 'b' : 5.0,
  227. }
  228. }
  229. }
  230. })
  231. parent2 = MyParent2(config=cfg)
  232. parent = MyParent2(parent=parent2)
  233. myc = MyConfigurable(parent=parent)
  234. self.assertEqual(myc.b, parent.config.MyParent2.MyParent.MyConfigurable.b)
  235. class Containers(Configurable):
  236. lis = List().tag(config=True)
  237. def _lis_default(self):
  238. return [-1]
  239. s = Set().tag(config=True)
  240. def _s_default(self):
  241. return {'a'}
  242. d = Dict().tag(config=True)
  243. def _d_default(self):
  244. return {'a' : 'b'}
  245. class TestConfigContainers(TestCase):
  246. def test_extend(self):
  247. c = Config()
  248. c.Containers.lis.extend(list(range(5)))
  249. obj = Containers(config=c)
  250. self.assertEqual(obj.lis, list(range(-1,5)))
  251. def test_insert(self):
  252. c = Config()
  253. c.Containers.lis.insert(0, 'a')
  254. c.Containers.lis.insert(1, 'b')
  255. obj = Containers(config=c)
  256. self.assertEqual(obj.lis, ['a', 'b', -1])
  257. def test_prepend(self):
  258. c = Config()
  259. c.Containers.lis.prepend([1,2])
  260. c.Containers.lis.prepend([2,3])
  261. obj = Containers(config=c)
  262. self.assertEqual(obj.lis, [2,3,1,2,-1])
  263. def test_prepend_extend(self):
  264. c = Config()
  265. c.Containers.lis.prepend([1,2])
  266. c.Containers.lis.extend([2,3])
  267. obj = Containers(config=c)
  268. self.assertEqual(obj.lis, [1,2,-1,2,3])
  269. def test_append_extend(self):
  270. c = Config()
  271. c.Containers.lis.append([1,2])
  272. c.Containers.lis.extend([2,3])
  273. obj = Containers(config=c)
  274. self.assertEqual(obj.lis, [-1,[1,2],2,3])
  275. def test_extend_append(self):
  276. c = Config()
  277. c.Containers.lis.extend([2,3])
  278. c.Containers.lis.append([1,2])
  279. obj = Containers(config=c)
  280. self.assertEqual(obj.lis, [-1,2,3,[1,2]])
  281. def test_insert_extend(self):
  282. c = Config()
  283. c.Containers.lis.insert(0, 1)
  284. c.Containers.lis.extend([2,3])
  285. obj = Containers(config=c)
  286. self.assertEqual(obj.lis, [1,-1,2,3])
  287. def test_set_update(self):
  288. c = Config()
  289. c.Containers.s.update({0,1,2})
  290. c.Containers.s.update({3})
  291. obj = Containers(config=c)
  292. self.assertEqual(obj.s, {'a', 0, 1, 2, 3})
  293. def test_dict_update(self):
  294. c = Config()
  295. c.Containers.d.update({'c' : 'd'})
  296. c.Containers.d.update({'e' : 'f'})
  297. obj = Containers(config=c)
  298. self.assertEqual(obj.d, {'a':'b', 'c':'d', 'e':'f'})
  299. def test_update_twice(self):
  300. c = Config()
  301. c.MyConfigurable.a = 5
  302. m = MyConfigurable(config=c)
  303. self.assertEqual(m.a, 5)
  304. c2 = Config()
  305. c2.MyConfigurable.a = 10
  306. m.update_config(c2)
  307. self.assertEqual(m.a, 10)
  308. c2.MyConfigurable.a = 15
  309. m.update_config(c2)
  310. self.assertEqual(m.a, 15)
  311. def test_update_self(self):
  312. """update_config with same config object still triggers config_changed"""
  313. c = Config()
  314. c.MyConfigurable.a = 5
  315. m = MyConfigurable(config=c)
  316. self.assertEqual(m.a, 5)
  317. c.MyConfigurable.a = 10
  318. m.update_config(c)
  319. self.assertEqual(m.a, 10)
  320. def test_config_default(self):
  321. class SomeSingleton(SingletonConfigurable):
  322. pass
  323. class DefaultConfigurable(Configurable):
  324. a = Integer().tag(config=True)
  325. def _config_default(self):
  326. if SomeSingleton.initialized():
  327. return SomeSingleton.instance().config
  328. return Config()
  329. c = Config()
  330. c.DefaultConfigurable.a = 5
  331. d1 = DefaultConfigurable()
  332. self.assertEqual(d1.a, 0)
  333. single = SomeSingleton.instance(config=c)
  334. d2 = DefaultConfigurable()
  335. self.assertIs(d2.config, single.config)
  336. self.assertEqual(d2.a, 5)
  337. def test_config_default_deprecated(self):
  338. """Make sure configurables work even with the deprecations in traitlets"""
  339. class SomeSingleton(SingletonConfigurable):
  340. pass
  341. # reset deprecation limiter
  342. _deprecations_shown.clear()
  343. with expected_warnings([]):
  344. class DefaultConfigurable(Configurable):
  345. a = Integer(config=True)
  346. def _config_default(self):
  347. if SomeSingleton.initialized():
  348. return SomeSingleton.instance().config
  349. return Config()
  350. c = Config()
  351. c.DefaultConfigurable.a = 5
  352. d1 = DefaultConfigurable()
  353. self.assertEqual(d1.a, 0)
  354. single = SomeSingleton.instance(config=c)
  355. d2 = DefaultConfigurable()
  356. self.assertIs(d2.config, single.config)
  357. self.assertEqual(d2.a, 5)
  358. class TestLogger(TestCase):
  359. class A(LoggingConfigurable):
  360. foo = Integer(config=True)
  361. bar = Integer(config=True)
  362. baz = Integer(config=True)
  363. @mark.skipif(not hasattr(TestCase, 'assertLogs'), reason='requires TestCase.assertLogs')
  364. def test_warn_match(self):
  365. logger = logging.getLogger('test_warn_match')
  366. cfg = Config({'A': {'bat': 5}})
  367. with self.assertLogs(logger, logging.WARNING) as captured:
  368. a = TestLogger.A(config=cfg, log=logger)
  369. output = '\n'.join(captured.output)
  370. self.assertIn('Did you mean one of: `bar, baz`?', output)
  371. self.assertIn('Config option `bat` not recognized by `A`.', output)
  372. cfg = Config({'A': {'fool': 5}})
  373. with self.assertLogs(logger, logging.WARNING) as captured:
  374. a = TestLogger.A(config=cfg, log=logger)
  375. output = '\n'.join(captured.output)
  376. self.assertIn('Config option `fool` not recognized by `A`.', output)
  377. self.assertIn('Did you mean `foo`?', output)
  378. cfg = Config({'A': {'totally_wrong': 5}})
  379. with self.assertLogs(logger, logging.WARNING) as captured:
  380. a = TestLogger.A(config=cfg, log=logger)
  381. output = '\n'.join(captured.output)
  382. self.assertIn('Config option `totally_wrong` not recognized by `A`.', output)
  383. self.assertNotIn('Did you mean', output)