Skip to content

Commit

Permalink
🚸 Clearer feedback when somebody tries to switch the default instance (…
Browse files Browse the repository at this point in the history
…#882)

* 🚸 Clearer error message when somebody tries to switch instances

* 🚸 Improve

* πŸ’š Fix

* βœ… Re-organize tests

* πŸ’š Fix deps

* πŸ’š Fix

* πŸ’š Fix

* πŸ’š Fix

* πŸ’š Fix

* Improve words
  • Loading branch information
falexwolf authored Oct 10, 2024
1 parent f65877b commit c4fafda
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 35 deletions.
23 changes: 0 additions & 23 deletions docs/hub-cloud/02-connect-local-instance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,6 @@
")"
]
},
{
"cell_type": "markdown",
"id": "0986a5f3",
"metadata": {},
"source": [
"You cannot load another instance in the same Python session:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc3568ae",
"metadata": {},
"outputs": [],
"source": [
"import pytest\n",
"\n",
"with pytest.raises(RuntimeError):\n",
" ln_setup.connect(\"testuser2/mydata\")\n",
"\n",
"assert ln_setup.settings.instance.slug == \"testuser1/mydata\""
]
},
{
"cell_type": "markdown",
"id": "e3701120",
Expand Down
10 changes: 7 additions & 3 deletions docs/hub-cloud/08-test-multi-session.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@
"metadata": {},
"outputs": [],
"source": [
"with pytest.raises(RuntimeError):\n",
"from lamindb_setup._init_instance import CannotSwitchDefaultInstance\n",
"\n",
"with pytest.raises(CannotSwitchDefaultInstance):\n",
" ln_setup.init(storage=\"./testsetup2\")\n",
"with pytest.raises(RuntimeError):\n",
"with pytest.raises(CannotSwitchDefaultInstance):\n",
" ln_setup.connect(\"testsetup\")\n",
"with pytest.raises(RuntimeError):\n",
" ln_setup.migrate.create()\n",
"with pytest.raises(RuntimeError):\n",
" ln_setup.migrate.deploy()"
" ln_setup.migrate.deploy()\n",
"\n",
"assert ln_setup.settings.instance.slug == \"testuser1/testsetup\""
]
},
{
Expand Down
10 changes: 8 additions & 2 deletions lamindb_setup/_connect_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

from ._check_setup import _check_instance_setup
from ._close import close as close_instance
from ._init_instance import MESSAGE_NO_MULTIPLE_INSTANCE, load_from_isettings
from ._init_instance import (
MESSAGE_CANNOT_SWITCH_DEFAULT_INSTANCE,
CannotSwitchDefaultInstance,
load_from_isettings,
)
from ._silence_loggers import silence_loggers
from .core._hub_core import connect_instance_hub
from .core._hub_utils import (
Expand Down Expand Up @@ -224,7 +228,9 @@ def connect(slug: str, **kwargs) -> str | tuple | None:
logger.info(f"connected lamindb: {settings.instance.slug}")
return None
else:
raise RuntimeError(MESSAGE_NO_MULTIPLE_INSTANCE)
raise CannotSwitchDefaultInstance(
MESSAGE_CANNOT_SWITCH_DEFAULT_INSTANCE
)
elif (
_write_settings
and settings._instance_exists
Expand Down
18 changes: 13 additions & 5 deletions lamindb_setup/_init_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,18 @@ def validate_init_args(
return name_str, instance_id, instance_state, instance_slug


MESSAGE_NO_MULTIPLE_INSTANCE = """
Currently don't support subsequent connection to different databases in the same
Python session.\n
Try running on the CLI: lamin settings set auto-connect false
class CannotSwitchDefaultInstance(SystemExit):
pass


MESSAGE_CANNOT_SWITCH_DEFAULT_INSTANCE = """
You cannot write to different instances in the same Python session.
Do you want to read from another instance via `Record.using()`? For example:
ln.Artifact.using("laminlabs/cellxgene").filter()
Or do you want to switch off auto-connect via `lamin settings set auto-connect false`?
"""


Expand Down Expand Up @@ -250,7 +258,7 @@ def init(
from ._check_setup import _check_instance_setup

if _check_instance_setup() and not _test:
raise RuntimeError(MESSAGE_NO_MULTIPLE_INSTANCE)
raise CannotSwitchDefaultInstance(MESSAGE_CANNOT_SWITCH_DEFAULT_INSTANCE)
elif _write_settings:
close_instance(mute=True)
from .core._hub_core import init_instance as init_instance_hub
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def lint(session: nox.Session) -> None:
["hub-local", "hub-prod", "hub-cloud", "storage", "docs"],
)
def install(session: nox.Session, group: str) -> None:
no_deps_packages = "git+https://github.com/laminlabs/lnschema-core git+https://github.com/laminlabs/wetlab git+https://github.com/laminlabs/lamin-cli"
no_deps_packages = "git+https://github.com/laminlabs/lnschema-core git+https://github.com/laminlabs/wetlab git+https://github.com/laminlabs/lamin-cli git+https://github.com/laminlabs/findrefs"
schema_deps = f"""uv pip install --system git+https://github.com/laminlabs/bionty git+https://github.com/laminlabs/lamindb
uv pip install --system --no-deps {no_deps_packages}
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/hub-cloud/test_edge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_edge_request():
assert instance["owner"] == "laminlabs"
assert instance["name"] == "lamindata"
assert instance["api_url"] == "https://us-east-1.api.lamin.ai"
assert instance["schema_str"] == "bionty,wetlab"
assert instance["schema_str"].startswith("bionty,wetlab")
assert "schema_id" in instance
# check that schema_id is well-formed UUID
# if not, throws ValueError
Expand Down

0 comments on commit c4fafda

Please sign in to comment.