Coverage for colour/utilities/tests/test_deprecation.py: 100%

124 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-15 19:01 +1300

1"""Define the unit tests for the :mod:`colour.utilities.deprecation` module.""" 

2 

3from __future__ import annotations 

4 

5import sys 

6 

7import pytest 

8 

9from colour.utilities import ColourUsageWarning 

10from colour.utilities.deprecation import ( 

11 ArgumentFutureRemove, 

12 ArgumentFutureRename, 

13 ArgumentRemoved, 

14 ArgumentRenamed, 

15 ModuleAPI, 

16 ObjectFutureAccessChange, 

17 ObjectFutureAccessRemove, 

18 ObjectFutureRemove, 

19 ObjectFutureRename, 

20 ObjectRemoved, 

21 ObjectRenamed, 

22 build_API_changes, 

23 get_attribute, 

24 handle_arguments_deprecation, 

25) 

26 

27__author__ = "Colour Developers" 

28__copyright__ = "Copyright 2013 Colour Developers" 

29__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" 

30__maintainer__ = "Colour Developers" 

31__email__ = "colour-developers@colour-science.org" 

32__status__ = "Production" 

33 

34__all__ = [ 

35 "TestObjectRenamed", 

36 "TestObjectRemoved", 

37 "TestObjectFutureRename", 

38 "TestObjectFutureRemove", 

39 "TestObjectFutureAccessChange", 

40 "TestObjectFutureAccessRemove", 

41 "TestArgumentRenamed", 

42 "TestArgumentRemoved", 

43 "TestArgumentFutureRename", 

44 "TestArgumentFutureRemove", 

45 "TestModuleAPI", 

46 "TestGetAttribute", 

47 "TestBuildAPIChanges", 

48 "TestHandleArgumentsDeprecation", 

49] 

50 

51 

52class TestObjectRenamed: 

53 """ 

54 Define :class:`colour.utilities.deprecation.ObjectRenamed` class unit 

55 tests methods. 

56 """ 

57 

58 def test_required_methods(self) -> None: 

59 """Test the presence of required methods.""" 

60 

61 required_methods = ("__str__",) 

62 

63 for method in required_methods: 

64 assert method in dir(ObjectRenamed) 

65 

66 def test__str__(self) -> None: 

67 """ 

68 Test :meth:`colour.utilities.deprecation.ObjectRenamed.__str__` 

69 method. 

70 """ 

71 

72 assert "name" in str(ObjectRenamed("name", "new_name")) 

73 assert "new_name" in str(ObjectRenamed("name", "new_name")) 

74 

75 

76class TestObjectRemoved: 

77 """ 

78 Define :class:`colour.utilities.deprecation.ObjectRemoved` class unit 

79 tests methods. 

80 """ 

81 

82 def test_required_methods(self) -> None: 

83 """Test the presence of required methods.""" 

84 

85 required_methods = ("__str__",) 

86 

87 for method in required_methods: 

88 assert method in dir(ObjectRemoved) 

89 

90 def test__str__(self) -> None: 

91 """ 

92 Test :meth:`colour.utilities.deprecation.ObjectRemoved.__str__` 

93 method. 

94 """ 

95 

96 assert "name" in str(ObjectRemoved("name")) 

97 

98 

99class TestObjectFutureRename: 

100 """ 

101 Define :class:`colour.utilities.deprecation.ObjectFutureRename` class unit 

102 tests methods. 

103 """ 

104 

105 def test_required_methods(self) -> None: 

106 """Test the presence of required methods.""" 

107 

108 required_methods = ("__str__",) 

109 

110 for method in required_methods: 

111 assert method in dir(ObjectFutureRename) 

112 

113 def test__str__(self) -> None: 

114 """ 

115 Test :meth:`colour.utilities.deprecation.ObjectFutureRename.__str__` 

116 method. 

117 """ 

118 

119 assert "name" in str(ObjectFutureRename("name", "new_name")) 

120 assert "new_name" in str(ObjectFutureRename("name", "new_name")) 

121 

122 

123class TestObjectFutureRemove: 

124 """ 

125 Define :class:`colour.utilities.deprecation.ObjectFutureRemove` class unit 

126 tests methods. 

127 """ 

128 

129 def test_required_methods(self) -> None: 

130 """Test the presence of required methods.""" 

131 

132 required_methods = ("__str__",) 

133 

134 for method in required_methods: 

135 assert method in dir(ObjectFutureRemove) 

136 

137 def test__str__(self) -> None: 

138 """ 

139 Test :meth:`colour.utilities.deprecation.ObjectFutureRemove.__str__` 

140 method. 

141 """ 

142 

143 assert "name" in str( 

144 ObjectFutureRemove( 

145 "name", 

146 ) 

147 ) 

148 

149 

150class TestObjectFutureAccessChange: 

151 """ 

152 Define :class:`colour.utilities.deprecation.ObjectFutureAccessChange` 

153 class unit tests methods. 

154 """ 

155 

156 def test_required_methods(self) -> None: 

157 """Test the presence of required methods.""" 

158 

159 required_methods = ("__str__",) 

160 

161 for method in required_methods: 

162 assert method in dir(ObjectFutureAccessChange) 

163 

164 def test__str__(self) -> None: 

165 """ 

166 Test :meth:`colour.utilities.deprecation.\ 

167ObjectFutureAccessChange.__str__` method. 

168 """ 

169 

170 assert "name" in str(ObjectFutureAccessChange("name", "new_access")) 

171 assert "new_access" in str(ObjectFutureAccessChange("name", "new_access")) 

172 

173 

174class TestObjectFutureAccessRemove: 

175 """ 

176 Define :class:`colour.utilities.deprecation.ObjectFutureAccessRemove` 

177 class unit tests methods. 

178 """ 

179 

180 def test_required_methods(self) -> None: 

181 """Test the presence of required methods.""" 

182 

183 required_methods = ("__str__",) 

184 

185 for method in required_methods: 

186 assert method in dir(ObjectFutureAccessRemove) 

187 

188 def test__str__(self) -> None: 

189 """ 

190 Test :meth:`colour.utilities.deprecation.\ 

191ObjectFutureAccessRemove.__str__` method. 

192 """ 

193 

194 assert "name" in str( 

195 ObjectFutureAccessRemove( 

196 "name", 

197 ) 

198 ) 

199 

200 

201class TestArgumentRenamed: 

202 """ 

203 Define :class:`colour.utilities.deprecation.ArgumentRenamed` class unit 

204 tests methods. 

205 """ 

206 

207 def test_required_methods(self) -> None: 

208 """Test the presence of required methods.""" 

209 

210 required_methods = ("__str__",) 

211 

212 for method in required_methods: 

213 assert method in dir(ArgumentRenamed) 

214 

215 def test__str__(self) -> None: 

216 """ 

217 Test :meth:`colour.utilities.deprecation.ArgumentRenamed.__str__` 

218 method. 

219 """ 

220 

221 assert "name" in str(ArgumentRenamed("name", "new_name")) 

222 assert "new_name" in str(ArgumentRenamed("name", "new_name")) 

223 

224 

225class TestArgumentRemoved: 

226 """ 

227 Define :class:`colour.utilities.deprecation.ArgumentRemoved` class unit 

228 tests methods. 

229 """ 

230 

231 def test_required_methods(self) -> None: 

232 """Test the presence of required methods.""" 

233 

234 required_methods = ("__str__",) 

235 

236 for method in required_methods: 

237 assert method in dir(ArgumentRemoved) 

238 

239 def test__str__(self) -> None: 

240 """ 

241 Test :meth:`colour.utilities.deprecation.ArgumentRemoved.__str__` 

242 method. 

243 """ 

244 

245 assert "name" in str(ArgumentRemoved("name")) 

246 

247 

248class TestArgumentFutureRename: 

249 """ 

250 Define :class:`colour.utilities.deprecation.ArgumentFutureRename` class 

251 unit tests methods. 

252 """ 

253 

254 def test_required_methods(self) -> None: 

255 """Test the presence of required methods.""" 

256 

257 required_methods = ("__str__",) 

258 

259 for method in required_methods: 

260 assert method in dir(ArgumentFutureRename) 

261 

262 def test__str__(self) -> None: 

263 """ 

264 Test :meth:`colour.utilities.deprecation.\ 

265ArgumentFutureRename.__str__` method. 

266 """ 

267 

268 assert "name" in str(ArgumentFutureRename("name", "new_name")) 

269 assert "new_name" in str(ArgumentFutureRename("name", "new_name")) 

270 

271 

272class TestArgumentFutureRemove: 

273 """ 

274 Define :class:`colour.utilities.deprecation.ArgumentFutureRemove` class 

275 unit tests methods. 

276 """ 

277 

278 def test_required_methods(self) -> None: 

279 """Test the presence of required methods.""" 

280 

281 required_methods = ("__str__",) 

282 

283 for method in required_methods: 

284 assert method in dir(ArgumentFutureRemove) 

285 

286 def test__str__(self) -> None: 

287 """ 

288 Test :meth:`colour.utilities.deprecation.\ 

289ArgumentFutureRemove.__str__` method. 

290 """ 

291 

292 assert "name" in str( 

293 ArgumentFutureRemove( 

294 "name", 

295 ) 

296 ) 

297 

298 

299class TestModuleAPI: 

300 """ 

301 Define :class:`colour.utilities.deprecation.ModuleAPI` class unit tests 

302 methods. 

303 """ 

304 

305 def test_required_methods(self) -> None: 

306 """Test the presence of required methods.""" 

307 

308 required_methods = ("__init__", "__getattr__", "__dir__") 

309 

310 for method in required_methods: 

311 assert method in dir(ModuleAPI) 

312 

313 def test__getattr__(self) -> None: 

314 """ 

315 Test :meth:`colour.utilities.deprecation.ModuleAPI.__getattr__` 

316 method. 

317 """ 

318 

319 import colour.utilities.tests.test_deprecated # noqa: PLC0415 

320 

321 assert colour.utilities.tests.test_deprecated.NAME is None 

322 

323 def assert_warns() -> None: 

324 """Help to test the runtime warning.""" 

325 

326 colour.utilities.tests.test_deprecated.OLD_NAME # noqa: B018 # pyright: ignore 

327 

328 pytest.warns(ColourUsageWarning, assert_warns) 

329 

330 del sys.modules["colour.utilities.tests.test_deprecated"] 

331 

332 def test_raise_exception__getattr__(self) -> None: 

333 """ 

334 Test :func:`colour.utilities.deprecation.ModuleAPI.__getattr__` 

335 method raised exception. 

336 """ 

337 

338 import colour.utilities.tests.test_deprecated # noqa: PLC0415 

339 

340 pytest.raises( 

341 AttributeError, 

342 getattr, 

343 colour.utilities.tests.test_deprecated, 

344 "REMOVED", 

345 ) 

346 

347 del sys.modules["colour.utilities.tests.test_deprecated"] 

348 

349 

350class TestGetAttribute: 

351 """ 

352 Define :func:`colour.utilities.deprecation.get_attribute` definition unit 

353 tests methods. 

354 """ 

355 

356 def test_get_attribute(self) -> None: 

357 """Test :func:`colour.utilities.deprecation.get_attribute` definition.""" 

358 

359 from colour import adaptation # noqa: PLC0415 

360 

361 assert get_attribute("colour.adaptation") is adaptation 

362 

363 from colour.models import eotf_inverse_sRGB # noqa: PLC0415 

364 

365 assert get_attribute("colour.models.eotf_inverse_sRGB") is eotf_inverse_sRGB 

366 

367 from colour.utilities.array import as_float # noqa: PLC0415 

368 

369 assert get_attribute("colour.utilities.array.as_float") is as_float 

370 

371 if "colour.utilities.tests.test_deprecated" in sys.modules: # pragma: no cover 

372 del sys.modules["colour.utilities.tests.test_deprecated"] 

373 

374 attribute = get_attribute("colour.utilities.tests.test_deprecated.NEW_NAME") 

375 

376 import colour.utilities.tests.test_deprecated # noqa: PLC0415 

377 

378 assert attribute is colour.utilities.tests.test_deprecated.NEW_NAME 

379 del sys.modules["colour.utilities.tests.test_deprecated"] 

380 

381 

382class TestBuildAPIChanges: 

383 """ 

384 Define :func:`colour.utilities.deprecation.build_API_changes` definition 

385 unit tests methods. 

386 """ 

387 

388 def test_build_API_changes(self) -> None: 

389 """ 

390 Test :func:`colour.utilities.deprecation.build_API_changes` 

391 definition. 

392 """ 

393 

394 changes = build_API_changes( 

395 { 

396 "ObjectRenamed": [ 

397 [ 

398 "module.object_1_name", 

399 "module.object_1_new_name", 

400 ] 

401 ], 

402 "ObjectFutureRename": [ 

403 [ 

404 "module.object_2_name", 

405 "module.object_2_new_name", 

406 ] 

407 ], 

408 "ObjectFutureAccessChange": [ 

409 [ 

410 "module.object_3_access", 

411 "module.sub_module.object_3_new_access", 

412 ] 

413 ], 

414 "ObjectRemoved": ["module.object_4_name"], 

415 "ObjectFutureRemove": ["module.object_5_name"], 

416 "ObjectFutureAccessRemove": ["module.object_6_access"], 

417 "ArgumentRenamed": [ 

418 [ 

419 "argument_1_name", 

420 "argument_1_new_name", 

421 ] 

422 ], 

423 "ArgumentFutureRename": [ 

424 [ 

425 "argument_2_name", 

426 "argument_2_new_name", 

427 ] 

428 ], 

429 "ArgumentRemoved": ["argument_3_name"], 

430 "ArgumentFutureRemove": ["argument_4_name"], 

431 } 

432 ) 

433 for name, change_type in ( 

434 ("object_1_name", ObjectRenamed), 

435 ("object_2_name", ObjectFutureRename), 

436 ("object_3_access", ObjectFutureAccessChange), 

437 ("object_4_name", ObjectRemoved), 

438 ("object_5_name", ObjectFutureRemove), 

439 ("object_6_access", ObjectFutureAccessRemove), 

440 ("argument_1_name", ArgumentRenamed), 

441 ("argument_2_name", ArgumentFutureRename), 

442 ("argument_3_name", ArgumentRemoved), 

443 ("argument_4_name", ArgumentFutureRemove), 

444 ): 

445 assert isinstance(changes[name], change_type) 

446 

447 

448class TestHandleArgumentsDeprecation: 

449 """ 

450 Define :func:`colour.utilities.deprecation.handle_arguments_deprecation` 

451 definition unit tests methods. 

452 """ 

453 

454 def test_handle_arguments_deprecation(self) -> None: 

455 """ 

456 Test :func:`colour.utilities.deprecation.handle_arguments_deprecation` 

457 definition. 

458 """ 

459 

460 changes = { 

461 "ArgumentRenamed": [ 

462 [ 

463 "argument_1_name", 

464 "argument_1_new_name", 

465 ] 

466 ], 

467 "ArgumentFutureRename": [ 

468 [ 

469 "argument_2_name", 

470 "argument_2_new_name", 

471 ] 

472 ], 

473 "ArgumentRemoved": ["argument_3_name"], 

474 "ArgumentFutureRemove": ["argument_4_name"], 

475 } 

476 

477 assert handle_arguments_deprecation( 

478 changes, 

479 argument_1_name=True, 

480 argument_2_name=True, 

481 argument_3_name=True, 

482 argument_4_name=True, 

483 argument_5_name=True, 

484 ) == { 

485 "argument_1_new_name": True, 

486 "argument_2_new_name": True, 

487 "argument_4_name": True, 

488 "argument_5_name": True, 

489 }