Skip to content
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

Disallow unserializable #408

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open

Disallow unserializable #408

wants to merge 3 commits into from

Conversation

pseusys
Copy link
Collaborator

@pseusys pseusys commented Nov 18, 2024

Description

I checked it and to the best of my knowledge it should work now.

Checklist

  • I have performed a self-review of the changes

List here tasks to complete in order to mark this PR as ready for review.

To Consider

  • Add tests (if functionality is changed)
  • Update API reference / tutorials / guides
  • Update CONTRIBUTING.md (if devel workflow is changed)
  • Update .ignore files, scripts (such as lint), distribution manifest (if files are added/deleted)
  • Search for references to changed entities in the codebase

@pseusys pseusys requested a review from RLKRo November 18, 2024 10:57
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears this PR is a release PR (change its base from master if that is not the case).

Here's a release checklist:

  • Update package version
  • Update poetry.lock
  • Change PR merge option
  • Update template repo
  • Search for objects to be deprecated
  • Test parts not covered with pytest:
    • web_api tutorials
    • Test integrations with external services (telegram; stats)

@pseusys pseusys changed the base branch from master to dev November 18, 2024 10:58
@pseusys pseusys self-assigned this Nov 18, 2024
Copy link
Member

@RLKRo RLKRo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to use model_construct when working with models that had pickle serialization utils because model_validate would try to use pickle_validator and fail.
Now that it is no longer use, we can replace model_construct calls with __init__.

Also: need to update docs (context guide, context api ref, slots guide, slots api ref, e.t.c.) to mention that it is forbidden to store unserializable types.

original_message: Optional[Any] = None
annotations: Optional[Dict[str, Union[BaseModel, JsonValue]]] = None
misc: Optional[Dict[str, Union[BaseModel, JsonValue]]] = None
original_message: Optional[Union[BaseModel, JsonValue]] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge dev to have #398 here.

if value is not None:
return pickle_validator(value)
return value
extracted_value: Union[BaseModel, JsonValue]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slots store exceptions on failure (which are not serializable).
I think we should store exception representation instead.

misc: Optional[Dict[str, Any]] = None
original_message: Optional[Any] = None
annotations: Optional[Dict[str, Union[BaseModel, JsonValue]]] = None
misc: Optional[Dict[str, Union[BaseModel, JsonValue]]] = None
Copy link
Member

@RLKRo RLKRo Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change type annotation (for Union[BaseModel, JsonValue]) to allow deeper BaseModel usage (e.g. a dictionary or a list with BaseModel values).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PydanticValue: TypeAlias = Union[
    List["PydanticValue"],
    Dict[str, "PydanticValue"],
    BaseModel,
    str,
    bool,
    int,
    float,
    None,
]

@@ -627,7 +627,7 @@ async def _on_event(self, update: Update, _: Any, create_message: Callable[[Upda
data_available = update.message is not None or update.callback_query is not None
if update.effective_chat is not None and data_available:
message = create_message(update)
message.original_message = update
message.original_message = update.to_dict(recursive=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to apply to extra fields in Attachments.
Add a validator for Attachment and Message extras that modifies the extra field via to_dict if the field is of the TelegramObject value.

AFAIK if the extra field value is a dictionary from to_dict it should still work for the tg bot methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants