Skip to content

Commit 2aab3e8

Browse files
committed
Disable hidden attributes (#1091) by default
1 parent 467990e commit 2aab3e8

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

datajoint/declare.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .errors import DataJointError, _support_filepath_types, FILEPATH_FEATURE_SWITCH
1111
from .attribute_adapter import get_adapter
1212
from .condition import translate_attribute
13+
from .settings import config
1314

1415
UUID_DATA_TYPE = "binary(16)"
1516
MAX_TABLE_NAME_LENGTH = 64
@@ -311,17 +312,18 @@ def declare(full_table_name, definition, context):
311312
external_stores,
312313
) = prepare_declare(definition, context)
313314

314-
metadata_attr_sql = [
315-
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
316-
]
317-
attribute_sql.extend(
318-
attr.format(
319-
full_table_name=sha1(
320-
full_table_name.replace("`", "").encode("utf-8")
321-
).hexdigest()
315+
if config.get("enable_hidden_attributes", False):
316+
metadata_attr_sql = [
317+
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
318+
]
319+
attribute_sql.extend(
320+
attr.format(
321+
full_table_name=sha1(
322+
full_table_name.replace("`", "").encode("utf-8")
323+
).hexdigest()
324+
)
325+
for attr in metadata_attr_sql
322326
)
323-
for attr in metadata_attr_sql
324-
)
325327

326328
if not primary_key:
327329
raise DataJointError("Table must have a primary key")

datajoint/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"display.show_tuple_count": True,
4848
"database.use_tls": None,
4949
"enable_python_native_blobs": True, # python-native/dj0 encoding support
50+
"enable_hidden_attributes": False,
5051
"filepath_checksum_size_limit": None, # file size limit for when to disable checksums
5152
}
5253
)

tests/test_declare.py

+51-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
import datajoint as dj
44
import inspect
55
from datajoint.declare import declare
6+
from datajoint.settings import config
7+
8+
9+
@pytest.fixture(scope="function")
10+
def enable_hidden_attributes():
11+
orig_config_val = config.get("enable_hidden_attributes")
12+
config["enable_hidden_attributes"] = True
13+
yield
14+
if orig_config_val is not None:
15+
config["enable_hidden_attributes"] = orig_config_val
16+
17+
18+
@pytest.fixture(scope="function")
19+
def disable_hidden_attributes():
20+
orig_config_val = config.get("enable_hidden_attributes")
21+
config["enable_hidden_attributes"] = False
22+
yield
23+
if orig_config_val is not None:
24+
config["enable_hidden_attributes"] = orig_config_val
625

726

827
def test_schema_decorator(schema_any):
@@ -373,9 +392,35 @@ class Table_With_Underscores(dj.Manual):
373392
schema_any(Table_With_Underscores)
374393

375394

376-
def test_hidden_attributes(schema_any):
377-
assert (
378-
list(Experiment().heading._attributes.keys())[-1].split("_")[2] == "timestamp"
379-
)
380-
assert any(a.is_hidden for a in Experiment().heading._attributes.values())
381-
assert not any(a.is_hidden for a in Experiment().heading.attributes.values())
395+
def test_hidden_attributes_default_value():
396+
config_val = config.get("enable_hidden_attributes")
397+
assert config_val is not None and not config_val, \
398+
"Default value for enable_hidden_attributes is not False"
399+
400+
401+
def test_hidden_attributes_enabled(enable_hidden_attributes, schema_any):
402+
orig_config_val = config.get("enable_hidden_attributes")
403+
config["enable_hidden_attributes"] = True
404+
405+
msg = f"{Experiment().heading._attributes=}"
406+
assert any(a.name.endswith("_timestamp") for a in Experiment().heading._attributes.values()), msg
407+
assert any(a.name.startswith("_") for a in Experiment().heading._attributes.values()), msg
408+
assert any(a.is_hidden for a in Experiment().heading._attributes.values()), msg
409+
assert not any(a.is_hidden for a in Experiment().heading.attributes.values()), msg
410+
411+
if orig_config_val is not None:
412+
config["enable_hidden_attributes"] = orig_config_val
413+
414+
415+
def test_hidden_attributes_disabled(disable_hidden_attributes, schema_any):
416+
orig_config_val = config.get("enable_hidden_attributes")
417+
config["enable_hidden_attributes"] = False
418+
419+
msg = f"{Experiment().heading._attributes=}"
420+
assert not any(a.name.endswith("_timestamp") for a in Experiment().heading._attributes.values()), msg
421+
assert not any(a.name.startswith("_") for a in Experiment().heading._attributes.values()), msg
422+
assert not any(a.is_hidden for a in Experiment().heading._attributes.values()), msg
423+
assert not any(a.is_hidden for a in Experiment().heading.attributes.values()), msg
424+
425+
if orig_config_val is not None:
426+
config["enable_hidden_attributes"] = orig_config_val

0 commit comments

Comments
 (0)