Skip to content

Commit 755f385

Browse files
michaelosthegebrandonwillard
authored andcommitted
Get rid of most global variables in configparser
1 parent 8e3e839 commit 755f385

File tree

5 files changed

+246
-247
lines changed

5 files changed

+246
-247
lines changed

tests/test_config.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,33 @@
66

77
from theano import configdefaults, configparser
88
from theano.configdefaults import default_blas_ldflags
9-
from theano.configparser import THEANO_FLAGS_DICT, AddConfigVar, ConfigParam
9+
from theano.configparser import ConfigParam
1010

1111

1212
def test_invalid_default():
1313
# Ensure an invalid default value found in the Theano code only causes
1414
# a crash if it is not overridden by the user.
1515

16+
root = configdefaults.config
17+
1618
def validate(val):
1719
if val == "invalid":
1820
raise ValueError("Test-triggered")
1921

2022
with pytest.raises(ValueError, match="Test-triggered"):
2123
# This should raise a ValueError because the default value is
2224
# invalid.
23-
AddConfigVar(
25+
root.add(
2426
"T_config__test_invalid_default_a",
2527
doc="unittest",
2628
configparam=ConfigParam("invalid", validate=validate),
2729
in_c_key=False,
2830
)
2931

30-
THEANO_FLAGS_DICT["T_config__test_invalid_default_b"] = "ok"
32+
root._flags_dict["T_config__test_invalid_default_b"] = "ok"
3133
# This should succeed since we defined a proper value, even
3234
# though the default was invalid.
33-
AddConfigVar(
35+
root.add(
3436
"T_config__test_invalid_default_b",
3537
doc="unittest",
3638
configparam=ConfigParam("invalid", validate=validate),
@@ -39,7 +41,7 @@ def validate(val):
3941

4042
# TODO We should remove these dummy options on test exit.
4143
# Check that the flag has been removed
42-
assert "T_config__test_invalid_default_b" not in THEANO_FLAGS_DICT
44+
assert "T_config__test_invalid_default_b" not in root._flags_dict
4345

4446

4547
@patch("theano.configdefaults.try_blas_flag", return_value=None)
@@ -84,20 +86,19 @@ def test_config_param_apply_and_validation():
8486
def test_config_hash():
8587
# TODO: use custom config instance for the test
8688
root = configparser.config
87-
configparser.AddConfigVar(
89+
root.add(
8890
"test_config_hash",
8991
"A config var from a test case.",
9092
configparser.StrParam("test_default"),
91-
root=root,
9293
)
9394

94-
h0 = configparser.get_config_hash()
95+
h0 = root.get_config_hash()
9596

9697
with configparser.change_flags(test_config_hash="new_value"):
9798
assert root.test_config_hash == "new_value"
98-
h1 = configparser.get_config_hash()
99+
h1 = root.get_config_hash()
99100

100-
h2 = configparser.get_config_hash()
101+
h2 = root.get_config_hash()
101102
assert h1 != h0
102103
assert h2 == h0
103104

@@ -141,11 +142,10 @@ def test_deviceparam(self):
141142
def test_config_context():
142143
# TODO: use custom config instance for the test
143144
root = configparser.config
144-
configparser.AddConfigVar(
145+
root.add(
145146
"test_config_context",
146147
"A config var from a test case.",
147148
configparser.StrParam("test_default"),
148-
root=root,
149149
)
150150
assert hasattr(root, "test_config_context")
151151
assert root.test_config_context == "test_default"
@@ -156,8 +156,9 @@ def test_config_context():
156156

157157

158158
def test_no_more_dotting():
159+
root = configparser.config
159160
with pytest.raises(ValueError, match="Dot-based"):
160-
AddConfigVar(
161+
root.add(
161162
"T_config.something",
162163
doc="unittest",
163164
configparam=ConfigParam("invalid"),

theano/configdefaults.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import numpy as np
1313

1414
import theano
15+
import theano.configparser
1516
from theano.configparser import (
16-
THEANO_FLAGS_DICT,
1717
AddConfigVar,
1818
BoolParam,
1919
ConfigParam,
@@ -23,7 +23,6 @@
2323
FloatParam,
2424
IntParam,
2525
StrParam,
26-
TheanoConfigParser,
2726
)
2827
from theano.misc.windows import call_subprocess_Popen, output_subprocess_Popen
2928
from theano.utils import maybe_add_to_os_environ_pathlist
@@ -2236,7 +2235,7 @@ def default_compiledir():
22362235
# TODO: we should add the configs explicitly to an instance of the TheanoConfigParser
22372236
# Even if we treat it as a singleton, we should implement as if it wasn't, so we can
22382237
# test it independently.
2239-
config = TheanoConfigParser()
2238+
config = theano.configparser.config
22402239
add_basic_configvars()
22412240
add_dnn_configvars()
22422241
add_magma_configvars()
@@ -2272,6 +2271,5 @@ def default_compiledir():
22722271
add_caching_dir_configvars()
22732272

22742273
# Check if there are remaining flags provided by the user through THEANO_FLAGS.
2275-
# TODO: the global variable THEANO_FLAGS_DICT should probably become an attribute on the `config` singleton
2276-
for key in THEANO_FLAGS_DICT.keys():
2274+
for key in config._flags_dict.keys():
22772275
warnings.warn(f"Theano does not recognise this flag: {key}")

0 commit comments

Comments
 (0)