| 838 | |
| 839 | |
| 840 | class ApplySchemaTransformer(ApplySchemaBaseTransformer): |
| 841 | def __init__( |
| 842 | self, |
| 843 | module_name: str, |
| 844 | class_name: str, |
| 845 | properties: dict[str, (PythonType | GithubClass | None, bool)], |
| 846 | completable: bool, |
| 847 | deprecate: bool, |
| 848 | ): |
| 849 | super().__init__(module_name, class_name, properties, deprecate) |
| 850 | self.completable = completable |
| 851 | |
| 852 | @staticmethod |
| 853 | def deprecate_function(node: cst.FunctionDef) -> cst.FunctionDef: |
| 854 | decorators = list(node.decorators) |
| 855 | decorators.append(cst.Decorator(decorator=cst.Name(value="deprecated"))) |
| 856 | return node.with_changes(decorators=decorators) |
| 857 | |
| 858 | def inner_github_type(self, data_type: PythonType | GithubClass | list[PythonType | GithubClass]) -> [GithubClass]: |
| 859 | if data_type is None: |
| 860 | return [] |
| 861 | if isinstance(data_type, list): |
| 862 | return [ght for dt in data_type for ght in self.inner_github_type(dt)] |
| 863 | if isinstance(data_type, PythonType): |
| 864 | return self.inner_github_type(data_type.inner_types) |
| 865 | if isinstance(data_type, GithubClass): |
| 866 | return [data_type] |
| 867 | raise ValueError("Unsupported data type", data_type) |
| 868 | |
| 869 | def leave_Module(self, original_node: Module, updated_node: Module) -> Module: |
| 870 | i = 0 |
| 871 | node = updated_node |
| 872 | |
| 873 | # add from __future__ import annotations if not the first import |
| 874 | node = self.add_future_import(node) |
| 875 | |
| 876 | property_classes = { |
| 877 | ghc |
| 878 | for p in self.all_properties |
| 879 | for ghc in self.inner_github_type(p.data_type) |
| 880 | if ghc.module != self.module_name and ghc.name != self.class_name |
| 881 | } |
| 882 | import_classes = sorted(property_classes, key=lambda c: c.module) |
| 883 | typing_classes = sorted(property_classes, key=lambda c: c.module) |
| 884 | # TODO: do not import this file itself |
| 885 | datetime_exists = False |
| 886 | in_github_imports = False |
| 887 | needs_datetime_import = any( |
| 888 | p.data_type.type == "datetime" for p in self.all_properties if isinstance(p.data_type, PythonType) |
| 889 | ) |
| 890 | |
| 891 | # insert import classes if needed |
| 892 | while ( |
| 893 | i < len(node.body) |
| 894 | and isinstance(node.body[i], cst.SimpleStatementLine) |
| 895 | and isinstance(node.body[i].body[0], (cst.Import, cst.ImportFrom)) |
| 896 | ): |
| 897 | if self.is_datetime_import(node.body[i].body[0]): |
no outgoing calls
no test coverage detected
searching dependent graphs…