-
Notifications
You must be signed in to change notification settings - Fork 4
[LABIMP-8422] List templates API integration #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
206046e
01adf9a
844171b
fbb7396
aa274a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,18 @@ def get_annotation_template(client: "LabellerrClient", annotation_template_id: s | |
|
|
||
| """Base class for all Labellerr projects with factory behavior""" | ||
|
|
||
| def __new__(cls, client: "LabellerrClient", annotation_template_id: str): | ||
| # Validate that the annotation template exists before creating the instance | ||
| def __new__( | ||
| cls, | ||
| client: "LabellerrClient", | ||
| annotation_template_id: str, | ||
| _skip_api_fetch: bool = False, | ||
| **kwargs, | ||
| ): | ||
| # If skip flag is set, create instance without API call | ||
| if _skip_api_fetch: | ||
| return super().__new__(cls) | ||
|
|
||
| # Otherwise, fetch from API and validate | ||
| annotation_template_data = cls.get_annotation_template( | ||
| client, annotation_template_id | ||
| ) | ||
|
|
@@ -37,14 +47,82 @@ def __new__(cls, client: "LabellerrClient", annotation_template_id: str): | |
| f"Annotation template with ID '{annotation_template_id}' does not exist or could not be retrieved." | ||
| ) | ||
|
|
||
| # Create the instance only if validation passes | ||
| instance = super().__new__(cls) | ||
| # Store the data on the instance to avoid calling API again in __init__ | ||
| instance.__annotation_template_data = annotation_template_data | ||
| return instance | ||
| # Pass fetched data to __init__ via kwargs | ||
| kwargs["_fetched_data"] = annotation_template_data | ||
|
ximihoque marked this conversation as resolved.
|
||
| return super().__new__(cls) | ||
|
|
||
| def __init__(self, client: "LabellerrClient", annotation_template_id: str): | ||
| def __init__( | ||
| self, | ||
| client: "LabellerrClient", | ||
| annotation_template_id: str, | ||
| _skip_api_fetch: bool = False, | ||
| **kwargs, | ||
| ): | ||
| self.client = client | ||
| self.annotation_template_id = annotation_template_id | ||
| # Use the data already fetched in __new__ | ||
| self.annotation_template_data = self.__annotation_template_data | ||
| self.__annotation_template_id = annotation_template_id | ||
|
|
||
| # Set __annotation_template_data from either source | ||
| if "_cached_data" in kwargs: | ||
| # Data provided directly (from factory method) | ||
| self.__annotation_template_data = kwargs["_cached_data"] | ||
| elif "_fetched_data" in kwargs: | ||
| # Data fetched in __new__ | ||
| self.__annotation_template_data = kwargs["_fetched_data"] | ||
| else: | ||
| # Fallback - shouldn't happen in normal usage | ||
| self.__annotation_template_data = {} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Silent failure with empty dictionary The fallback to an empty dictionary means all property accessors (template_name, data_type, questions, etc.) will return Recommendation: Raise an exception here instead: else:
raise RuntimeError(
"LabellerrAnnotationTemplate instantiated without data. "
"Use from_annotation_template_data() or let __new__ fetch it."
) |
||
|
|
||
| @classmethod | ||
| def from_annotation_template_data(cls, client: "LabellerrClient", **kwargs): | ||
| """ | ||
| Create a LabellerrAnnotationTemplate instance from annotation template data. | ||
|
|
||
| :param client: LabellerrClient instance | ||
| :param kwargs: Annotation template fields (template_id, template_name, questions, etc.) | ||
| :return: Instance of LabellerrAnnotationTemplate | ||
| """ | ||
| # Validate required fields | ||
| required_fields = { | ||
| "template_id", | ||
| "template_name", | ||
| "questions", | ||
| "created_at", | ||
| "created_by", | ||
| } | ||
| missing_fields = required_fields - set(kwargs.keys()) | ||
|
ximihoque marked this conversation as resolved.
|
||
| if missing_fields: | ||
| raise ValueError( | ||
| f"Missing required fields in annotation_template_data: {missing_fields}" | ||
| ) | ||
|
ximihoque marked this conversation as resolved.
|
||
|
|
||
| # Create instance without API call - explicit flag makes intent clear | ||
| return cls( | ||
| client, | ||
| annotation_template_id=kwargs.get("template_id"), | ||
| _skip_api_fetch=True, | ||
| _cached_data=kwargs, | ||
| ) | ||
|
|
||
| @property | ||
| def template_name(self): | ||
| return self.__annotation_template_data.get("template_name") | ||
|
|
||
| @property | ||
| def data_type(self): | ||
| return self.__annotation_template_data.get("data_type") | ||
|
|
||
| @property | ||
| def template_id(self): | ||
| return self.__annotation_template_id | ||
|
|
||
| @property | ||
| def created_at(self): | ||
| return self.__annotation_template_data.get("created_at") | ||
|
|
||
| @property | ||
| def created_by(self): | ||
| return self.__annotation_template_data.get("created_by") | ||
|
|
||
| @property | ||
| def questions(self): | ||
| return self.__annotation_template_data.get("questions") | ||
|
ximihoque marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from .base import LabellerrProject, LabellerrProjectMeta | ||
|
|
||
|
|
||
| class TextProject(LabellerrProject): | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| LabellerrProjectMeta._register("text", TextProject) |
Uh oh!
There was an error while loading. Please reload this page.