提交 b6fb55d5 authored 作者: Maxim Kochurov's avatar Maxim Kochurov 提交者: Maxim Kochurov

remove deprecated config api

上级 0013aea5
...@@ -11,7 +11,6 @@ import numpy as np ...@@ -11,7 +11,6 @@ import numpy as np
from setuptools._distutils.spawn import find_executable from setuptools._distutils.spawn import find_executable
import pytensor import pytensor
import pytensor.configparser
from pytensor.configparser import ( from pytensor.configparser import (
BoolParam, BoolParam,
ConfigParam, ConfigParam,
...@@ -20,6 +19,7 @@ from pytensor.configparser import ( ...@@ -20,6 +19,7 @@ from pytensor.configparser import (
FloatParam, FloatParam,
IntParam, IntParam,
StrParam, StrParam,
_create_default_config,
) )
from pytensor.utils import ( from pytensor.utils import (
LOCAL_BITWIDTH, LOCAL_BITWIDTH,
...@@ -1195,27 +1195,6 @@ def add_vm_configvars(): ...@@ -1195,27 +1195,6 @@ def add_vm_configvars():
) )
def add_deprecated_configvars():
# TODO: remove this?
config.add(
"unittests__rseed",
"Seed to use for randomized unit tests. "
"Special value 'random' means using a seed of None.",
StrParam(666, validate=_good_seem_param),
in_c_key=False,
)
config.add(
"warn__round",
"Warn when using `tensor.round` with the default mode. "
"Round changed its default from `half_away_from_zero` to "
"`half_to_even` to have the same default as NumPy.",
BoolParam(_warn_default("0.9")),
in_c_key=False,
)
def add_scan_configvars(): def add_scan_configvars():
config.add( config.add(
"scan__allow_gc", "scan__allow_gc",
...@@ -1444,12 +1423,8 @@ SUPPORTED_DNN_CONV_PRECISION = ( ...@@ -1444,12 +1423,8 @@ SUPPORTED_DNN_CONV_PRECISION = (
) )
# Eventually, the instance of `PyTensorConfigParser` should be created right here, # Eventually, the instance of `PyTensorConfigParser` should be created right here,
# where it is also populated with settings. But for a transition period, it # where it is also populated with settings.
# remains as `configparser._config`, while everybody accessing it through config = _create_default_config()
# `configparser.config` is flooded with deprecation warnings. These warnings
# instruct one to use `pytensor.config`, which is an alias for
# `pytensor.configdefaults.config`.
config = pytensor.configparser._config
# The functions below register config variables into the config instance above. # The functions below register config variables into the config instance above.
add_basic_configvars() add_basic_configvars()
...@@ -1467,7 +1442,6 @@ add_optimizer_configvars() ...@@ -1467,7 +1442,6 @@ add_optimizer_configvars()
# that module, which introduces a circular dependency! # that module, which introduces a circular dependency!
add_metaopt_configvars() add_metaopt_configvars()
add_vm_configvars() add_vm_configvars()
add_deprecated_configvars()
add_numba_configvars() add_numba_configvars()
# TODO: `gcc_version_str` is used by other modules.. Should it become an immutable config var? # TODO: `gcc_version_str` is used by other modules.. Should it become an immutable config var?
......
...@@ -65,27 +65,6 @@ class _ChangeFlagsDecorator: ...@@ -65,27 +65,6 @@ class _ChangeFlagsDecorator:
v.__set__(self._root, self.old_vals[k]) v.__set__(self._root, self.old_vals[k])
class _SectionRedirect:
"""Functions as a mock property on the PyTensorConfigParser.
It redirects attribute access (to config subsectinos) to the
new config variable properties that use "__" in their name.
"""
def __init__(self, root, section_name):
self._root = root
self._section_name = section_name
super().__init__()
def __getattr__(self, attr):
warnings.warn(
f"Accessing section '{attr}' through old .-based API. "
f"This will be removed. Use 'config.{self._section_name}__{attr}' instead.",
DeprecationWarning,
)
return getattr(self._root, f"{self._section_name}__{attr}")
class PyTensorConfigParser: class PyTensorConfigParser:
"""Object that holds configuration settings.""" """Object that holds configuration settings."""
...@@ -189,18 +168,6 @@ class PyTensorConfigParser: ...@@ -189,18 +168,6 @@ class PyTensorConfigParser:
# the ConfigParam implements __get__/__set__, enabling us to create a property: # the ConfigParam implements __get__/__set__, enabling us to create a property:
setattr(self.__class__, name, configparam) setattr(self.__class__, name, configparam)
# The old API used dots for accessing a hierarchy of sections.
# The following code adds redirects that spill DeprecationWarnings
# while allowing backwards-compatible access to dot-based subsections.
# Because the subsectioning is recursive, redirects must be added for
# all levels. For example: ".test", ".test.subsection".
sections = name.split("__")
for s in range(1, len(sections)):
section_name = "__".join(sections[:s])
if not hasattr(self, section_name):
redirect = _SectionRedirect(self, section_name)
setattr(self.__class__, section_name, redirect)
def fetch_val_for_key(self, key, delete_key=False): def fetch_val_for_key(self, key, delete_key=False):
"""Return the overriding config value for a key. """Return the overriding config value for a key.
A successful search returns a string value. A successful search returns a string value.
...@@ -565,76 +532,3 @@ def _create_default_config(): ...@@ -565,76 +532,3 @@ def _create_default_config():
pytensor_raw_cfg=pytensor_raw_cfg, pytensor_raw_cfg=pytensor_raw_cfg,
) )
return config return config
class _ConfigProxy:
"""Like _SectionRedirect this class enables backwards-compatible access to the
config settings, but raises DeprecationWarnings with instructions to use `pytensor.config`.
"""
def __init__(self, actual):
_ConfigProxy._actual = actual
def __getattr__(self, attr):
if attr == "_actual":
return _ConfigProxy._actual
warnings.warn(
"`pytensor.configparser.config` is deprecated; use `pytensor.config` instead.",
DeprecationWarning,
stacklevel=2,
)
return getattr(self._actual, attr)
def __setattr__(self, attr, value):
if attr == "_actual":
return setattr(_ConfigProxy._actual, attr, value)
warnings.warn(
"`pytensor.configparser.config` is deprecated; use `pytensor.config` instead.",
DeprecationWarning,
stacklevel=2,
)
return setattr(self._actual, attr, value)
# Create the actual instance of the config. This one should eventually move to
# `configdefaults`:
_config = _create_default_config()
# The old API often imported the default config object from `configparser`.
# These imports/accesses should be replaced with `pytensor.config`, so this wraps
# it with warnings:
config = _ConfigProxy(_config)
DEPRECATED_NAMES = [
(
"change_flags",
"`change_flags` is deprecated; use `pytensor.config.change_flags` instead.",
_config.change_flags,
),
(
"_change_flags",
"`_change_flags` is deprecated; use `pytensor.config.change_flags` instead.",
_config.change_flags,
),
(
"_config_print",
"`_config_print` is deprecated; use `pytensor.config.config_print` instead.",
_config.config_print,
),
]
def __getattr__(name):
"""Intercept module-level attribute access of deprecated symbols.
Adapted from https://stackoverflow.com/a/55139609/3006474.
"""
from warnings import warn
for old_name, msg, old_object in DEPRECATED_NAMES:
if name == old_name:
warn(msg, DeprecationWarning, stacklevel=2)
return old_object
raise AttributeError(f"module {__name__} has no attribute {name}")
...@@ -18,52 +18,6 @@ def _create_test_config(): ...@@ -18,52 +18,6 @@ def _create_test_config():
) )
def test_api_deprecation_warning():
# accessing through configdefaults.config is the new best practice
with pytest.warns(None):
root = configdefaults.config
assert isinstance(str(root), str)
# accessing through configparser.config is discouraged
root = configparser.config
with pytest.warns(DeprecationWarning, match="instead"):
root.add(
"test_deprecationwarning",
"A config var from a test case.",
configparser.StrParam("test_default"),
)
with pytest.warns(DeprecationWarning, match="instead"):
with root.change_flags(test_deprecationwarning="new_value"):
pass
def test_api_redirect():
root = _create_test_config()
# one section level
root.add(
"test__section_redirect",
"A config var from a test case.",
configparser.StrParam("test_default"),
)
assert hasattr(root, "test__section_redirect")
assert root.test__section_redirect == "test_default"
assert hasattr(root, "test")
assert isinstance(root.test, configparser._SectionRedirect)
with pytest.warns(DeprecationWarning):
assert root.test.section_redirect == "test_default"
# two section levels
root.add(
"test__subsection__redirect",
"A config var from a test case.",
configparser.StrParam("test_default2"),
)
assert hasattr(root, "test__subsection__redirect")
assert root.test__subsection__redirect == "test_default2"
with pytest.warns(DeprecationWarning):
assert root.test.subsection.redirect == "test_default2"
def test_invalid_default(): def test_invalid_default():
# Ensure an invalid default value found in the PyTensor code only causes # Ensure an invalid default value found in the PyTensor code only causes
# a crash if it is not overridden by the user. # a crash if it is not overridden by the user.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论