3434LAYER_RE = re .compile (r"//\sLAYER\s(\d+)" )
3535COMBINATOR_RE = re .compile (r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);$" , re .MULTILINE )
3636ARGS_RE = re .compile (r"[^{](\w+):([\w?!.<>#]+)" )
37- FLAGS_RE = re .compile (r"flags\.(\d+)\?" )
38- FLAGS_RE_2 = re .compile (r"flags\.(\d+)\?([\w<>.]+)" )
39- FLAGS_RE_3 = re .compile (r"flags:#" )
37+ FLAGS_RE = re .compile (r"flags(\d?) \.(\d+)\?" )
38+ FLAGS_RE_2 = re .compile (r"flags(\d?) \.(\d+)\?([\w<>.]+)" )
39+ FLAGS_RE_3 = re .compile (r"flags(\d?) :#" )
4040INT_RE = re .compile (r"int(\d+)" )
4141
4242CORE_TYPES = ["int" , "long" , "int128" , "int256" , "double" , "bytes" , "string" , "Bool" , "true" ]
@@ -115,7 +115,7 @@ def get_type_hint(type: str) -> str:
115115 type = f"List[{ get_type_hint (sub_type )} ]"
116116
117117 if is_core :
118- return f"Union[None, { type } ] = None" if is_flag else type
118+ return f"Optional[ { type } ] = None" if is_flag else type
119119 else :
120120 ns , name = type .split ("." ) if "." in type else ("" , type )
121121 type = f'"raw.base.' + "." .join ([ns , name ]).strip ("." ) + '"'
@@ -131,10 +131,9 @@ def sort_args(args):
131131 for i in flags :
132132 args .remove (i )
133133
134- try :
135- args .remove (("flags" , "#" ))
136- except ValueError :
137- pass
134+ for i in args [:]:
135+ if re .match (r"flags\d?" , i [0 ]) and i [1 ] == "#" :
136+ args .remove (i )
138137
139138 return args + flags
140139
@@ -362,14 +361,12 @@ def start(format: bool = False):
362361
363362 for i , arg in enumerate (sorted_args ):
364363 arg_name , arg_type = arg
365- is_optional = FLAGS_RE .match (arg_type )
366- flag_number = is_optional .group (1 ) if is_optional else - 1
367364 arg_type = arg_type .split ("?" )[- 1 ]
368365
369366 docstring_args .append (
370367 "{}{}: {}" .format (
371368 arg_name ,
372- " (optional)" . format ( flag_number ) if is_optional else "" ,
369+ " (optional)" ,
373370 get_docstring_arg_type (arg_type , is_pyrogram_type = c .namespace == "pyrogram" )
374371 )
375372 )
@@ -401,42 +398,46 @@ def start(format: bool = False):
401398 for arg_name , arg_type in c .args :
402399 flag = FLAGS_RE_2 .match (arg_type )
403400
404- if arg_name == "flags" and arg_type == "#" :
401+ if re . match ( r "flags\d?" , arg_name ) and arg_type == "#" :
405402 write_flags = []
406403
407404 for i in c .args :
408405 flag = FLAGS_RE_2 .match (i [1 ])
409406
410407 if flag :
411- if flag .group (2 ) == "true" or flag .group (2 ).startswith ("Vector" ):
412- write_flags .append (f"flags |= (1 << { flag .group (1 )} ) if self.{ i [0 ]} else 0" )
408+ if arg_name != f"flags{ flag .group (1 )} " :
409+ continue
410+
411+ if flag .group (3 ) == "true" or flag .group (3 ).startswith ("Vector" ):
412+ write_flags .append (f"{ arg_name } |= (1 << { flag .group (2 )} ) if self.{ i [0 ]} else 0" )
413413 else :
414- write_flags .append (f"flags |= (1 << { flag .group (1 )} ) if self.{ i [0 ]} is not None else 0" )
414+ write_flags .append (
415+ f"{ arg_name } |= (1 << { flag .group (2 )} ) if self.{ i [0 ]} is not None else 0" )
415416
416417 write_flags = "\n " .join ([
417- "flags = 0" ,
418+ f" { arg_name } = 0" ,
418419 "\n " .join (write_flags ),
419- "b.write(Int(flags ))\n "
420+ f "b.write(Int({ arg_name } ))\n "
420421 ])
421422
422423 write_types += write_flags
423- read_types += "flags = Int.read(b)\n "
424+ read_types += f" \n { arg_name } = Int.read(b)\n "
424425
425426 continue
426427
427428 if flag :
428- index , flag_type = flag .groups ()
429+ number , index , flag_type = flag .groups ()
429430
430431 if flag_type == "true" :
431432 read_types += "\n "
432- read_types += f"{ arg_name } = True if flags & (1 << { index } ) else False"
433+ read_types += f"{ arg_name } = True if flags{ number } & (1 << { index } ) else False"
433434 elif flag_type in CORE_TYPES :
434435 write_types += "\n "
435436 write_types += f"if self.{ arg_name } is not None:\n "
436437 write_types += f"b.write({ flag_type .title ()} (self.{ arg_name } ))\n "
437438
438439 read_types += "\n "
439- read_types += f"{ arg_name } = { flag_type .title ()} .read(b) if flags & (1 << { index } ) else None"
440+ read_types += f"{ arg_name } = { flag_type .title ()} .read(b) if flags{ number } & (1 << { index } ) else None"
440441 elif "vector" in flag_type .lower ():
441442 sub_type = arg_type .split ("<" )[1 ][:- 1 ]
442443
@@ -447,16 +448,16 @@ def start(format: bool = False):
447448 )
448449
449450 read_types += "\n "
450- read_types += "{} = TLObject.read(b{}) if flags & (1 << {}) else []\n " .format (
451- arg_name , f", { sub_type .title ()} " if sub_type in CORE_TYPES else "" , index
451+ read_types += "{} = TLObject.read(b{}) if flags{} & (1 << {}) else []\n " .format (
452+ arg_name , f", { sub_type .title ()} " if sub_type in CORE_TYPES else "" , number , index
452453 )
453454 else :
454455 write_types += "\n "
455456 write_types += f"if self.{ arg_name } is not None:\n "
456457 write_types += f"b.write(self.{ arg_name } .write())\n "
457458
458459 read_types += "\n "
459- read_types += f"{ arg_name } = TLObject.read(b) if flags & (1 << { index } ) else None\n "
460+ read_types += f"{ arg_name } = TLObject.read(b) if flags{ number } & (1 << { index } ) else None\n "
460461 else :
461462 if arg_type in CORE_TYPES :
462463 write_types += "\n "
0 commit comments