Skip to content

Commit 67fcf43

Browse files
fix: provide helpful error when table heading is not configured
When using tables from non-activated schemas, operations that access the heading now raise a clear DataJointError instead of confusing "NoneType has no attribute" errors. Example: schema = dj.Schema() # Not activated @Schema class MyTable(dj.Manual): ... MyTable().heading # Now raises: "Table `MyTable` is not properly # configured. Ensure the schema is activated..." Closes #1039 Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent c1b36f0 commit 67fcf43

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/datajoint/table.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ def table_name(self):
120120
def class_name(self):
121121
return self.__class__.__name__
122122

123+
@property
124+
def heading(self):
125+
"""
126+
Return the table's heading, or raise a helpful error if not configured.
127+
128+
Overrides QueryExpression.heading to provide a clear error message
129+
when the table is not properly associated with an activated schema.
130+
"""
131+
if self._heading is None:
132+
raise DataJointError(
133+
f"Table `{self.__class__.__name__}` is not properly configured. "
134+
"Ensure the schema is activated before using the table. "
135+
"Example: schema.activate('database_name') or schema = dj.Schema('database_name')"
136+
)
137+
return self._heading
138+
123139
@property
124140
def definition(self):
125141
raise NotImplementedError("Subclasses of Table must implement the `definition` property")

tests/integration/test_schema.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ class UndecoratedClass(dj.Manual):
110110
print(a.full_table_name)
111111

112112

113+
def test_non_activated_schema_heading_error():
114+
"""
115+
Tables from non-activated schemas should raise informative errors.
116+
Regression test for issue #1039.
117+
"""
118+
# Create schema without activating (no database name)
119+
schema = dj.Schema()
120+
121+
@schema
122+
class TableA(dj.Manual):
123+
definition = """
124+
id : int
125+
---
126+
value : float
127+
"""
128+
129+
# Accessing heading should raise a helpful error
130+
instance = TableA()
131+
with pytest.raises(dj.DataJointError, match="not properly configured"):
132+
_ = instance.heading
133+
134+
# Operations that use heading should also raise helpful errors
135+
with pytest.raises(dj.DataJointError, match="not properly configured"):
136+
_ = instance.primary_key # Uses heading.primary_key
137+
138+
113139
def test_reject_decorated_part(schema_any):
114140
"""
115141
Decorating a dj.Part table should raise an informative exception.

0 commit comments

Comments
 (0)