Skip to content

Commit 65c07b7

Browse files
committed
Use a better repr for all types
now eval(repr(obj) == obj
1 parent 0e80b39 commit 65c07b7

44 files changed

Lines changed: 116 additions & 95 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyrogram/api/core/object.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,51 @@ class Object:
3030
QUALNAME = "Base"
3131

3232
@staticmethod
33-
def read(b: BytesIO, *args):
33+
def read(b: BytesIO, *args): # TODO: Rename b -> data
3434
return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args)
3535

3636
def write(self, *args) -> bytes:
3737
pass
3838

39+
def __eq__(self, other: "Object") -> bool:
40+
for attr in self.__slots__:
41+
try:
42+
if getattr(self, attr) != getattr(other, attr):
43+
return False
44+
except AttributeError:
45+
return False
46+
47+
return True
48+
3949
def __str__(self) -> str:
50+
def default(obj: Object):
51+
try:
52+
return OrderedDict(
53+
[("_", obj.QUALNAME)]
54+
+ [(attr, getattr(obj, attr))
55+
for attr in obj.__slots__
56+
if getattr(obj, attr) is not None]
57+
)
58+
except AttributeError:
59+
if isinstance(obj, datetime):
60+
return obj.strftime("%d-%b-%Y %H:%M:%S")
61+
else:
62+
return repr(obj)
63+
4064
return dumps(self, indent=4, default=default, ensure_ascii=False)
4165

66+
def __repr__(self) -> str:
67+
return "pyrogram.api.{}({})".format(
68+
self.QUALNAME,
69+
", ".join(
70+
"{}={}".format(attr, repr(getattr(self, attr)))
71+
for attr in self.__slots__
72+
if getattr(self, attr) is not None
73+
)
74+
)
75+
4276
def __len__(self) -> int:
4377
return len(self.write())
4478

4579
def __getitem__(self, item):
4680
return getattr(self, item)
47-
48-
49-
def remove_none(obj):
50-
if isinstance(obj, (list, tuple, set)):
51-
return type(obj)(remove_none(x) for x in obj if x is not None)
52-
elif isinstance(obj, dict):
53-
return type(obj)((remove_none(k), remove_none(v)) for k, v in obj.items() if k is not None and v is not None)
54-
else:
55-
return obj
56-
57-
58-
def default(o: "Object"):
59-
try:
60-
content = {i: getattr(o, i) for i in o.__slots__}
61-
62-
return remove_none(
63-
OrderedDict(
64-
[("_", o.QUALNAME)]
65-
+ [i for i in content.items()]
66-
)
67-
)
68-
except AttributeError:
69-
if isinstance(o, datetime):
70-
return o.strftime("%d-%b-%Y %H:%M:%S")
71-
else:
72-
return repr(o)

pyrogram/client/types/inline_mode/inline_query.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class InlineQuery(PyrogramType, Update):
5353
def __init__(
5454
self,
5555
*,
56-
client: "pyrogram.client.ext.BaseClient",
56+
client: "pyrogram.BaseClient" = None,
5757
id: str,
5858
from_user: User,
5959
query: str,
@@ -62,7 +62,6 @@ def __init__(
6262
):
6363
super().__init__(client)
6464

65-
self._client = client
6665
self.id = id
6766
self.from_user = from_user
6867
self.query = query

pyrogram/client/types/inline_mode/inline_query_result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class InlineQueryResult(PyrogramType):
5050
__slots__ = ["type", "id"]
5151

5252
def __init__(self, type: str, id: str):
53-
super().__init__(None)
53+
super().__init__()
5454

5555
self.type = type
5656
self.id = id

pyrogram/client/types/input_media/input_media.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class InputMedia(PyrogramType):
3333
__slots__ = ["media", "caption", "parse_mode"]
3434

3535
def __init__(self, media: str, caption: str, parse_mode: str):
36-
super().__init__(None)
36+
super().__init__()
3737

3838
self.media = media
3939
self.caption = caption

pyrogram/client/types/input_message_content/input_message_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ class InputMessageContent(PyrogramType):
3434
__slots__ = []
3535

3636
def __init__(self):
37-
super().__init__(None)
37+
super().__init__()

pyrogram/client/types/keyboards/callback_game.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ class CallbackGame(PyrogramType):
2828
__slots__ = []
2929

3030
def __init__(self):
31-
super().__init__(None)
31+
super().__init__()

pyrogram/client/types/keyboards/callback_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CallbackQuery(PyrogramType, Update):
6464
def __init__(
6565
self,
6666
*,
67-
client: "pyrogram.client.ext.BaseClient",
67+
client: "pyrogram.BaseClient" = None,
6868
id: str,
6969
from_user: User,
7070
chat_instance: str,

pyrogram/client/types/keyboards/force_reply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
self,
4343
selective: bool = None
4444
):
45-
super().__init__(None)
45+
super().__init__()
4646

4747
self.selective = selective
4848

pyrogram/client/types/keyboards/game_high_score.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class GameHighScore(PyrogramType):
4242
def __init__(
4343
self,
4444
*,
45-
client: "pyrogram.client.ext.BaseClient",
45+
client: "pyrogram.BaseClient" = None,
4646
user: User,
4747
score: int,
4848
position: int = None

pyrogram/client/types/keyboards/game_high_scores.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class GameHighScores(PyrogramType):
4040
def __init__(
4141
self,
4242
*,
43-
client: "pyrogram.client.ext.BaseClient",
43+
client: "pyrogram.BaseClient" = None,
4444
total_count: int,
4545
game_high_scores: List[GameHighScore]
4646
):

0 commit comments

Comments
 (0)