Retrieves an instance from the database by its ID. Args: id_value (int): The ID of the instance to retrieve. Returns: Optional[T]: The retrieved instance, or None if not found. Raises: TypeError: If id_value is not an integer.
(self, id_value: int)
| 222 | return [self.model(**record) for record in filtered_data] # type: ignore |
| 223 | |
| 224 | def get(self, id_value: int) -> Optional[T]: |
| 225 | """Retrieves an instance from the database by its ID. |
| 226 | |
| 227 | Args: |
| 228 | id_value (int): The ID of the instance to retrieve. |
| 229 | |
| 230 | Returns: |
| 231 | Optional[T]: The retrieved instance, or None if not found. |
| 232 | |
| 233 | Raises: |
| 234 | TypeError: If id_value is not an integer. |
| 235 | IdNotFoundError: If no instance with the given ID is found. |
| 236 | """ |
| 237 | if not isinstance(id_value, int): |
| 238 | raise TypeError( |
| 239 | f"Expected id_value to be an int, got {type(id_value).__name__} instead." |
| 240 | ) |
| 241 | |
| 242 | data = self._read_data()["data"] |
| 243 | for record in data: |
| 244 | if record.get(self.id_fieldname) == id_value: |
| 245 | return self.model(**record) # type: ignore |
| 246 | |
| 247 | raise IdNotFoundError(f"Id {id_value!r} does not exist.") |
| 248 | |
| 249 | def delete(self, id_value: int) -> None: |
| 250 | """Deletes an instance from the database by its ID. |
no test coverage detected