random.random() and other methods of the global random state do not compare as equal to themselves after a pickle roundtrip
(module)
| 1284 | ["random", pytest.param("np.random", marks=pytest.mark.skipif("not np"))], |
| 1285 | ) |
| 1286 | def test_tokenize_random_functions(module): |
| 1287 | """random.random() and other methods of the global random state do not compare as |
| 1288 | equal to themselves after a pickle roundtrip""" |
| 1289 | module = eval(module) |
| 1290 | module.seed(2) |
| 1291 | |
| 1292 | a = module.random |
| 1293 | b = pickle.loads(pickle.dumps(a)) |
| 1294 | assert check_tokenize(a) == check_tokenize(b) |
| 1295 | |
| 1296 | # Drawing elements or reseeding changes the global state |
| 1297 | a() |
| 1298 | c = pickle.loads(pickle.dumps(a)) |
| 1299 | assert check_tokenize(a) == check_tokenize(c) |
| 1300 | assert check_tokenize(a) != check_tokenize(b) |
| 1301 | |
| 1302 | module.seed(123) |
| 1303 | d = pickle.loads(pickle.dumps(a)) |
| 1304 | assert check_tokenize(a) == check_tokenize(d) |
| 1305 | assert check_tokenize(a) != check_tokenize(c) |
| 1306 | |
| 1307 | |
| 1308 | def test_tokenize_random_functions_with_state(): |
nothing calls this directly
no test coverage detected