Skip to content

Commit 60b0b14

Browse files
committed
remove deprecated config api
1 parent 1e01db9 commit 60b0b14

File tree

3 files changed

+3
-137
lines changed

3 files changed

+3
-137
lines changed

pytensor/configdefaults.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from setuptools._distutils.spawn import find_executable
1212

1313
import pytensor
14-
import pytensor.configparser
1514
from pytensor.configparser import (
1615
BoolParam,
1716
ConfigParam,
@@ -20,6 +19,7 @@
2019
FloatParam,
2120
IntParam,
2221
StrParam,
22+
_create_default_config,
2323
)
2424
from pytensor.utils import (
2525
LOCAL_BITWIDTH,
@@ -1444,12 +1444,8 @@ def add_caching_dir_configvars():
14441444
)
14451445

14461446
# Eventually, the instance of `PyTensorConfigParser` should be created right here,
1447-
# where it is also populated with settings. But for a transition period, it
1448-
# remains as `configparser._config`, while everybody accessing it through
1449-
# `configparser.config` is flooded with deprecation warnings. These warnings
1450-
# instruct one to use `pytensor.config`, which is an alias for
1451-
# `pytensor.configdefaults.config`.
1452-
config = pytensor.configparser._config
1447+
# where it is also populated with settings.
1448+
config = _create_default_config()
14531449

14541450
# The functions below register config variables into the config instance above.
14551451
add_basic_configvars()

pytensor/configparser.py

-106
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,6 @@ def __exit__(self, *args):
6565
v.__set__(self._root, self.old_vals[k])
6666

6767

68-
class _SectionRedirect:
69-
"""Functions as a mock property on the PyTensorConfigParser.
70-
71-
It redirects attribute access (to config subsectinos) to the
72-
new config variable properties that use "__" in their name.
73-
"""
74-
75-
def __init__(self, root, section_name):
76-
self._root = root
77-
self._section_name = section_name
78-
super().__init__()
79-
80-
def __getattr__(self, attr):
81-
warnings.warn(
82-
f"Accessing section '{attr}' through old .-based API. "
83-
f"This will be removed. Use 'config.{self._section_name}__{attr}' instead.",
84-
DeprecationWarning,
85-
)
86-
return getattr(self._root, f"{self._section_name}__{attr}")
87-
88-
8968
class PyTensorConfigParser:
9069
"""Object that holds configuration settings."""
9170

@@ -189,18 +168,6 @@ def add(self, name, doc, configparam, in_c_key=True):
189168
# the ConfigParam implements __get__/__set__, enabling us to create a property:
190169
setattr(self.__class__, name, configparam)
191170

192-
# The old API used dots for accessing a hierarchy of sections.
193-
# The following code adds redirects that spill DeprecationWarnings
194-
# while allowing backwards-compatible access to dot-based subsections.
195-
# Because the subsectioning is recursive, redirects must be added for
196-
# all levels. For example: ".test", ".test.subsection".
197-
sections = name.split("__")
198-
for s in range(1, len(sections)):
199-
section_name = "__".join(sections[:s])
200-
if not hasattr(self, section_name):
201-
redirect = _SectionRedirect(self, section_name)
202-
setattr(self.__class__, section_name, redirect)
203-
204171
def fetch_val_for_key(self, key, delete_key=False):
205172
"""Return the overriding config value for a key.
206173
A successful search returns a string value.
@@ -565,76 +532,3 @@ def _create_default_config():
565532
pytensor_raw_cfg=pytensor_raw_cfg,
566533
)
567534
return config
568-
569-
570-
class _ConfigProxy:
571-
"""Like _SectionRedirect this class enables backwards-compatible access to the
572-
config settings, but raises DeprecationWarnings with instructions to use `pytensor.config`.
573-
"""
574-
575-
def __init__(self, actual):
576-
_ConfigProxy._actual = actual
577-
578-
def __getattr__(self, attr):
579-
if attr == "_actual":
580-
return _ConfigProxy._actual
581-
warnings.warn(
582-
"`pytensor.configparser.config` is deprecated; use `pytensor.config` instead.",
583-
DeprecationWarning,
584-
stacklevel=2,
585-
)
586-
return getattr(self._actual, attr)
587-
588-
def __setattr__(self, attr, value):
589-
if attr == "_actual":
590-
return setattr(_ConfigProxy._actual, attr, value)
591-
warnings.warn(
592-
"`pytensor.configparser.config` is deprecated; use `pytensor.config` instead.",
593-
DeprecationWarning,
594-
stacklevel=2,
595-
)
596-
return setattr(self._actual, attr, value)
597-
598-
599-
# Create the actual instance of the config. This one should eventually move to
600-
# `configdefaults`:
601-
_config = _create_default_config()
602-
603-
# The old API often imported the default config object from `configparser`.
604-
# These imports/accesses should be replaced with `pytensor.config`, so this wraps
605-
# it with warnings:
606-
config = _ConfigProxy(_config)
607-
608-
DEPRECATED_NAMES = [
609-
(
610-
"change_flags",
611-
"`change_flags` is deprecated; use `pytensor.config.change_flags` instead.",
612-
_config.change_flags,
613-
),
614-
(
615-
"_change_flags",
616-
"`_change_flags` is deprecated; use `pytensor.config.change_flags` instead.",
617-
_config.change_flags,
618-
),
619-
(
620-
"_config_print",
621-
"`_config_print` is deprecated; use `pytensor.config.config_print` instead.",
622-
_config.config_print,
623-
),
624-
]
625-
626-
627-
def __getattr__(name):
628-
"""Intercept module-level attribute access of deprecated symbols.
629-
630-
Adapted from https://stackoverflow.com/a/55139609/3006474.
631-
632-
"""
633-
from warnings import warn
634-
635-
for old_name, msg, old_object in DEPRECATED_NAMES:
636-
if name == old_name:
637-
warn(msg, DeprecationWarning, stacklevel=2)
638-
return old_object
639-
640-
raise AttributeError(f"module {__name__} has no attribute {name}")

tests/test_config.py

-24
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,6 @@ def _create_test_config():
1818
)
1919

2020

21-
def test_api_deprecation_warning():
22-
# accessing through configdefaults.config is the new best practice
23-
with pytest.warns(None):
24-
root = configdefaults.config
25-
assert isinstance(str(root), str)
26-
27-
# accessing through configparser.config is discouraged
28-
root = configparser.config
29-
with pytest.warns(DeprecationWarning, match="instead"):
30-
root.add(
31-
"test_deprecationwarning",
32-
"A config var from a test case.",
33-
configparser.StrParam("test_default"),
34-
)
35-
with pytest.warns(DeprecationWarning, match="instead"):
36-
with root.change_flags(test_deprecationwarning="new_value"):
37-
pass
38-
39-
4021
def test_api_redirect():
4122
root = _create_test_config()
4223
# one section level
@@ -48,9 +29,6 @@ def test_api_redirect():
4829
assert hasattr(root, "test__section_redirect")
4930
assert root.test__section_redirect == "test_default"
5031
assert hasattr(root, "test")
51-
assert isinstance(root.test, configparser._SectionRedirect)
52-
with pytest.warns(DeprecationWarning):
53-
assert root.test.section_redirect == "test_default"
5432

5533
# two section levels
5634
root.add(
@@ -60,8 +38,6 @@ def test_api_redirect():
6038
)
6139
assert hasattr(root, "test__subsection__redirect")
6240
assert root.test__subsection__redirect == "test_default2"
63-
with pytest.warns(DeprecationWarning):
64-
assert root.test.subsection.redirect == "test_default2"
6541

6642

6743
def test_invalid_default():

0 commit comments

Comments
 (0)