Skip to content

Commit 59868f9

Browse files
committed
Improve code organization: add future annotations, type hints, and docstrings
This commit improves the overall code organization of the datajoint package: 1. Added `from __future__ import annotations` to all Python modules for consistent PEP 563 deferred annotation evaluation 2. Added comprehensive module-level docstrings to all files explaining the purpose and functionality of each module 3. Added modern Python 3.10+ type hints to core API methods: - expression.py: QueryExpression properties and query operators - table.py: Table CRUD operations (insert, delete, etc.) - fetch.py: Fetch and Fetch1 classes 4. Standardized import organization across all modules with: - Future annotations import at the top - Standard library imports - Third-party imports - Local imports - TYPE_CHECKING block for circular dependency handling 5. Replaced legacy Union[X, Y] syntax with modern X | Y union syntax 6. Improved docstrings for critical public methods with parameter and return type documentation
1 parent 63ebc38 commit 59868f9

27 files changed

+617
-131
lines changed

datajoint/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
- http://dx.doi.org/10.1101/031658
1616
"""
1717

18+
from __future__ import annotations
19+
1820
__author__ = "DataJoint Contributors"
1921
__date__ = "November 7, 2020"
2022
__all__ = [

datajoint/admin.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
"""
2+
Administrative utilities for DataJoint database management.
3+
4+
This module provides functions for administrative tasks such as setting passwords
5+
and managing database connections. These operations typically require appropriate
6+
database privileges.
7+
"""
8+
9+
from __future__ import annotations
10+
111
import logging
212
from getpass import getpass
13+
from typing import TYPE_CHECKING
314

415
import pymysql
516
from packaging import version
@@ -8,6 +19,9 @@
819
from .settings import config
920
from .utils import user_choice
1021

22+
if TYPE_CHECKING:
23+
from .connection import Connection
24+
1125
logger = logging.getLogger(__name__.split(".")[0])
1226

1327

datajoint/attribute_adapter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
"""
2+
Attribute adapter module for user-defined types.
3+
4+
This module provides the AttributeAdapter base class for creating custom attribute
5+
types in DataJoint. Adapters allow transparent conversion between Python objects
6+
and database-storable values.
7+
8+
Note: This module is deprecated. Use datajoint.AttributeType instead for new
9+
custom types.
10+
"""
11+
12+
from __future__ import annotations
13+
114
import re
15+
from typing import Any
216

317
from .errors import DataJointError, _support_adapted_types
418

datajoint/autopopulate.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
"""This module defines class dj.AutoPopulate"""
1+
"""
2+
AutoPopulate mixin for automated table population.
3+
4+
This module defines the AutoPopulate class, a mixin that adds the populate() method
5+
to Table classes. Auto-populated tables must define a key_source property and a make()
6+
callback method. The populate system handles job management, error tracking, and
7+
parallel execution.
8+
"""
9+
10+
from __future__ import annotations
211

312
import contextlib
413
import datetime
@@ -8,6 +17,7 @@
817
import random
918
import signal
1019
import traceback
20+
from typing import TYPE_CHECKING, Any
1121

1222
import deepdiff
1323
from tqdm import tqdm
@@ -16,7 +26,9 @@
1626
from .expression import AndList, QueryExpression
1727
from .hash import key_hash
1828

19-
# noinspection PyExceptionInherit,PyCallingNonCallable
29+
if TYPE_CHECKING:
30+
from .jobs import JobTable
31+
from .table import Table
2032

2133
logger = logging.getLogger(__name__.split(".")[0])
2234

datajoint/blob.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
"""
2-
(De)serialization methods for basic datatypes and numpy.ndarrays with provisions for mutual
3-
compatibility with Matlab-based serialization implemented by mYm.
2+
Blob serialization module for DataJoint.
3+
4+
This module provides (de)serialization methods for basic datatypes and numpy.ndarrays
5+
with provisions for mutual compatibility with Matlab-based serialization implemented
6+
by mYm. It handles packing data for storage in blob columns and unpacking on retrieval.
47
"""
58

9+
from __future__ import annotations
10+
611
import collections
712
import datetime
813
import uuid
914
import zlib
1015
from decimal import Decimal
1116
from itertools import repeat
17+
from typing import Any
1218

1319
import numpy as np
1420

datajoint/cli.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
"""
2+
Command-line interface for DataJoint.
3+
4+
This module provides the console interface for DataJoint Python, allowing users
5+
to start an interactive REPL session with DataJoint pre-configured and optional
6+
schema modules loaded.
7+
"""
8+
9+
from __future__ import annotations
10+
111
import argparse
212
from code import interact
313
from collections import ChainMap
414

515
import datajoint as dj
616

717

8-
def cli(args: list = None):
18+
def cli(args: list[str] | None = None) -> None:
919
"""
1020
Console interface for DataJoint Python
1121

datajoint/condition.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
"""methods for generating SQL WHERE clauses from datajoint restriction conditions"""
1+
"""
2+
SQL condition generation module for DataJoint.
3+
4+
This module provides methods for generating SQL WHERE clauses from DataJoint
5+
restriction conditions. It handles translation of various Python objects
6+
(dicts, lists, query expressions) into valid SQL conditions.
7+
"""
8+
9+
from __future__ import annotations
210

311
import collections
412
import datetime
@@ -8,13 +16,16 @@
816
import re
917
import uuid
1018
from dataclasses import dataclass
11-
from typing import List, Union
19+
from typing import TYPE_CHECKING, Any
1220

1321
import numpy
1422
import pandas
1523

1624
from .errors import DataJointError
1725

26+
if TYPE_CHECKING:
27+
from .expression import QueryExpression
28+
1829
JSON_PATTERN = re.compile(
1930
r"^(?P<attr>\w+)(\.(?P<path>[\w.*\[\]]+))?(:(?P<type>[\w(,\s)]+))?$"
2031
)

datajoint/connection.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
"""
2-
This module contains the Connection class that manages the connection to the database, and
3-
the ``conn`` function that provides access to a persistent connection in datajoint.
2+
Database connection module for DataJoint.
3+
4+
This module contains the Connection class that manages the connection to the database,
5+
and the ``conn`` function that provides access to a persistent connection in datajoint.
6+
It handles connection pooling, transaction management, query execution, and query caching.
47
"""
58

9+
from __future__ import annotations
10+
611
import logging
712
import pathlib
813
import re
914
import warnings
1015
from contextlib import contextmanager
1116
from getpass import getpass
17+
from typing import TYPE_CHECKING, Any, Iterator
1218

1319
import pymysql as client
1420

@@ -19,6 +25,9 @@
1925
from .settings import config
2026
from .version import __version__
2127

28+
if TYPE_CHECKING:
29+
from .schemas import Schema
30+
2231
logger = logging.getLogger(__name__.split(".")[0])
2332
query_log_max_length = 300
2433

datajoint/declare.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
"""
2-
This module hosts functions to convert DataJoint table definitions into mysql table definitions, and to
3-
declare the corresponding mysql tables.
2+
Table declaration module for DataJoint.
3+
4+
This module hosts functions to convert DataJoint table definitions into MySQL table
5+
definitions, and to declare the corresponding MySQL tables. It parses definition strings
6+
and generates DDL statements for creating and altering tables.
47
"""
58

9+
from __future__ import annotations
10+
611
import logging
712
import re
813
from hashlib import sha1
14+
from typing import TYPE_CHECKING, Any
915

1016
import pyparsing as pp
1117

@@ -14,6 +20,9 @@
1420
from .errors import FILEPATH_FEATURE_SWITCH, DataJointError, _support_filepath_types
1521
from .settings import config
1622

23+
if TYPE_CHECKING:
24+
pass # No circular imports currently needed
25+
1726
UUID_DATA_TYPE = "binary(16)"
1827
MAX_TABLE_NAME_LENGTH = 64
1928
CONSTANT_LITERALS = {

datajoint/dependencies.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
"""
2+
Dependency graph module for tracking relationships between tables.
3+
4+
This module provides classes and functions for managing the dependency graph
5+
of DataJoint tables. It handles topological sorting, parent/child relationships,
6+
and ensures proper ordering for cascading operations like delete.
7+
"""
8+
9+
from __future__ import annotations
10+
111
import itertools
212
import re
313
from collections import defaultdict
14+
from typing import TYPE_CHECKING
415

516
import networkx as nx
617

718
from .errors import DataJointError
819

20+
if TYPE_CHECKING:
21+
from .connection import Connection
22+
923

1024
def extract_master(part_table):
1125
"""

0 commit comments

Comments
 (0)