From aaaba6785cf2405ace03aaf0e7c09f1626789e6e Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Tue, 21 May 2024 20:19:31 +0200 Subject: [PATCH] ORM: Fix deprecation warning being shown for new code types The `InstalledCode` and `PortableCode` have been introduced already some time ago as the refactored version of the legacy `Code` class. The latter has been deprecated for that reason, but for backwards compatibility reasons, the new implementations still need to keep the legacy class as a base class. Unfortunately, this meant that the deprecation warning was also shown when users instantiated an instance of the new classes, which would be confusing. The deprecation warning had already been moved from module level to the constructor of the `Code` class to prevent it from showing merely upon import of the `aiida.orm` package. As a final work around, the new classes define the `_EMIT_CODE_DEPRECATION_WARNING` class attribute which is checked in the `Code` constructor such that when set, the deprecation warning is skipped. --- src/aiida/orm/nodes/data/code/installed.py | 1 + src/aiida/orm/nodes/data/code/legacy.py | 18 +++++++++++------- src/aiida/orm/nodes/data/code/portable.py | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/aiida/orm/nodes/data/code/installed.py b/src/aiida/orm/nodes/data/code/installed.py index 6454c8b0ce..5f9fb39749 100644 --- a/src/aiida/orm/nodes/data/code/installed.py +++ b/src/aiida/orm/nodes/data/code/installed.py @@ -37,6 +37,7 @@ class InstalledCode(Code): """Data plugin representing an executable code on a remote computer.""" + _EMIT_CODE_DEPRECATION_WARNING: bool = False _KEY_ATTRIBUTE_FILEPATH_EXECUTABLE: str = 'filepath_executable' class Model(AbstractCode.Model): diff --git a/src/aiida/orm/nodes/data/code/legacy.py b/src/aiida/orm/nodes/data/code/legacy.py index 30f04b8264..5f62b1cf97 100644 --- a/src/aiida/orm/nodes/data/code/legacy.py +++ b/src/aiida/orm/nodes/data/code/legacy.py @@ -75,13 +75,17 @@ class Code(AbstractCode): def __init__(self, remote_computer_exec=None, local_executable=None, input_plugin_name=None, files=None, **kwargs): super().__init__(**kwargs) - warn_deprecation( - 'The `Code` class is deprecated. To create an instance, use the ' - '`aiida.orm.nodes.data.code.installed.InstalledCode` or `aiida.orm.nodes.data.code.portable.PortableCode` ' - 'for a "remote" or "local" code, respectively. If you are using this class to compare type, e.g. in ' - '`isinstance`, use `aiida.orm.nodes.data.code.abstract.AbstractCode`.', - version=3, - ) + # The ``_EMIT_CODE_DEPRECATION_WARNING`` attribute is set in subclasses to avoid the deprecation message below + # is also shown when they are instantiated, since they are not deprecated. + if getattr(self, '_EMIT_CODE_DEPRECATION_WARNING', True): + warn_deprecation( + 'The `Code` class is deprecated. To create an instance, use the ' + '`aiida.orm.nodes.data.code.installed.InstalledCode` or ' + '`aiida.orm.nodes.data.code.portable.PortableCode` for a "remote" or "local" code, respectively. If ' + 'you are using this class to compare type, e.g. in ' + '`isinstance`, use `aiida.orm.nodes.data.code.abstract.AbstractCode`.', + version=3, + ) if remote_computer_exec and local_executable: raise ValueError('cannot set `remote_computer_exec` and `local_executable` at the same time') diff --git a/src/aiida/orm/nodes/data/code/portable.py b/src/aiida/orm/nodes/data/code/portable.py index 48b927c9ca..a09ccb90a4 100644 --- a/src/aiida/orm/nodes/data/code/portable.py +++ b/src/aiida/orm/nodes/data/code/portable.py @@ -39,6 +39,7 @@ class PortableCode(Code): """Data plugin representing an executable code stored in AiiDA's storage.""" + _EMIT_CODE_DEPRECATION_WARNING: bool = False _KEY_ATTRIBUTE_FILEPATH_EXECUTABLE: str = 'filepath_executable' class Model(AbstractCode.Model):