Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/experimaestro/core/objects/config.py
Comment thread
teo-lohrer-su marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions src/experimaestro/tests/test_objects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from pathlib import Path

import pytest
Expand Down Expand Up @@ -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