Last active
January 5, 2022 08:59
-
-
Save Bibo-Joshi/399382cda537fb01bd86b13c3d03a956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from collections import namedtuple | |
from typing import List | |
Arg = namedtuple('Arg', ['name', 'type_var', 'default']) | |
args = [ | |
Arg('context', 'CCT', 'CallbackContext'), | |
# Order matters! | |
Arg('user_data', 'UD', 'Dict'), | |
Arg('chat_data', 'CD', 'Dict'), | |
Arg('bot_data', 'BD', 'Dict'), | |
] | |
def annotation(arg: Arg, names: List[str]) -> str: | |
if arg.name != 'context': | |
return arg.type_var if arg.name in names else arg.default | |
else: | |
if arg.name in names: | |
return arg.type_var | |
base = arg.default | |
user_data = 'UD' if 'user_data' in names else 'Dict' | |
chat_data = 'CD' if 'chat_data' in names else 'Dict' | |
bot_data = 'BD' if 'bot_data' in names else 'Dict' | |
return f'{base}[{user_data}, {chat_data}, {bot_data}]' | |
present_args = [] | |
for i in range(len(args) + 1): | |
present_args.extend(list(itertools.combinations(args, i))) | |
outs = [] | |
for combination in present_args: | |
present_names = [a.name for a in combination] | |
self_annotation = ', '.join([annotation(arg, present_names) for arg in args]) | |
self_annotation = f"self: 'ContextTypes[{self_annotation}]'" | |
other_annotations = [ | |
f'{arg.name}: Type[{arg.type_var}]' for arg in args if arg.name in present_names | |
] | |
complete_annotations = f'{self_annotation}, {", ".join(other_annotations)}' | |
outs.append(f'@overload\ndef __init__({complete_annotations}): ...') | |
with open('output.txt', 'w') as file: | |
file.write('\n'.join(outs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment