Skip to content
New issue

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

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

Already on GitHub? # to your account

Add ops.lib deprecation notice #901

Merged
merged 3 commits into from
Feb 7, 2023
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
18 changes: 17 additions & 1 deletion ops/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Infrastructure for the opslib functionality."""
"""Infrastructure for the opslib functionality.

DEPRECATED: The ops.lib functionality is deprecated, and is superseded by
charm libraries (https://juju.is/docs/sdk/library) and regular Python imports.
We now prefer to do version selection at build (charmcraft pack) time.
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
"""

import logging
import os
import re
import sys
import warnings
from ast import literal_eval
from importlib.machinery import ModuleSpec
from importlib.util import module_from_spec
Expand All @@ -41,6 +47,9 @@
def use(name: str, api: int, author: str) -> ModuleType:
"""Use a library from the ops libraries.

DEPRECATED: This function is deprecated. Prefer charm libraries instead
(https://juju.is/docs/sdk/library).

Args:
name: the name of the library requested.
api: the API version of the library.
Expand All @@ -52,6 +61,8 @@ def use(name: str, api: int, author: str) -> ModuleType:
TypeError: if the name, api, or author are the wrong type.
ValueError: if the name, api, or author are invalid.
"""
warnings.warn("ops.lib is deprecated, prefer charm libraries instead",
category=DeprecationWarning)
if not isinstance(name, str):
raise TypeError(f"invalid library name: {name!r} (must be a str)")
if not isinstance(author, str):
Expand Down Expand Up @@ -88,7 +99,12 @@ def autoimport():
You only need to call this if you've installed a package or
otherwise changed sys.path in the current run, and need to see the
changes. Otherwise libraries are found on first call of `use`.

DEPRECATED: This function is deprecated. Prefer charm libraries instead
(https://juju.is/docs/sdk/library).
"""
warnings.warn("ops.lib is deprecated, prefer charm libraries instead",
category=DeprecationWarning)
global _libraries
_libraries = {}
for spec in _find_all_specs(sys.path):
Expand Down
15 changes: 15 additions & 0 deletions test/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
from unittest.mock import patch

import logassert
import pytest

import ops.lib

# Ignore deprecation warnings for this module.
pytestmark = pytest.mark.filterwarnings('ignore::DeprecationWarning')


def _mklib(topdir: str, pkgname: str, libname: str) -> Path:
"""Make a for-testing library.
Expand Down Expand Up @@ -609,3 +613,14 @@ def test_others_found(self):

with self.assertRaises(ImportError):
ops.lib.use('baz', 2, 'bob@example.com')


class TestDeprecationWarning(TestCase):
def test_autoimport_deprecated(self):
with self.assertWarns(DeprecationWarning):
ops.lib.autoimport()

def test_use_deprecated(self):
with self.assertWarns(DeprecationWarning):
with self.assertRaises(ImportError):
ops.lib.use('foo', 1, 'bob@example.com')