mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
Update code generator to emit type hints on init methods
This commit is contained in:
parent
3a1496c205
commit
fce5cfea0e
|
@ -89,6 +89,9 @@ def _write_modules(
|
||||||
# Import struct for the .__bytes__(self) serialization
|
# Import struct for the .__bytes__(self) serialization
|
||||||
builder.writeln('import struct')
|
builder.writeln('import struct')
|
||||||
|
|
||||||
|
# Import datetime for type hinting
|
||||||
|
builder.writeln('from datetime import datetime')
|
||||||
|
|
||||||
tlobjects.sort(key=lambda x: x.name)
|
tlobjects.sort(key=lambda x: x.name)
|
||||||
|
|
||||||
type_names = set()
|
type_names = set()
|
||||||
|
@ -194,24 +197,17 @@ def _write_class_init(tlobject, kind, type_constructors, builder):
|
||||||
builder.writeln()
|
builder.writeln()
|
||||||
|
|
||||||
# Convert the args to string parameters, flags having =None
|
# Convert the args to string parameters, flags having =None
|
||||||
args = [(a.name if not a.is_flag and not a.can_be_inferred
|
args = ['{}: {}{}'.format(
|
||||||
else '{}=None'.format(a.name)) for a in tlobject.real_args]
|
a.name, a.type_hint(), '=None' if a.is_flag or a.can_be_inferred else '')
|
||||||
|
for a in tlobject.real_args
|
||||||
|
]
|
||||||
|
|
||||||
# Write the __init__ function if it has any argument
|
# Write the __init__ function if it has any argument
|
||||||
if not tlobject.real_args:
|
if not tlobject.real_args:
|
||||||
return
|
return
|
||||||
|
|
||||||
builder.writeln('def __init__({}):', ', '.join(['self'] + args))
|
builder.writeln("def __init__({}):", ', '.join(['self'] + args))
|
||||||
# Write the docstring, to know the type of the args
|
|
||||||
builder.writeln('"""')
|
builder.writeln('"""')
|
||||||
for arg in tlobject.real_args:
|
|
||||||
if not arg.flag_indicator:
|
|
||||||
builder.writeln(':param {} {}:', arg.type_hint(), arg.name)
|
|
||||||
builder.current_indent -= 1 # It will auto-indent (':')
|
|
||||||
|
|
||||||
# We also want to know what type this request returns
|
|
||||||
# or to which type this constructor belongs to
|
|
||||||
builder.writeln()
|
|
||||||
if tlobject.is_function:
|
if tlobject.is_function:
|
||||||
builder.write(':returns {}: ', tlobject.result)
|
builder.write(':returns {}: ', tlobject.result)
|
||||||
else:
|
else:
|
||||||
|
@ -232,8 +228,7 @@ def _write_class_init(tlobject, kind, type_constructors, builder):
|
||||||
# Set the arguments
|
# Set the arguments
|
||||||
for arg in tlobject.real_args:
|
for arg in tlobject.real_args:
|
||||||
if not arg.can_be_inferred:
|
if not arg.can_be_inferred:
|
||||||
builder.writeln('self.{0} = {0} # type: {1}',
|
builder.writeln('self.{0} = {0}', arg.name)
|
||||||
arg.name, arg.type_hint())
|
|
||||||
|
|
||||||
# Currently the only argument that can be
|
# Currently the only argument that can be
|
||||||
# inferred are those called 'random_id'
|
# inferred are those called 'random_id'
|
||||||
|
|
|
@ -97,7 +97,7 @@ def parse_tl(file_path, layer, methods=None, ignored_ids=CORE_TYPES):
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
match = re.match('---(\w+)---', line)
|
match = re.match(r'---(\w+)---', line)
|
||||||
if match:
|
if match:
|
||||||
following_types = match.group(1)
|
following_types = match.group(1)
|
||||||
is_function = following_types == 'functions'
|
is_function = following_types == 'functions'
|
||||||
|
|
|
@ -174,7 +174,7 @@ class TLArg:
|
||||||
'date': 'Optional[datetime]', # None date = 0 timestamp
|
'date': 'Optional[datetime]', # None date = 0 timestamp
|
||||||
'bytes': 'bytes',
|
'bytes': 'bytes',
|
||||||
'true': 'bool',
|
'true': 'bool',
|
||||||
}.get(cls, "Type{}".format(cls))
|
}.get(cls, "'Type{}'".format(cls))
|
||||||
if self.is_vector:
|
if self.is_vector:
|
||||||
result = 'List[{}]'.format(result)
|
result = 'List[{}]'.format(result)
|
||||||
if self.is_flag and cls != 'date':
|
if self.is_flag and cls != 'date':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user