From 48ae9d437936dc85ad38d190b6cbc55ec72ad047 Mon Sep 17 00:00:00 2001 From: ahalev Date: Tue, 19 Dec 2023 17:44:40 -0800 Subject: [PATCH 1/4] add env signature error --- src/pymgrid/errors/env_signature.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/pymgrid/errors/env_signature.py diff --git a/src/pymgrid/errors/env_signature.py b/src/pymgrid/errors/env_signature.py new file mode 100644 index 00000000..593b4b10 --- /dev/null +++ b/src/pymgrid/errors/env_signature.py @@ -0,0 +1,13 @@ +from pymgrid import Microgrid, NonModularMicrogrid +from pymgrid.errors import DeprecatedError + + +def environment_signature_error(cls, modules): + if isinstance(modules, (Microgrid, NonModularMicrogrid)): + msg = f'Initializing a {cls} with a microgrid is deprecated as of version 1.5.0. ' \ + f'Use {cls}.from_microgrid() as a drop in replacement.' + else: + msg = f'Initializing a {cls} with a scenario integer is deprecated as of version 1.5.0. ' \ + f'Use {cls}.from_scenario() as a drop in replacement.' + + raise DeprecatedError(msg) From 08dd55ed6d579193eb50e174e5c770adc55f3309 Mon Sep 17 00:00:00 2001 From: ahalev Date: Tue, 19 Dec 2023 17:44:58 -0800 Subject: [PATCH 2/4] remove new and raise environment_signature_error --- src/pymgrid/envs/base/base.py | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/pymgrid/envs/base/base.py b/src/pymgrid/envs/base/base.py index 8db6a26c..5c775f53 100644 --- a/src/pymgrid/envs/base/base.py +++ b/src/pymgrid/envs/base/base.py @@ -7,7 +7,7 @@ from abc import abstractmethod from pymgrid import NonModularMicrogrid, Microgrid -from pymgrid.envs.base.skip_init import skip_init +from pymgrid.errors.env_signature import environment_signature_error class BaseMicrogridEnv(Microgrid, Env): @@ -70,26 +70,6 @@ class BaseMicrogridEnv(Microgrid, Env): observation_space = None 'Space object corresponding to valid observations.' - def __new__(cls, modules, *args, **kwargs): - if isinstance(modules, (NonModularMicrogrid, Microgrid)): - import warnings - warnings.warn('Initializing an environment with a microgrid will be deprecated in a future version.' - 'Use from_microgrid() instead.', category=FutureWarning) - - instance = cls.from_microgrid(modules, **kwargs) - - elif isinstance(modules, int): - import warnings - warnings.warn('Initializing an environment with a scenario integer will be deprecated in a future version.' - 'Use from_scenario() instead.', category=FutureWarning) - instance = cls.from_scenario(modules, **kwargs) - - else: - return super().__new__(cls) - - cls.__init__ = skip_init(cls, cls.__init__) - return instance - def __init__(self, modules, add_unbalanced_module=True, @@ -103,6 +83,9 @@ def __init__(self, reset_callback=None ): + if isinstance(modules, (NonModularMicrogrid, Microgrid, int)): + environment_signature_error(self.__class__.__name__, modules) + super().__init__(modules, add_unbalanced_module=add_unbalanced_module, loss_load_cost=loss_load_cost, From 86205c6462ff06087570b53c86331ef96448c3a8 Mon Sep 17 00:00:00 2001 From: ahalev Date: Tue, 19 Dec 2023 17:48:59 -0800 Subject: [PATCH 3/4] update references --- src/pymgrid/envs/base/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pymgrid/envs/base/base.py b/src/pymgrid/envs/base/base.py index 5c775f53..a6c6cf4b 100644 --- a/src/pymgrid/envs/base/base.py +++ b/src/pymgrid/envs/base/base.py @@ -423,4 +423,4 @@ def from_scenario(cls, microgrid_number=0, **kwargs): @classmethod def load(cls, stream): - return cls(super().load(stream)) + return cls.from_microgrid(super().load(stream)) From 50167c2a814d4f108a805f50f38e6c6efb8b6bda Mon Sep 17 00:00:00 2001 From: ahalev Date: Tue, 19 Dec 2023 17:49:05 -0800 Subject: [PATCH 4/4] update test --- tests/envs/test_discrete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/envs/test_discrete.py b/tests/envs/test_discrete.py index a1612636..e4c773a4 100644 --- a/tests/envs/test_discrete.py +++ b/tests/envs/test_discrete.py @@ -11,7 +11,7 @@ class TestDiscreteEnv(TestCase): def test_init_from_microgrid(self): microgrid = get_modular_microgrid() - env = DiscreteMicrogridEnv(microgrid) + env = DiscreteMicrogridEnv.from_microgrid(microgrid) self.assertEqual(env.modules, microgrid.modules) self.assertIsNot(env.modules.to_tuples(), microgrid.modules.to_tuples())