Skip to content

Commit e24605d

Browse files
kushalbakshiclaude
andcommitted
refactor: move database_prefix deprecation warning to Schema.activate()
Move the DeprecationWarning from Pydantic model validator (fires at config load time with unhelpful stacklevel) to Schema.activate() where database_prefix is consumed. The warning now points to the user's dj.Schema(...) call — exactly where they need to make a change. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8e3d39b commit e24605d

File tree

3 files changed

+8
-29
lines changed

3 files changed

+8
-29
lines changed

src/datajoint/schemas.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ def activate(
172172
self.connection = connection
173173
if self.connection is None:
174174
self.connection = _get_singleton_connection()
175+
if self.connection._config.get("database.database_prefix"):
176+
warnings.warn(
177+
"database_prefix is deprecated and will be removed in DataJoint 2.3. "
178+
"Use database.name to select a PostgreSQL database instead.",
179+
DeprecationWarning,
180+
stacklevel=2,
181+
)
175182
self.database = schema_name
176183
if create_schema is not None:
177184
self.create_schema = create_schema

src/datajoint/settings.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,6 @@ def set_default_port_from_backend(self) -> "DatabaseSettings":
223223
self.port = 5432 if self.backend == "postgresql" else 3306
224224
return self
225225

226-
@model_validator(mode="after")
227-
def warn_database_prefix_deprecated(self) -> "DatabaseSettings":
228-
"""Emit deprecation warning when database_prefix is set."""
229-
if self.database_prefix:
230-
import warnings
231-
232-
warnings.warn(
233-
"database_prefix is deprecated and will be removed in DataJoint 2.3. "
234-
"Use database.name to select a PostgreSQL database instead.",
235-
DeprecationWarning,
236-
stacklevel=2,
237-
)
238-
return self
239226

240227

241228
class ConnectionSettings(BaseSettings):

tests/unit/test_settings.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -800,23 +800,8 @@ def test_database_name_override_context_manager(self):
800800
assert dj.config.database.name == "override_db"
801801
assert dj.config.database.name == original
802802

803-
def test_database_prefix_deprecation_warning(self, monkeypatch):
804-
"""Non-empty database_prefix emits DeprecationWarning."""
805-
import warnings
806-
807-
from datajoint.settings import DatabaseSettings
808-
809-
monkeypatch.setenv("DJ_DATABASE_PREFIX", "test_")
810-
with warnings.catch_warnings(record=True) as w:
811-
warnings.simplefilter("always")
812-
DatabaseSettings()
813-
deprecation_warnings = [
814-
x for x in w if issubclass(x.category, DeprecationWarning) and "database_prefix" in str(x.message)
815-
]
816-
assert len(deprecation_warnings) >= 1
817-
818803
def test_database_prefix_empty_no_warning(self):
819-
"""Empty database_prefix does not emit DeprecationWarning."""
804+
"""Empty database_prefix does not emit DeprecationWarning at config load."""
820805
import warnings
821806

822807
from datajoint.settings import DatabaseSettings

0 commit comments

Comments
 (0)