Skip to content

Commit

Permalink
Backport PR #1193: Fix get_loader returning None when load_jupyter_se…
Browse files Browse the repository at this point in the history
…rver_extension is not found (#1193) (#1199)Co-authored-by: Steven Silvester <steven.silvester@ieee.org> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Backport #1193 to 1.x

* bump isort dep

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix check links job name

---------

Co-authored-by: Steven Silvester <steven.silvester@ieee.org>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 3, 2023
1 parent 05758da commit 8ab076e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Expand Up @@ -111,7 +111,7 @@ jobs:
pytest -vv -W default || pytest -vv -W default --lf
check_links:
name: Make SDist
name: Check Links
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Expand Up @@ -22,7 +22,7 @@ repos:
args: ["--line-length", "100"]

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
files: \.py$
Expand Down
29 changes: 17 additions & 12 deletions jupyter_server/extension/utils.py
Expand Up @@ -26,19 +26,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:
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:
raise ExtensionLoadingError("_load_jupyter_server_extension function was not found.")
pass

try:
func = getattr(obj, "load_jupyter_server_extension") # noqa B009
except AttributeError:
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
@@ -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
15 changes: 14 additions & 1 deletion tests/extension/test_utils.py
@@ -1,6 +1,11 @@
import pytest

from jupyter_server.extension.utils import validate_extension
from jupyter_server.extension.utils import (
ExtensionLoadingError,
get_loader,
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 @@ -17,3 +22,11 @@ def test_validate_extension():
assert validate_extension("tests.extension.mockextensions.mockext_user")
# enabled at Python
assert validate_extension("tests.extension.mockextensions.mockext_py")


def test_get_loader():
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())

0 comments on commit 8ab076e

Please sign in to comment.