Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print warning to stdout on load_profile for development versions #5311

Merged
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
29 changes: 29 additions & 0 deletions aiida/manage/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@
BACKEND_UUID = None # This will be set to the UUID of the profile as soon as its corresponding backend is loaded


def check_version():
"""Check the currently installed version of ``aiida-core`` and warn if it is a post release development version.

The ``aiida-core`` package maintains the protocol that the ``develop`` branch will use a post release version
number. This means it will always append `.post0` to the version of the latest release. This should mean that if
this protocol is maintained properly, this method will print a warning if the currently installed version is a
post release development branch and not an actual release.
"""
from packaging.version import parse

from aiida import __version__
from aiida.cmdline.utils import echo

version = parse(__version__)

# Showing of the warning can be turned off by setting the following option to false.
show_warning = get_config_option('warnings.development_version')

if version.is_postrelease and show_warning:
echo.echo_warning(f'You are currently using a post release development version of AiiDA: {version}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use logger warnings, rather than using click outside of the CLI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does use the logging system though. It is true that the module is in the aiida.cmdline module, but after the recent refactor, they all go through the logger.

echo.echo_warning('Be aware that this is not recommended for production and is not officially supported.')
echo.echo_warning('Databases used with this version may not be compatible with future releases of AiiDA')
echo.echo_warning('as you might not be able to automatically migrate your data.\n')


def load_profile(profile=None):
"""Load a profile.

Expand Down Expand Up @@ -92,6 +117,10 @@ def load_profile(profile=None):
# instead be done lazily in `Manager._load_backend`.
configure_logging()

# Check whether a development version is being run. Note that needs to be called after ``configure_logging`` because
# this function relies on the logging being properly configured for the warning to show.
check_version()

return PROFILE


Expand Down
6 changes: 6 additions & 0 deletions aiida/manage/configuration/schema/config-v5.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
"default": true,
"description": "Whether to print AiiDA deprecation warnings"
},
"warnings.development_version": {
"type": "boolean",
"default": true,
"description": "Whether to print a warning when a profile is loaded while a development version is installed",
"global_only": true
},
"transport.task_retry_initial_interval": {
"type": "integer",
"default": 20,
Expand Down
50 changes: 50 additions & 0 deletions tests/manage/configuration/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
"""Tests for the :mod:`aiida.manage.configuration` module."""
import pytest

import aiida
from aiida.manage.configuration import check_version


def test_check_version_release(monkeypatch, capsys, isolated_config):
"""Test that ``check_version`` prints nothing for a release version.

If a warning is emitted, it should be printed to stdout. So even though it will go through the logging system, the
logging configuration of AiiDA will interfere with that of pytest and the ultimately the output will simply be
written to stdout, so we use the ``capsys`` fixture and not the ``caplog`` one.
"""
version = '1.0.0'
monkeypatch.setattr(aiida, '__version__', version)

# Explicitly setting the default in case the test profile has it changed.
isolated_config.set_option('warnings.development_version', True)

check_version()
captured = capsys.readouterr()
assert not captured.err
assert not captured.out


@pytest.mark.parametrize('suppress_warning', (True, False))
def test_check_version_development(monkeypatch, capsys, isolated_config, suppress_warning):
"""Test that ``check_version`` prints a warning for a post release development version.

The warning can be suppressed by setting the option ``warnings.development_version`` to ``False``.

If a warning is emitted, it should be printed to stdout. So even though it will go through the logging system, the
logging configuration of AiiDA will interfere with that of pytest and the ultimately the output will simply be
written to stdout, so we use the ``capsys`` fixture and not the ``caplog`` one.
"""
version = '1.0.0.post0'
monkeypatch.setattr(aiida, '__version__', version)

isolated_config.set_option('warnings.development_version', not suppress_warning)

check_version()
captured = capsys.readouterr()
assert not captured.err

if suppress_warning:
assert not captured.out
else:
assert f'You are currently using a post release development version of AiiDA: {version}' in captured.out
2 changes: 1 addition & 1 deletion tests/manage/configuration/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestConfigurationOptions(AiidaTestCase):
def test_get_option_names(self):
"""Test `get_option_names` function."""
self.assertIsInstance(get_option_names(), list)
self.assertEqual(len(get_option_names()), 26)
self.assertEqual(len(get_option_names()), 27)

def test_get_option(self):
"""Test `get_option` function."""
Expand Down