Skip to content

Commit

Permalink
ORM: Move deprecation warnings from module level
Browse files Browse the repository at this point in the history
The `aiida.orm.nodes.data.code.legacy` and `aiida.orm.nodes.data.upf`
modules contained deprecation warnings on the module level. Since these
were exposed directly on the `aiida.orm` level, these deprecations would
always show. This would even be the case if `warnings.showdeprecations`
would be disabled, because that acts on the profile by default, which is
not yet loaded at import time. Disabling the warnings globally would
work but this would not be intuitive for users.

The original reason for putting the warning on the module level is that
third-party code could rely on the module merely by importing the
classes without actually constructing it. This would make it possible
for some plugins to completely miss the deprecation warning.

At this point we decide to accept this risk in favor of reducing the
noise for the majority of users as these warnings are printed as soon as
anything from AiiDA is imported.
  • Loading branch information
sphuber committed Dec 12, 2023
1 parent 9807ced commit c4afdb9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
15 changes: 8 additions & 7 deletions aiida/orm/nodes/data/code/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@

__all__ = ('Code',)

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
)


class Code(AbstractCode):
"""
Expand All @@ -50,6 +43,14 @@ 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
)

if remote_computer_exec and local_executable:
raise ValueError('cannot set `remote_computer_exec` and `local_executable` at the same time')

Expand Down
25 changes: 20 additions & 5 deletions aiida/orm/nodes/data/upf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

__all__ = ('UpfData',)

warn_deprecation(
'This module is deprecated. See '
'https://aiida-pseudo.readthedocs.io/en/latest/howto.html#migrate-from-legacy-upfdata-from-aiida-core.',
version=3
)

def emit_deprecation():
warn_deprecation(
'The `aiida.orm.nodes.data.upf` module is deprecated. For details how to replace it, please see '
'https://aiida-pseudo.readthedocs.io/en/latest/howto.html#migrate-from-legacy-upfdata-from-aiida-core.',
version=3
)


REGEX_UPF_VERSION = re.compile(r"""
\s*<UPF\s+version\s*="
Expand Down Expand Up @@ -57,6 +60,8 @@ def get_pseudos_from_structure(structure, family_name):
"""
from aiida.common.exceptions import MultipleObjectsError, NotExistent

emit_deprecation()

pseudo_list = {}
family_pseudos = {}
family = UpfData.get_upf_group(family_name)
Expand Down Expand Up @@ -96,6 +101,8 @@ def upload_upf_family(folder, group_label, group_description, stop_if_existing=T
from aiida.common.exceptions import UniquenessError
from aiida.common.files import md5_file

emit_deprecation()

if not os.path.isdir(folder):
raise ValueError('folder must be a directory')

Expand Down Expand Up @@ -204,6 +211,8 @@ def parse_upf(fname, check_filename=True, encoding='utf-8'):
from aiida.common.exceptions import ParsingError
from aiida.orm.nodes.data.structure import _valid_symbols

emit_deprecation()

parsed_data = {}

try:
Expand Down Expand Up @@ -278,6 +287,8 @@ def get_or_create(cls, filepath, use_first=False, store_upf=True, backend=None):

from aiida.common.files import md5_file

emit_deprecation()

if not os.path.isabs(filepath):
raise ValueError('filepath must be an absolute path')

Expand All @@ -301,6 +312,10 @@ def get_or_create(cls, filepath, use_first=False, store_upf=True, backend=None):

return (pseudos[0], False)

def __init__(self, *args, **kwargs):
emit_deprecation()
super().__init__(*args, **kwargs)

def store(self, *args, **kwargs): # pylint: disable=signature-differs
"""Store the node, reparsing the file so that the md5 and the element are correctly reset."""
from aiida.common.exceptions import ParsingError
Expand Down

0 comments on commit c4afdb9

Please sign in to comment.