1010from ..remote import RemoteObject , get_local_resource_prefix , get_resource_prefix
1111
1212
13- class DABContentTypeManager (django_models .Manager ["DABContentType" ]):
13+ class DABContentTypeManager (django_models .Manager [django_models . Model ]):
1414 """Manager storing DABContentType objects in a local cache like original ContentType.
1515
1616 The major structural difference is that the cache keys have to add the service reference.
@@ -20,23 +20,23 @@ class DABContentTypeManager(django_models.Manager["DABContentType"]):
2020
2121 def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
2222 super ().__init__ (* args , ** kwargs )
23- self ._cache : Dict [str , Dict [Union [Tuple [str , str , str ], int ], "DABContentType" ]] = {}
23+ self ._cache : Dict [str , Dict [Union [Tuple [str , str , str ], int ], django_models . Model ]] = {}
2424
2525 def clear_cache (self ) -> None :
2626 self ._cache .clear ()
2727
28- def create (self , * args : Any , ** kwargs : Any ) -> "DABContentType" :
28+ def create (self , * args : Any , ** kwargs : Any ) -> django_models . Model :
2929 obj = super ().create (* args , ** kwargs )
3030 self ._add_to_cache (self .db , obj )
3131 return obj
3232
33- def _add_to_cache (self , using : str , ct : "DABContentType" ) -> None :
33+ def _add_to_cache (self , using : str , ct : django_models . Model ) -> None :
3434 """Store ``ct`` in the manager cache for the given database alias."""
3535 key = (ct .service , ct .app_label , ct .model )
3636 self ._cache .setdefault (using , {})[key ] = ct
3737 self ._cache .setdefault (using , {})[ct .id ] = ct
3838
39- def _get_from_cache (self , opts : Options , service : str ) -> "DABContentType" :
39+ def _get_from_cache (self , opts : Options , service : str ) -> django_models . Model :
4040 """Return a cached ``DABContentType`` for ``opts`` and ``service``."""
4141 key = (service , opts .app_label , opts .model_name )
4242 return self ._cache [self .db ][key ]
@@ -50,7 +50,7 @@ def get_for_model(
5050 model : Union [Type [django_models .Model ], django_models .Model , RemoteObject , Type [RemoteObject ]],
5151 for_concrete_model : bool = True ,
5252 service : Optional [str ] = None ,
53- ) -> "DABContentType" :
53+ ) -> django_models . Model :
5454 # Is a remote object, we only know of these objects by virtue of their content type
5555 if isinstance (model , RemoteObject ):
5656 ct = model .content_type
@@ -84,15 +84,15 @@ def get_for_models(
8484 * model_list : Union [Type [django_models .Model ], django_models .Model ],
8585 for_concrete_models : bool = True ,
8686 service : Optional [str ] = None ,
87- ) -> Dict [Type [django_models .Model ], "DABContentType" ]:
87+ ) -> Dict [Type [django_models .Model ], django_models . Model ]:
8888 """Return ``DABContentType`` objects for each model in ``model_list``.
8989
9090 This gets deep into the customization of unique rules for DAB RBAC.
9191 We require that model_name must be unique for a given service,
9292 and this will rely on that assumption, which compares to app_label
9393 in the original ContentType model.
9494 """
95- results : Dict [Type [django_models .Model ], "DABContentType" ] = {}
95+ results : Dict [Type [django_models .Model ], django_models . Model ] = {}
9696 # A keyed by (service, app_name) unlike Django where it was just app_name
9797 needed_models : Dict [Tuple [str , str ], set [str ]] = defaultdict (set )
9898 # A dict of (service, app_name, model_name), differs from Django ContentType
@@ -135,8 +135,12 @@ def get_for_models(
135135 )
136136 return results
137137
138- def get_by_natural_key (self , * args : str ) -> "DABContentType" :
139- """Return the content type identified by its natural key."""
138+ def get_by_natural_key (self , * args : str ) -> django_models .Model :
139+ """Return the content type identified by its natural key.
140+
141+ Note that we can not type hint the return value fully because it is used in migrations.
142+ Migrations will return a prior model state.
143+ """
140144 if len (args ) == 2 :
141145 service = get_local_resource_prefix ()
142146 app_label , model = args
@@ -160,7 +164,7 @@ def get_by_natural_key(self, *args: str) -> "DABContentType":
160164 self ._add_to_cache (self .db , ct )
161165 return ct
162166
163- def get_for_id (self , id : int ) -> "DABContentType" :
167+ def get_for_id (self , id : int ) -> django_models . Model :
164168 """Return the content type with primary key ``id`` from the cache."""
165169 try :
166170 return self ._cache [self .db ][id ]
0 commit comments