fix: deserialize via to_object method (#624)

This commit is contained in:
Oleg A 2021-07-10 22:43:36 +03:00 committed by GitHub
parent 2b4e3ad5c6
commit 7a9bab01eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -139,14 +139,18 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
return type(self).telegram_types
@classmethod
def to_object(cls: typing.Type[T], data: typing.Dict[str, typing.Any]) -> T:
def to_object(cls: typing.Type[T],
data: typing.Dict[str, typing.Any],
conf: typing.Dict[str, typing.Any] = None
) -> T:
"""
Deserialize object
:param data:
:param conf:
:return:
"""
return cls(**data)
return cls(conf=conf, **data)
@property
def bot(self) -> Bot:

View file

@ -1,4 +1,5 @@
import datetime
import typing
from typing import Optional
from . import base, fields
@ -6,6 +7,9 @@ from .user import User
from ..utils import helper
T = typing.TypeVar('T')
class ChatMemberStatus(helper.Helper):
"""
Chat member status
@ -81,6 +85,13 @@ class ChatMember(base.TelegramObject):
return class_(**kwargs)
@classmethod
def to_object(cls,
data: typing.Dict[str, typing.Any],
conf: typing.Dict[str, typing.Any] = None
) -> "ChatMember":
return cls.resolve(**data)
def is_chat_creator(self) -> bool:
return ChatMemberStatus.is_chat_creator(self.status)

View file

@ -112,7 +112,7 @@ class Field(BaseField):
and not hasattr(value, 'to_python'):
if not isinstance(parent, weakref.ReferenceType):
parent = weakref.ref(parent)
return self.base_object(conf={'parent':parent}, **value)
return self.base_object.to_object(conf={'parent': parent}, data=value)
return value