1
1
2
2
import logging
3
3
from functools import reduce
4
- from typing import List , Dict , Any , NamedTuple , Union , Optional , Set , Callable , Literal
4
+ from typing import List , Dict , Any , NamedTuple , Union , Optional , Set , Callable , Literal , get_type_hints
5
5
6
6
from django .db import transaction , IntegrityError
7
7
@@ -79,13 +79,23 @@ class DeferredScopeUploadTable(NamedTuple):
79
79
relationship_name : str
80
80
filter_field : str
81
81
82
- # In a DeferredScopeUploadTable, the overrideScope value can be either an integer
83
- # (which follows the same logic as in UploadTable), or a function which has the parameter
84
- # signature: (deferred_upload_plan: DeferredScopeUploadTable, row_index: int) -> models.Collection
85
- # (see apply_deferred_scopes in .upload.py)
86
- overrideScope : Optional [Dict [Literal ["collection" ], Union [int , Callable [["DeferredScopeUploadTable" , int ], Any ]]]] = None
82
+ """ In a DeferredScopeUploadTable, the overrideScope value can be either an integer
83
+ (which follows the same logic as in UploadTable), or a function which has the parameter
84
+ signature: (deferred_upload_plan: DeferredScopeUploadTable, row_index: int) -> models.Collection
85
+ (see apply_deferred_scopes in .upload.py)
87
86
88
- def apply_scoping (self , collection , defer : bool = True ) -> Union ["ScopedUploadTable" , "DeferredScopeUploadTable" ]:
87
+ overrideScope should be of type
88
+ Optional[Dict[Literal["collection"], Union[int, Callable[["DeferredScopeUploadTable", int], Any]]]]
89
+
90
+ But recursively using the type within the class definition of a NamedTuple is not supported in our version
91
+ of mypy
92
+ See https://github.com/python/mypy/issues/8695
93
+ """
94
+ overrideScope : Optional [Dict [Literal ["collection" ], Union [int , Callable [[NamedTuple , int ], Any ]]]] = None
95
+
96
+
97
+ # Typehint for return type should be: Union["ScopedUploadTable", "DeferredScopeUploadTable"]
98
+ def apply_scoping (self , collection , defer : bool = True ) -> Union ["ScopedUploadTable" , NamedTuple ]:
89
99
if not defer :
90
100
from .scoping import apply_scoping_to_uploadtable
91
101
return apply_scoping_to_uploadtable (self , collection )
@@ -96,7 +106,12 @@ def get_cols(self) -> Set[str]:
96
106
| set (col for u in self .toOne .values () for col in u .get_cols ()) \
97
107
| set (col for rs in self .toMany .values () for r in rs for col in r .get_cols ())
98
108
99
- def add_colleciton_override (self , collection : Union [int , Callable [["DeferredScopeUploadTable" , int ], Any ]]) -> "DeferredScopeUploadTable" :
109
+
110
+ """
111
+ The Typehint for parameter collection should be: Union[int, Callable[["DeferredScopeUploadTable", int], Any]]
112
+ The Typehint for return type should be: "DeferredScopeUploadTable"
113
+ """
114
+ def add_colleciton_override (self , collection : Union [int , Callable [[NamedTuple , int ], Any ]]) -> NamedTuple :
100
115
''' To modify the overrideScope after the DeferredScope UploadTable is created, use add_colleciton_override
101
116
To properly apply scoping (see self.bind()), the <collection> should either be a collection's id, or a callable (function),
102
117
which has paramaters that accept: this DeferredScope UploadTable, and an integer representing the current row_index.
0 commit comments