From 6978c74ba5139e836f2f5e506b93d490eb02f57c Mon Sep 17 00:00:00 2001 From: Johannes Roos Date: Fri, 23 Aug 2024 15:32:44 +0200 Subject: [PATCH] blacked --- turms/helpers.py | 8 +-- turms/parsers/base.py | 3 +- turms/plugins/base.py | 7 ++- turms/plugins/funcs.py | 98 ++++++++++++++++++++----------------- turms/plugins/inputs.py | 10 ++-- turms/plugins/objects.py | 25 +++++++--- turms/plugins/operations.py | 16 +++--- turms/plugins/strawberry.py | 15 +++--- turms/processors/base.py | 3 +- turms/recurse.py | 30 +++++++----- turms/referencer.py | 13 ++--- turms/utils.py | 8 ++- 12 files changed, 130 insertions(+), 106 deletions(-) diff --git a/turms/helpers.py b/turms/helpers.py index d977b41..c983fa0 100644 --- a/turms/helpers.py +++ b/turms/helpers.py @@ -74,11 +74,11 @@ def load_introspection_from_url( raise GenerationError( f"Failed to fetch schema from {url} Graphql error: {x['errors']}" ) - - if "data" not in x: - raise GenerationError(f"Failed to fetch schema from {url}. Did not receive data attripute: {x}") - + if "data" not in x: + raise GenerationError( + f"Failed to fetch schema from {url}. Did not receive data attripute: {x}" + ) return x["data"] diff --git a/turms/parsers/base.py b/turms/parsers/base.py index 4a054a5..c904dc9 100644 --- a/turms/parsers/base.py +++ b/turms/parsers/base.py @@ -21,5 +21,4 @@ class Parser(BaseModel): def parse_ast( self, asts: List[ast.AST], - ) -> List[ast.AST]: - ... # pragma: no cover + ) -> List[ast.AST]: ... # pragma: no cover diff --git a/turms/plugins/base.py b/turms/plugins/base.py index dc5a80d..1936c01 100644 --- a/turms/plugins/base.py +++ b/turms/plugins/base.py @@ -6,6 +6,7 @@ from graphql.utilities.build_client_schema import GraphQLSchema from turms.config import LogFunction + class PluginConfig(BaseSettings): type: str @@ -30,10 +31,8 @@ def generate_ast( config, client_schema: GraphQLSchema, registry, - ) -> List[ast.AST]: - ... # pragma: no cover - + ) -> List[ast.AST]: ... # pragma: no cover class Config: extra = "forbid" - arbitrary_types_allowed = True \ No newline at end of file + arbitrary_types_allowed = True diff --git a/turms/plugins/funcs.py b/turms/plugins/funcs.py index bf50514..0eadd97 100644 --- a/turms/plugins/funcs.py +++ b/turms/plugins/funcs.py @@ -785,32 +785,36 @@ def generate_operation_func( ), body=[ doc, - genereate_async_call( - definition, - o, - client_schema, - config, - plugin_config, - registry, - collapse, - ) - if definition.type != OperationType.SUBSCRIPTION - else genereate_async_iterator( - definition, - o, - client_schema, - config, - plugin_config, - registry, - collapse, + ( + genereate_async_call( + definition, + o, + client_schema, + config, + plugin_config, + registry, + collapse, + ) + if definition.type != OperationType.SUBSCRIPTION + else genereate_async_iterator( + definition, + o, + client_schema, + config, + plugin_config, + registry, + collapse, + ) ), ], decorator_list=[], - returns=return_type - if definition.type != OperationType.SUBSCRIPTION - else ast.Subscript( - value=ast.Name(id="AsyncIterator", ctx=ast.Load()), - slice=return_type, + returns=( + return_type + if definition.type != OperationType.SUBSCRIPTION + else ast.Subscript( + value=ast.Name(id="AsyncIterator", ctx=ast.Load()), + slice=return_type, + ) ), ) ) @@ -831,31 +835,35 @@ def generate_operation_func( ), body=[ doc, - genereate_sync_call( - definition, - o, - client_schema, - config, - plugin_config, - registry, - collapse, - ) - if definition.type != OperationType.SUBSCRIPTION - else genereate_sync_iterator( - definition, - o, - client_schema, - config, - plugin_config, - registry, - collapse, + ( + genereate_sync_call( + definition, + o, + client_schema, + config, + plugin_config, + registry, + collapse, + ) + if definition.type != OperationType.SUBSCRIPTION + else genereate_sync_iterator( + definition, + o, + client_schema, + config, + plugin_config, + registry, + collapse, + ) ), ], decorator_list=[], - returns=return_type - if definition.type != OperationType.SUBSCRIPTION - else ast.Subscript( - value=ast.Name(id="Iterator", ctx=ast.Load()), slice=return_type + returns=( + return_type + if definition.type != OperationType.SUBSCRIPTION + else ast.Subscript( + value=ast.Name(id="Iterator", ctx=ast.Load()), slice=return_type + ) ), ) ) diff --git a/turms/plugins/inputs.py b/turms/plugins/inputs.py index 097dd19..cd507b3 100644 --- a/turms/plugins/inputs.py +++ b/turms/plugins/inputs.py @@ -176,9 +176,7 @@ def generate_inputs( if field_name != value_key: registry.register_import("pydantic.Field") keywords = [ - ast.keyword( - arg="alias", value=ast.Constant(value=value_key) - ) + ast.keyword(arg="alias", value=ast.Constant(value=value_key)) ] if not isinstance(value.type, GraphQLNonNull): keywords.append( @@ -215,7 +213,11 @@ def generate_inputs( is_optional=True, ), simple=1, - value=ast.Constant(None) if not isinstance(value.type, GraphQLNonNull) else None, + value=( + ast.Constant(None) + if not isinstance(value.type, GraphQLNonNull) + else None + ), ) potential_comment = ( diff --git a/turms/plugins/objects.py b/turms/plugins/objects.py index 49a56aa..11ef571 100644 --- a/turms/plugins/objects.py +++ b/turms/plugins/objects.py @@ -157,13 +157,22 @@ def generate_object_field_annotation( and config.freeze.convert_list_to_tuple ): registry.register_import("typing.Tuple") + def list_builder(x): - return ast.Subscript(value=ast.Name("Tuple", ctx=ast.Load()), slice=ast.Tuple(elts=[x, ast.Ellipsis()], ctx=ast.Load()), ctx=ast.Load()) + return ast.Subscript( + value=ast.Name("Tuple", ctx=ast.Load()), + slice=ast.Tuple(elts=[x, ast.Ellipsis()], ctx=ast.Load()), + ctx=ast.Load(), + ) + else: registry.register_import("typing.List") + def list_builder(x): - return ast.Subscript(value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load()) + return ast.Subscript( + value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load() + ) if is_optional: registry.register_import("typing.Optional") @@ -223,12 +232,12 @@ def generate_types( ) } - interface_map: Dict[ - str, List[str] - ] = {} # A list of interfaces with the union classes attached - interface_base_map: Dict[ - str, str - ] = {} # A list of interfaces with its respective base + interface_map: Dict[str, List[str]] = ( + {} + ) # A list of interfaces with the union classes attached + interface_base_map: Dict[str, str] = ( + {} + ) # A list of interfaces with its respective base for base in plugin_config.types_bases: registry.register_import(base) diff --git a/turms/plugins/operations.py b/turms/plugins/operations.py index 5a7915a..0468c07 100644 --- a/turms/plugins/operations.py +++ b/turms/plugins/operations.py @@ -253,9 +253,11 @@ def generate_operation( ast.keyword( arg="default", value=ast.Constant( - value=parse_value_node(v.default_value) - if v.default_value is not None - else None + value=( + parse_value_node(v.default_value) + if v.default_value is not None + else None + ) ), ), ], @@ -289,9 +291,11 @@ def generate_operation( ast.keyword( arg="default", value=ast.Constant( - value=parse_value_node(v.default_value) - if v.default_value is not None - else None + value=( + parse_value_node(v.default_value) + if v.default_value is not None + else None + ) ), ), ], diff --git a/turms/plugins/strawberry.py b/turms/plugins/strawberry.py index 8750e97..84c7dc7 100644 --- a/turms/plugins/strawberry.py +++ b/turms/plugins/strawberry.py @@ -39,8 +39,7 @@ def __call__( config: GeneratorConfig, plugin_config: "StrawberryPluginConfig", registry: ClassRegistry, - ) -> List[ast.AST]: - ... # pragma: no cover + ) -> List[ast.AST]: ... # pragma: no cover def default_generate_directives( @@ -708,12 +707,12 @@ def generate_types( } registry.register_import("strawberry") - interface_map: Dict[ - str, List[str] - ] = {} # A list of interfaces with the union classes attached - interface_base_map: Dict[ - str, str - ] = {} # A list of interfaces with its respective base + interface_map: Dict[str, List[str]] = ( + {} + ) # A list of interfaces with the union classes attached + interface_base_map: Dict[str, str] = ( + {} + ) # A list of interfaces with its respective base for key, object_type in sorted_objects.items(): additional_bases = get_additional_bases_for_type( diff --git a/turms/processors/base.py b/turms/processors/base.py index d4b190b..dcaadf7 100644 --- a/turms/processors/base.py +++ b/turms/processors/base.py @@ -22,5 +22,4 @@ class Processor(BaseModel): config: ProcessorConfig @abstractmethod - def run(gen_file: str, config: GeneratorConfig): - ... # pragma: no cover + def run(gen_file: str, config: GeneratorConfig): ... # pragma: no cover diff --git a/turms/recurse.py b/turms/recurse.py index 9b6869a..0f4ffc3 100644 --- a/turms/recurse.py +++ b/turms/recurse.py @@ -118,10 +118,11 @@ class X(BaseModel): ) cls = ast.ClassDef( inline_fragment_name, - bases=additional_bases + [ - ast.Name(id=base.split(".")[-1], ctx=ast.Load()) - for base in config.object_bases - ], + bases=additional_bases + + [ + ast.Name(id=base.split(".")[-1], ctx=ast.Load()) + for base in config.object_bases + ], decorator_list=[], keywords=[], body=inline_fragment_fields @@ -166,9 +167,6 @@ class X(BaseModel): else: return ast.Name(id=union_class_names[0], ctx=ast.Load()) - - - if isinstance(type, GraphQLInterfaceType): # Lets Create Base Class to Inherit from for this mother_class_fields = [] @@ -294,9 +292,9 @@ class X(BaseModel): ) elif isinstance(sub_sub_node, FragmentSpreadNode): - additional_bases.append(registry.reference_fragment( - sub_sub_node.name.value, parent - )) + additional_bases.append( + registry.reference_fragment(sub_sub_node.name.value, parent) + ) cls = ast.ClassDef( inline_fragment_name, @@ -490,14 +488,22 @@ class X(BaseModel): if config.freeze.enabled: registry.register_import("typing.Tuple") + def list_builder(x): - return ast.Subscript(value=ast.Name("Tuple", ctx=ast.Load()), slice=ast.Tuple(elts=[x, ast.Ellipsis()], ctx=ast.Load()), ctx=ast.Load()) + return ast.Subscript( + value=ast.Name("Tuple", ctx=ast.Load()), + slice=ast.Tuple(elts=[x, ast.Ellipsis()], ctx=ast.Load()), + ctx=ast.Load(), + ) else: registry.register_import("typing.List") + def list_builder(x): - return ast.Subscript(value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load()) + return ast.Subscript( + value=ast.Name("List", ctx=ast.Load()), slice=x, ctx=ast.Load() + ) if is_optional: registry.register_import("typing.Optional") diff --git a/turms/referencer.py b/turms/referencer.py index a24d3f3..e605465 100644 --- a/turms/referencer.py +++ b/turms/referencer.py @@ -54,8 +54,6 @@ def is_input_registered(self, type_name: str): return type_name in self.inputs - - def recurse_find_references( node: FieldNode, graphql_type: GraphQLType, @@ -128,12 +126,10 @@ def recurse_find_references( raise Exception("Unknown Type", type(graphql_type), graphql_type) - def break_recursion_loop(*args, **kwargs): return recurse_type_annotation(*args, **kwargs) - def recurse_type_annotation( field: GraphQLInputField, graphql_type: NamedTypeNode, @@ -141,7 +137,7 @@ def recurse_type_annotation( registry: ReferenceRegistry, optional=True, ): - + if isinstance(graphql_type, NonNullTypeNode): return recurse_type_annotation( field, graphql_type.type, schema, registry, optional=False @@ -154,17 +150,16 @@ def recurse_type_annotation( type = schema.get_type(graphql_type.name.value) assert type, graphql_type return recurse_type_annotation(field, type, schema, registry, optional=False) - + elif isinstance(graphql_type, GraphQLNonNull): return recurse_type_annotation( field, graphql_type.of_type, schema, registry, optional=False ) - + elif isinstance(graphql_type, GraphQLList): return recurse_type_annotation( field, graphql_type.of_type, schema, registry, optional=False ) - elif isinstance(graphql_type, GraphQLScalarType): registry.register_scalar(graphql_type.name) @@ -176,7 +171,7 @@ def recurse_type_annotation( # Only and only is we have not registered this input object type yet # we need to register it (otherwise we might get into a recursion loop) if not registry.is_input_registered(graphql_type.name): - + registry.register_input(graphql_type.name) # We need to get all input objects that this graphql input object type references diff --git a/turms/utils.py b/turms/utils.py index c1aed7e..8f46663 100644 --- a/turms/utils.py +++ b/turms/utils.py @@ -397,7 +397,9 @@ def recurse_type_annotation( type.name.value, "", allow_forward=False ) except NoEnumFound: - raise NotImplementedError(f"Could not find corresponding type for type '{type.name.value}'. Did you register this scalar?") + raise NotImplementedError( + f"Could not find corresponding type for type '{type.name.value}'. Did you register this scalar?" + ) if not x: raise Exception(f"Could not set value for {type}") @@ -584,7 +586,9 @@ def recurse_type_label( type.name.value, "", allow_forward=False ) except NoEnumFound: - raise NotImplementedError(f"Could not find correspoinding type labler for {type.name.value}") + raise NotImplementedError( + f"Could not find correspoinding type labler for {type.name.value}" + ) if optional: return "Optional[" + x.id + "]"