def _to_model_class(self): """Returns the model class associated with this instance. Returns: type(BaseModel). The model type. """ return job_utils.get_model_class(self._model_kind)
def _to_model_class(self) -> Type[base_models.BaseModel]: """Returns the model class associated with this instance. Returns: type(BaseModel). The model type. """ model_class = job_utils.get_model_class(self._model_kind) # To narrow down the type from datastore_services.Model to # base_models.BaseModel, we used assert statement here. assert issubclass(model_class, base_models.BaseModel) return model_class
def get_model_kind_references(cls, model_kind, property_name): """Returns the kinds of models referenced by the given property. Args: model_kind: str. The kind of model the property belongs to. property_name: str. The property's name. Returns: list(str). The kinds of models referenced by the given property. """ model_cls = job_utils.get_model_class(model_kind) prop = model_property.ModelProperty( model_cls, getattr(model_cls, property_name)) return cls._ID_REFERENCING_PROPERTIES.get(prop, set())
def get_model_kind_references(cls, model_kind: str, property_name: str) -> Set[str]: """Returns the kinds of models referenced by the given property. Args: model_kind: str. The kind of model the property belongs to. property_name: str. The property's name. Returns: set(str). The kinds of models referenced by the given property. """ model_cls = job_utils.get_model_class(model_kind) # Here model_cls is of type Type[datastore_services.Model] but from the # implementation of ModelProperty it is clear that it can only accept # Type[base_models.BaseModel]. So to narrow down the type, we used # assert statement here. assert issubclass(model_cls, base_models.BaseModel) prop = model_property.ModelProperty(model_cls, getattr(model_cls, property_name)) return cls._ID_REFERENCING_PROPERTIES.get(prop, set())
def test_get_from_non_existing_model(self) -> None: with self.assertRaisesRegexp(Exception, 'No model class found'): # type: ignore[no-untyped-call] job_utils.get_model_class('InvalidModel')
def test_get_from_existing_model(self) -> None: self.assertIs( job_utils.get_model_class('BaseModel'), base_models.BaseModel)