Commit 36921ac
Add return-type annotations to core models and import/export pipeline (#1355)
* typing: annotate core models + import/export tasks (Closes #1334)
Adds return-type annotations and TypedDict-based parameter annotations
to the core domain models (corpuses, documents, annotations, extracts)
and the bulk import/export pipeline, without changing runtime behavior.
Coverage (models.py + sibling files in each package):
- corpuses/: 61.5% -> 88.4% (target >=80%)
- annotations/: 48.1% -> 93.8% (target >=80%)
- documents/: 51.9% -> 84.9% (target >=80%)
- extracts/: 47.1% -> 94.1% (target >=75%)
- tasks/{import,export}{,_v2}.py: 100% function-signature coverage
Each touched file now uses `from __future__ import annotations` so
forward refs work unquoted, with `TYPE_CHECKING` blocks to dodge
circular imports across corpuses / documents / annotations / extracts.
Import/export tasks now consume existing TypedDicts from
opencontractserver/types/dicts.py in their signatures instead of bare
dict -- OpenContractsExportDataJsonV2Type, OpenContractDocExport,
OpenContractsAnnotationPythonType, OpenContractsRelationshipPythonType,
IngestionSourceExport, DocumentPathExport, StructuralAnnotationSetExport,
AgentConfigExport, DescriptionRevisionExport, ConversationExport,
ChatMessageExport, MessageVoteExport, OpenContractsAnnotatedDocumentImportType.
No new TypedDicts were introduced; all were already defined.
Signal handlers (annotations/signals.py, corpuses/signals.py,
documents/signals.py, extracts/signals.py) now type sender / instance /
created / **kwargs using TYPE_CHECKING imports of sender model classes.
Manager methods on CorpusActionExecutionManager tighten visible_to_user's
user param to Optional[AbstractBaseUser] and tighten summary_by_status /
summary_by_action returns.
Graduation from the mypy baseline is deferred: the affected modules
still have pre-existing Django-plugin-specific errors (CharField
assignment mismatches, base-class variable overrides on
embedder_path / creator, lambdas in field defaults) that are not
caused by missing annotations. Per the issue's "annotations only, no
bugfixes" constraint, those are left for a follow-up PR.
* Fix linting and address PR review comments
- Drop unused Optional / Union / AbstractBaseUser imports (flake8 F401)
after pyupgrade rewrote Optional[X] to X | None
- Pass Annotation / Note class instead of None for sender in signal
tests so mypy accepts the typed signal signatures
- Tighten save() -> None on Corpus, CorpusAction, and Note (Django's
Model.save always returns None); drop dangling return super().save()
- Tighten Document.get_embedding_reference_kwargs -> dict[str, Any]
- Type structural_sets_seen as set[StructuralAnnotationSet]
- Use AbstractBaseUser (TYPE_CHECKING) for Corpus.update_description
author and get_or_create_personal_corpus user parameters instead of Any
- Remove unused AbstractBaseUser TYPE_CHECKING imports from export_tasks
and export_tasks_v2
* Address review feedback and unblock mypy CI
Review feedback (PR #1355 second Claude review):
- PipelineSettings.delete() now annotated -> NoReturn. The method always
raises, so the previous tuple[int, dict[str, int]] annotation (copied
from Django's Model.delete signature) would let mypy accept call sites
that try to unpack the return value.
- CorpusFolder.get_descendant_folders() now returns
QuerySet[CorpusFolder] with a narrow return-value type: ignore. The
CTE-annotated queryset subclass is not easily importable, but callers
iterate over CorpusFolder instances -- Any was strictly less useful.
- _import_v2_relationships' label_lookup parameter gains an inline
comment documenting the (label_text, label_type) tuple-key invariant.
Other reviewer items skipped with reasoning:
- dict[str | None, OpenContractDocExport | None] in export_tasks.py --
tightening requires tracing upstream None-producers; reviewer marked
it non-blocking.
- author: AbstractBaseUser | int unions -- reviewer explicitly flagged
as follow-up work outside this annotation-only PR.
- corpus_id: int | None local annotation -- reviewer called it fine to
leave.
mypy unblock:
- validate_v3_migration.py was fixed in main (commit 41eb6ae) for the
[:5]-indexing error that 6.0.3 surfaced, but the values_list + unpack
pattern still trips mypy 1.20.1 + django-stubs 6.0.3 with "object is
not iterable" and "Cannot determine type" errors. Suppress narrowly
with type: ignore on the two affected lines; runtime behaviour is
correct and the alternative refactors obscured the loop body.
* Address review: use AbstractBaseUser, drop unneeded has-type ignore
* Address PR #1355 review: tighten Any types + drop redundant comment
- CorpusActionTemplate.to_action_kwargs / clone_to_corpus: replace
'creator: Any | None' with 'creator: AbstractBaseUser | None' for
consistency with the rest of the file (the import is already in the
TYPE_CHECKING block).
- Corpus.get_descendant_folders: drop the two-line block comment about
tree-queries' CTE-annotated subclass — the inline 'type: ignore' is
already self-documenting and the comment violated CLAUDE.md's
no-redundant-comments guideline.
---------
Signed-off-by: JSIV <5049984+JSv4@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>1 parent 50ed674 commit 36921ac
16 files changed
Lines changed: 429 additions & 224 deletions
File tree
- opencontractserver
- annotations
- corpuses
- documents
- extracts
- tasks
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
22 | 35 | | |
23 | 36 | | |
24 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
1 | 5 | | |
2 | | - | |
| 6 | + | |
3 | 7 | | |
4 | 8 | | |
5 | 9 | | |
| |||
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
21 | 28 | | |
22 | 29 | | |
23 | 30 | | |
24 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
25 | 36 | | |
26 | 37 | | |
27 | 38 | | |
| |||
31 | 42 | | |
32 | 43 | | |
33 | 44 | | |
34 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
35 | 50 | | |
36 | 51 | | |
37 | 52 | | |
| |||
61 | 76 | | |
62 | 77 | | |
63 | 78 | | |
64 | | - | |
| 79 | + | |
65 | 80 | | |
66 | 81 | | |
67 | 82 | | |
| |||
73 | 88 | | |
74 | 89 | | |
75 | 90 | | |
76 | | - | |
| 91 | + | |
77 | 92 | | |
78 | 93 | | |
79 | 94 | | |
| |||
87 | 102 | | |
88 | 103 | | |
89 | 104 | | |
90 | | - | |
| 105 | + | |
91 | 106 | | |
92 | 107 | | |
93 | 108 | | |
| |||
99 | 114 | | |
100 | 115 | | |
101 | 116 | | |
102 | | - | |
| 117 | + | |
103 | 118 | | |
104 | 119 | | |
105 | 120 | | |
| |||
125 | 140 | | |
126 | 141 | | |
127 | 142 | | |
128 | | - | |
| 143 | + | |
129 | 144 | | |
130 | 145 | | |
131 | 146 | | |
132 | 147 | | |
133 | 148 | | |
134 | 149 | | |
135 | | - | |
| 150 | + | |
136 | 151 | | |
137 | 152 | | |
138 | 153 | | |
139 | | - | |
| 154 | + | |
140 | 155 | | |
141 | | - | |
142 | | - | |
| 156 | + | |
| 157 | + | |
143 | 158 | | |
144 | | - | |
| 159 | + | |
145 | 160 | | |
146 | 161 | | |
147 | 162 | | |
148 | 163 | | |
149 | 164 | | |
150 | 165 | | |
151 | 166 | | |
152 | | - | |
| 167 | + | |
153 | 168 | | |
154 | 169 | | |
155 | 170 | | |
| |||
194 | 209 | | |
195 | 210 | | |
196 | 211 | | |
197 | | - | |
| 212 | + | |
198 | 213 | | |
199 | 214 | | |
200 | 215 | | |
201 | 216 | | |
202 | 217 | | |
203 | 218 | | |
204 | | - | |
| 219 | + | |
205 | 220 | | |
206 | 221 | | |
207 | 222 | | |
208 | | - | |
| 223 | + | |
209 | 224 | | |
210 | | - | |
211 | | - | |
| 225 | + | |
| 226 | + | |
212 | 227 | | |
213 | 228 | | |
214 | 229 | | |
| |||
227 | 242 | | |
228 | 243 | | |
229 | 244 | | |
230 | | - | |
| 245 | + | |
231 | 246 | | |
232 | 247 | | |
233 | 248 | | |
| |||
237 | 252 | | |
238 | 253 | | |
239 | 254 | | |
240 | | - | |
| 255 | + | |
241 | 256 | | |
242 | | - | |
| 257 | + | |
243 | 258 | | |
244 | | - | |
| 259 | + | |
245 | 260 | | |
246 | 261 | | |
247 | 262 | | |
| |||
252 | 267 | | |
253 | 268 | | |
254 | 269 | | |
255 | | - | |
| 270 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
53 | 58 | | |
54 | 59 | | |
55 | 60 | | |
| |||
157 | 162 | | |
158 | 163 | | |
159 | 164 | | |
160 | | - | |
| 165 | + | |
161 | 166 | | |
162 | 167 | | |
163 | 168 | | |
| |||
376 | 381 | | |
377 | 382 | | |
378 | 383 | | |
379 | | - | |
| 384 | + | |
380 | 385 | | |
381 | 386 | | |
382 | 387 | | |
| |||
479 | 484 | | |
480 | 485 | | |
481 | 486 | | |
482 | | - | |
| 487 | + | |
483 | 488 | | |
484 | 489 | | |
485 | 490 | | |
| |||
583 | 588 | | |
584 | 589 | | |
585 | 590 | | |
586 | | - | |
| 591 | + | |
587 | 592 | | |
588 | 593 | | |
589 | 594 | | |
| |||
660 | 665 | | |
661 | 666 | | |
662 | 667 | | |
663 | | - | |
| 668 | + | |
664 | 669 | | |
665 | 670 | | |
666 | 671 | | |
667 | | - | |
| 672 | + | |
668 | 673 | | |
669 | 674 | | |
670 | 675 | | |
671 | 676 | | |
672 | | - | |
| 677 | + | |
673 | 678 | | |
674 | 679 | | |
675 | 680 | | |
676 | | - | |
| 681 | + | |
677 | 682 | | |
678 | 683 | | |
679 | 684 | | |
| |||
992 | 997 | | |
993 | 998 | | |
994 | 999 | | |
995 | | - | |
| 1000 | + | |
996 | 1001 | | |
997 | 1002 | | |
998 | 1003 | | |
999 | 1004 | | |
1000 | 1005 | | |
1001 | 1006 | | |
1002 | 1007 | | |
1003 | | - | |
| 1008 | + | |
1004 | 1009 | | |
1005 | 1010 | | |
1006 | 1011 | | |
| |||
1128 | 1133 | | |
1129 | 1134 | | |
1130 | 1135 | | |
1131 | | - | |
| 1136 | + | |
1132 | 1137 | | |
1133 | 1138 | | |
1134 | 1139 | | |
| |||
1258 | 1263 | | |
1259 | 1264 | | |
1260 | 1265 | | |
1261 | | - | |
| 1266 | + | |
1262 | 1267 | | |
1263 | 1268 | | |
1264 | 1269 | | |
| |||
1410 | 1415 | | |
1411 | 1416 | | |
1412 | 1417 | | |
1413 | | - | |
| 1418 | + | |
1414 | 1419 | | |
1415 | 1420 | | |
1416 | 1421 | | |
| |||
1438 | 1443 | | |
1439 | 1444 | | |
1440 | 1445 | | |
1441 | | - | |
| 1446 | + | |
1442 | 1447 | | |
1443 | 1448 | | |
1444 | 1449 | | |
| |||
1480 | 1485 | | |
1481 | 1486 | | |
1482 | 1487 | | |
1483 | | - | |
1484 | | - | |
1485 | | - | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
1486 | 1494 | | |
1487 | 1495 | | |
1488 | 1496 | | |
| |||
1546 | 1554 | | |
1547 | 1555 | | |
1548 | 1556 | | |
1549 | | - | |
| 1557 | + | |
1550 | 1558 | | |
1551 | 1559 | | |
1552 | 1560 | | |
| |||
1630 | 1638 | | |
1631 | 1639 | | |
1632 | 1640 | | |
1633 | | - | |
| 1641 | + | |
1634 | 1642 | | |
0 commit comments