Skip to content

Commit 5639d30

Browse files
committed
feature: add type hint to event.py, row_event.py, binlogfilereader.py
Co-authored-by: suhwan-cheon <[email protected]> Co-authored-by: ebang091 <[email protected]> Co-authored-by: yangbaechu <[email protected]> Co-authored-by: jaehyeonpy <[email protected]>
1 parent ffc6d54 commit 5639d30

File tree

1 file changed

+12
-102
lines changed

1 file changed

+12
-102
lines changed

Diff for: pymysqlreplication/event.py

+12-102
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pymysql
77
import decimal
88
import zlib
9-
from pymysql.protocol import MysqlPacket
9+
from pymysql.connection import MysqlPacket
1010
from typing import Dict, List, NoReturn, Tuple, Type, Union
1111
from pymysqlreplication.constants.STATUS_VAR_KEY import *
1212
from pymysqlreplication.exceptions import StatusVariableMismatch
@@ -211,12 +211,7 @@ class MariadbAnnotateRowsEvent(BinLogEvent):
211211
If you want to check this binlog, change the value of the flag(line 382 of the 'binlogstream.py') option to 2
212212
https://mariadb.com/kb/en/annotate_rows_event/
213213
214-
<<<<<<< HEAD
215214
:ivar sql_statement: str - The SQL statement
216-
=======
217-
Attributes:
218-
sql_statement: str - The SQL statement
219-
>>>>>>> 8811fca (docs: add typing in MariadbGtidListEvent, MariadbAnnotateRowsEvent)
220215
"""
221216
def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs):
222217
super().__init__(from_packet, event_size, table_map, ctl_connection, **kwargs)
@@ -231,22 +226,11 @@ class MariadbGtidListEvent(BinLogEvent):
231226
for more information: `[see MariadbGtidListEvent] <https://mariadb.com/kb/en/gtid_list_event/>`_
232227
233228
:ivar gtid_length: int - Number of GTIDs
234-
<<<<<<< HEAD
235-
:ivar gtid_list: list - list of 'MariadbGtidObejct'
236-
237-
'MariadbGtidObejct' Attributes:
238-
domain_id: Replication Domain ID
239-
server_id: Server_ID
240-
gtid_seq_no: GTID sequence
241-
gtid: 'domain_id'+ 'server_id' + 'gtid_seq_no'
242-
=======
243229
:ivar gtid_list: List - list of 'MariadbGtidObejct'
244-
245230
:ivar domain_id: int - Replication Domain ID (MariadbGtidObject)
246231
:ivar server_id: int -Server_ID (MariadbGtidObject)
247232
:ivar gtid_seq_no: int - GTID sequence (MariadbGtidObject)
248233
:ivar gtid: str - 'domain_id'+ 'server_id' + 'gtid_seq_no' (MariadbGtidObject)
249-
>>>>>>> 8811fca (docs: add typing in MariadbGtidListEvent, MariadbAnnotateRowsEvent)
250234
"""
251235
def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs):
252236

@@ -270,15 +254,10 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
270254

271255
class RotateEvent(BinLogEvent):
272256
"""
273-
<<<<<<< HEAD
274257
Change MySQL bin log file
275258
Represents information for the slave to know the name of the binary log it is going to receive.
276-
=======
277-
The event changes MySQL bin log file.
278-
It represents information for the slave to know the name of the binary log it is going to receive.
279-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
280259
281-
For more information: `[see details] <https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Rotate__event.html>`_.
260+
For more information: `[see RotateEvent] <https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Rotate__event.html>`_.
282261
283262
In detail, the class creates the following python objects in the constructor:
284263
@@ -309,19 +288,12 @@ class XAPrepareEvent(BinLogEvent):
309288
An XA prepare event is generated for a XA prepared transaction.
310289
Like Xid_event, it contains XID of the **prepared** transaction.
311290
312-
For more information: `[see details] <https://dev.mysql.com/doc/refman/8.0/en/xa-statements.html>`_.
313-
314-
<<<<<<< HEAD
315-
:ivar one_phase: current XA transaction commit method
316-
:ivar xid_format_id: a number that identifies the format used by the gtrid and bqual values
317-
:ivar xid: serialized XID representation of XA transaction (xid_gtrid + xid_bqual)
318-
=======
319-
In detail, the class creates the following python objects in the constructor:
291+
For more information: `[see XAPrepareEvent] <https://dev.mysql.com/doc/refman/8.0/en/xa-statements.html>`_.
320292
321293
:ivar one_phase: str - current XA transaction commit method
322294
:ivar xid_format_id: int - a number that identifies the format used by the gtrid and bqual values
323295
:ivar xid: str - serialized XID representation of XA transaction (xid_gtrid + xid_bqual)
324-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
296+
325297
"""
326298
def __init__(self,
327299
from_packet: Type[MysqlPacket],
@@ -354,35 +326,14 @@ def _dump(self) -> None:
354326

355327
class FormatDescriptionEvent(BinLogEvent):
356328
"""
357-
<<<<<<< HEAD
358-
Represents a Format Description Event in the MySQL binary log.
329+
The event Represents a Format Description Event in the MySQL binary log.
359330
360331
This event is written at the start of a binary log file for binlog version 4.
361332
It provides the necessary information to decode subsequent events in the file.
362333
363334
:ivar binlog_version: int - Version of the binary log format.
364335
:ivar mysql_version_str: str - Server's MySQL version in string format.
365336
"""
366-
367-
def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs):
368-
super().__init__(from_packet, event_size, table_map,
369-
ctl_connection, **kwargs)
370-
self.binlog_version = struct.unpack('<H', self.packet.read(2))
371-
self.mysql_version_str = self.packet.read(50).rstrip(b'\0').decode()
372-
numbers = self.mysql_version_str.split('-')[0]
373-
self.mysql_version = tuple(map(int, numbers.split('.')))
374-
=======
375-
The event represents a Format Description Event in the MySQL binary log.
376-
377-
This event is written at the start of a binary log file for binlog version 4.
378-
It provides the necessary information to decode subsequent events in the file.
379-
380-
In detail, the class creates the following python objects in the constructor:
381-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
382-
383-
:ivar binlog_version: str - Version of the binary log format
384-
:ivar mysql_version_str: str - Server's MySQL version in string format
385-
"""
386337
def __init__(self,
387338
from_packet: Type[MysqlPacket],
388339
event_size: int,
@@ -407,24 +358,13 @@ class StopEvent(BinLogEvent):
407358

408359

409360
class XidEvent(BinLogEvent):
410-
<<<<<<< HEAD
411-
"""
412-
A COMMIT event generated when COMMIT of a transaction that modifies one or more tables of an XA-capable storage engine occurs.
413-
414-
For more information: `[see details] <https://mariadb.com/kb/en/xid_event/>`_.
415-
416-
:ivar xid: uint - Transaction ID for 2 Phase Commit.
417-
=======
418-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
419361
"""
420-
A COMMIT event is generated when COMMIT of a transaction that modifies
362+
A COMMIT event generated when COMMIT of a transaction that modifies
421363
one or more tables of an XA-capable storage engine occurs.
422364
423-
For more information : `[see details] <https://mariadb.com/kb/en/xid_event/>`_.
365+
For more information: `[see XidEvent] <https://mariadb.com/kb/en/xid_event/>`_.
424366
425-
In detail, the class creates the following python objects in the constructor:
426-
427-
:ivar xid: int - Transaction ID for 2 Phase Commit
367+
:ivar xid: uint - Transaction ID for 2 Phase Commit.
428368
"""
429369
def __init__(self,
430370
from_packet: Type[MysqlPacket],
@@ -457,19 +397,11 @@ class HeartbeatLogEvent(BinLogEvent):
457397
(see Binlog_sender::send_events in sql/rpl_binlog_sender.cc)
458398
459399
Warning:
460-
<<<<<<< HEAD
461400
That makes 106 bytes of data for skipped event in the binlog.
462401
*this is also the case with GTID replication*.
463402
To mitigate such behavior, you are expected to keep the binlog small
464403
(see max_binlog_size, defaults to 1G).
465404
In any case, the timestamp is 0 (as in 1970-01-01T00:00:00).
466-
=======
467-
That makes 106 bytes of data for skipped event in the binlog.
468-
*this is also the case with GTID replication*.
469-
To mitigate such behavior, you are expected to keep the binlog small
470-
(see max_binlog_size, defaults to 1G).
471-
In any case, the timestamp is 0 (as in 1970-01-01T00:00:00).
472-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
473405
474406
In detail, the class creates the following python objects in the constructor:
475407
@@ -532,15 +464,10 @@ def __init__(self,
532464
self.schema: str = self.packet.read(self.schema_length)
533465
self.packet.advance(1)
534466

535-
<<<<<<< HEAD
467+
#string[EOF] query
536468
query = self.packet.read(event_size - 13 - self.status_vars_length
537469
- self.schema_length - 1)
538-
self.query = query.decode("utf-8", errors='backslashreplace')
539-
=======
540-
self.query: str = self.packet.read(event_size - 13 - self.status_vars_length
541-
- self.schema_length - 1).decode("utf-8")
542-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
543-
#string[EOF] query
470+
self.query:str = query.decode("utf-8", errors='backslashreplace')
544471

545472
def _dump(self) -> None:
546473
"""Dump the python objects for the event."""
@@ -605,11 +532,7 @@ def _read_status_vars_value_for_key(self, key: int) -> Union[None, NoReturn]:
605532
elif key == Q_TIME_ZONE_CODE: # 0x05
606533
time_zone_len = self.packet.read_uint8()
607534
if time_zone_len:
608-
<<<<<<< HEAD
609-
self.time_zone = self.packet.read(time_zone_len)
610-
=======
611535
self.time_zone: str = self.packet.read(time_zone_len)
612-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
613536
elif key == Q_CATALOG_NZ_CODE: # 0x06
614537
catalog_len = self.packet.read_uint8()
615538
if catalog_len:
@@ -756,19 +679,11 @@ def _dump(self) -> None:
756679

757680
class IntvarEvent(BinLogEvent):
758681
"""
759-
<<<<<<< HEAD
760682
Stores the value of auto-increment variables.
761683
This event will be created just before a QueryEvent.
762684
763685
:ivar type: int - 1 byte identifying the type of variable stored.
764-
=======
765-
The event stores the value of auto-increment variables.
766-
It will be created just before a QueryEvent.
767-
768-
In detail, the class creates the following python objects in the constructor:
769-
770-
:ivar type: int - 1 byte identifying the type of variable stored. \
771-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
686+
772687
Can be either LAST_INSERT_ID_EVENT (1) or INSERT_ID_EVENT (2).
773688
:ivar value: int - The value of the variable
774689
"""
@@ -804,11 +719,7 @@ class RandEvent(BinLogEvent):
804719
:ivar seed1: int - value for the first seed
805720
:ivar seed2: int - value for the second seed
806721
"""
807-
<<<<<<< HEAD
808-
def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs):
809-
super().__init__(from_packet, event_size, table_map,
810-
ctl_connection, **kwargs)
811-
=======
722+
812723
def __init__(self,
813724
from_packet: Type[MysqlPacket],
814725
event_size: int,
@@ -817,7 +728,6 @@ def __init__(self,
817728
**kwargs: str):
818729
super().__init__(from_packet, event_size, table_map, ctl_connection, **kwargs)
819730

820-
>>>>>>> 07b561d (add type hints to the rest of the events, improve docstring readability in some events)
821731
# Payload
822732
self._seed1: int = self.packet.read_uint64()
823733
self._seed2: int = self.packet.read_uint64()

0 commit comments

Comments
 (0)