diff --git a/src/experimaestro/core/objects/config.py b/src/experimaestro/core/objects/config.py index c658c968..1a405685 100644 --- a/src/experimaestro/core/objects/config.py +++ b/src/experimaestro/core/objects/config.py @@ -1489,7 +1489,6 @@ def __new__(cls: Type[T], *args, **kwargs) -> T: :deprecated: Use Config.C or Config.XPMConfig to construct a new configuration, and Config.V (or Config.XPMValue) for a new value """ - # If this is an XPMValue, just return a new instance from experimaestro.core.types import XPMValue @@ -1501,12 +1500,21 @@ def __new__(cls: Type[T], *args, **kwargs) -> T: if issubclass(cls, ConfigMixin): return object.__new__(cls) + # Log a deprecation warning for this way of creating a configuration + caller = inspect.getframeinfo(inspect.stack()[1][0]) + logger.warning( + "Creating a configuration using Config.__new__ is deprecated, and will be removed in a future version. " + "Use Config.C or Config.XPMConfig to create a new configuration. " + "Issue created at %s:%s", + str(Path(caller.filename).absolute()), + caller.lineno, + ) + # otherwise, we use the configuration type o: ConfigMixin = object.__new__(cls.__getxpmtype__().configtype) try: o.__init__(*args, **kwargs) except Exception: - caller = inspect.getframeinfo(inspect.stack()[1][0]) logger.error( "Init error in %s:%s" % (str(Path(caller.filename).absolute()), caller.lineno) diff --git a/src/experimaestro/tests/test_objects.py b/src/experimaestro/tests/test_objects.py index af008765..62ef0152 100644 --- a/src/experimaestro/tests/test_objects.py +++ b/src/experimaestro/tests/test_objects.py @@ -1,3 +1,4 @@ +import logging from pathlib import Path import pytest @@ -91,3 +92,18 @@ def test_copyconfig(xp): assert copy_b.x == b.x assert "path" not in copy_b.__xpm__.values + + +def test_direct_config_warns(caplog): + """Test that using a building Config directly raises a warning""" + message = "Config.__new__ is deprecated" + + with caplog.at_level(logging.WARNING): + A(x=3) + assert message in caplog.text + + caplog.clear() + + with caplog.at_level(logging.WARNING): + A.C(x=3) + assert message not in caplog.text