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

Fix get_loader returning None when load_jupyter_server_extension is not found (#1193)Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> #1193

Merged
merged 6 commits into from
Feb 3, 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
26 changes: 15 additions & 11 deletions jupyter_server/extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ def get_loader(obj, logger=None):
underscore prefix.
"""
try:
func = getattr(obj, "_load_jupyter_server_extension") # noqa B009
return getattr(obj, "_load_jupyter_server_extension") # noqa B009
except AttributeError:
pass

try:
func = getattr(obj, "load_jupyter_server_extension") # noqa B009
except AttributeError:
func = getattr(obj, "load_jupyter_server_extension", None)
warnings.warn(
"A `_load_jupyter_server_extension` function was not "
"found in {name!s}. Instead, a `load_jupyter_server_extension` "
"function was found and will be used for now. This function "
"name will be deprecated in future releases "
"of Jupyter Server.".format(name=obj),
DeprecationWarning,
)
except Exception:
msg = "_load_jupyter_server_extension function was not found."
raise ExtensionLoadingError(msg) from None

warnings.warn(
"A `_load_jupyter_server_extension` function was not "
"found in {name!s}. Instead, a `load_jupyter_server_extension` "
"function was found and will be used for now. This function "
"name will be deprecated in future releases "
"of Jupyter Server.".format(name=obj),
DeprecationWarning,
)
return func


Expand Down
12 changes: 12 additions & 0 deletions tests/extension/mockextensions/mockext_deprecated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""A mock extension named `mockext_py` for testing purposes.
"""
# Function that makes these extensions discoverable
# by the test functions.


def _jupyter_server_extension_paths():
return [{"module": "tests.extension.mockextensions.mockext_deprecated"}]


def load_jupyter_server_extension(serverapp):
pass
19 changes: 12 additions & 7 deletions tests/extension/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging
import warnings

import pytest

from jupyter_server.extension.utils import get_loader, get_metadata, validate_extension
from tests.extension.mockextensions import mockext_sys
from jupyter_server.extension.utils import (
ExtensionLoadingError,
get_loader,
get_metadata,
validate_extension,
)
from tests.extension.mockextensions import mockext_deprecated, mockext_sys

# Use ServerApps environment because it monkeypatches
# jupyter_core.paths and provides a config directory
Expand All @@ -24,10 +28,11 @@ def test_validate_extension():


def test_get_loader():
get_loader(mockext_sys)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
assert get_loader(object()) is None
assert get_loader(mockext_sys) == mockext_sys._load_jupyter_server_extension
with pytest.deprecated_call():
assert get_loader(mockext_deprecated) == mockext_deprecated.load_jupyter_server_extension
with pytest.raises(ExtensionLoadingError):
get_loader(object())


def test_get_metadata():
Expand Down