Make type hinting on the generated code more IDE-friendly

This commit is contained in:
Lonami Exo
2017-09-18 21:00:06 +02:00
parent 2595d45bd7
commit f7e4f3f678
2 changed files with 41 additions and 37 deletions

View File

@@ -96,6 +96,17 @@ class TLObject:
result=match.group(3),
is_function=is_function)
def class_name(self):
"""Gets the class name following the Python style guidelines"""
# Courtesy of http://stackoverflow.com/a/31531797/4759433
result = re.sub(r'_([a-z])', lambda m: m.group(1).upper(), self.name)
result = result[:1].upper() + result[1:].replace('_', '')
# If it's a function, let it end with "Request" to identify them
if self.is_function:
result += 'Request'
return result
def sorted_args(self):
"""Returns the arguments properly sorted and ready to plug-in
into a Python's method header (i.e., flags and those which
@@ -197,8 +208,8 @@ class TLArg:
else:
self.flag_indicator = False
self.is_generic = arg_type.startswith('!')
self.type = arg_type.lstrip(
'!') # Strip the exclamation mark always to have only the name
# Strip the exclamation mark always to have only the name
self.type = arg_type.lstrip('!')
# The type may be a flag (flags.IDX?REAL_TYPE)
# Note that 'flags' is NOT the flags name; this is determined by a previous argument
@@ -233,6 +244,22 @@ class TLArg:
self.generic_definition = generic_definition
def type_hint(self):
result = {
'int': 'int',
'long': 'int',
'string': 'str',
'date': 'datetime',
'bytes': 'bytes',
'true': 'bool',
}.get(self.type, 'TLObject')
if self.is_vector:
result = 'list[{}]'.format(result)
if self.is_flag:
result += ' | None'
return result
def __str__(self):
# Find the real type representation by updating it as required
real_type = self.type