Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

black.parsing.InvalidInput: Cannot parse: 1041:8: def return(cls, id: str) -> ReturnFields: #319

Open
Anton-Shutik opened this issue Oct 3, 2024 · 3 comments

Comments

@Anton-Shutik
Copy link

Fails when trying to generate Sync client for Shopify GraphQL, but works fine with enable_custom_operations=false

env:

Python 3.9.13
ariadne-codegen==0.14.0
isort==5.13.2
black==24.8.0
[tool.ariadne-codegen]
remote_schema_url = "https://{}.myshopify.com/admin/api/2024-07/graphql.json"
remote_schema_headers = {"X-Shopify-Access-Token" = "$SHOPIFY_API_TOKEN"}
include_all_enums = true
base_client_file_path  = "/usr/local/lib/python3.9/site-packages/ariadne_codegen/client_generators/dependencies/base_client.py"
plugins = []
**enable_custom_operations=true**
convert_to_snake_case = true
opentelemetry_client = false
client_name = "ShopifyGQLClient"
include_comments = "stable"
base_client_name = "BaseClient"
target_package_name = "gql"
client_file_name = "client"
enums_module_name = "enums"
async_client = false
Traceback (most recent call last):
  File "/usr/local/bin/ariadne-codegen", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/ariadne_codegen/main.py", line 37, in main
    client(config_dict)
  File "/usr/local/lib/python3.9/site-packages/ariadne_codegen/main.py", line 81, in client
    generated_files = package_generator.generate()
  File "/usr/local/lib/python3.9/site-packages/ariadne_codegen/client_generators/package.py", line 161, in generate
    self._generate_custom_queries()
  File "/usr/local/lib/python3.9/site-packages/ariadne_codegen/client_generators/package.py", line 384, in _generate_custom_queries
    code = self._add_comments_to_code(ast_to_str(module, False))
  File "/usr/local/lib/python3.9/site-packages/ariadne_codegen/utils.py", line 33, in ast_to_str
    return format_str(isort.code(code), mode=Mode())
  File "/usr/local/lib/python3.9/site-packages/black/__init__.py", line 1067, in format_str
    dst_contents = _format_str_once(src_contents, mode=mode)
  File "/usr/local/lib/python3.9/site-packages/black/__init__.py", line 1077, in _format_str_once
    src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
  File "/usr/local/lib/python3.9/site-packages/black/parsing.py", line 126, in lib2to3_parse
    raise exc from None
black.parsing.InvalidInput: Cannot parse: 1041:8:     def return(cls, id: str) -> ReturnFields:
@Anton-Shutik
Copy link
Author

Seems like here is the problem, they have query named return

@phrfpeixoto
Copy link

OH MY! Same issue here. Is there a workaround?

@phrfpeixoto
Copy link

phrfpeixoto commented Oct 23, 2024

Here's what I did:

  • Downloaded the schema manually
  • Modified the culprit query:
    from:
       """Returns a Return resource by ID."""
       return(
         """The ID of the Return to return."""
         id: ID!
       ): Return
    
    to:
       """Returns a Return resource by ID."""
       return_resource(
         """The ID of the Return to return."""
         id: ID!
       ): Return
    

Now I'm getting a different error:

Selected strategy: Strategy.CLIENT
Using schema from 'path/to/graphql_schemas/shopify.admin.2024-07.graphql'.
Reading queries from ''.
Using 'graphql' as package name.
Generating package into 'library/shopify/'.
Using 'AdminClient' as client name.
Using 'AsyncBaseClient' as base client class.
Coping base client class from '/usr/local/lib/python3.12/site-packages/ariadne_codegen/client_generators/dependencies/async_base_client.py'.
Generating enums into 'enums.py'.
Generating inputs into 'input_types.py'.
Generating fragments into 'fragments.py'.
Comments type: stable
Converting fields and arguments name to snake case.
Generating async client.
No files to copy.
Plugins to use: ariadne_codegen.contrib.client_forward_refs.ClientForwardRefsPlugin,ariadne_codegen.contrib.no_reimports.NoReimportsPlugin
Traceback (most recent call last):
  File "/usr/local/bin/ariadne-codegen", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/main.py", line 37, in main
    client(config_dict)
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/main.py", line 81, in client
    generated_files = package_generator.generate()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/client_generators/package.py", line 171, in generate
    self._generate_client()
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/client_generators/package.py", line 257, in _generate_client
    client_module = self.client_generator.generate()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/client_generators/client.py", line 148, in generate
    module = self.plugin_manager.generate_client_module(module)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/plugins/manager.py", line 60, in generate_client_module
    return self._apply_plugins_on_object("generate_client_module", module)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/plugins/manager.py", line 40, in _apply_plugins_on_object
    modified_obj = method(modified_obj, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/contrib/client_forward_refs.py", line 91, in generate_client_module
    self._insert_import_statement_in_method(method_def)
  File "/usr/local/lib/python3.12/site-packages/ariadne_codegen/contrib/client_forward_refs.py", line 184, in _insert_import_statement_in_method
    module=self.imported_classes[import_class_id],
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
KeyError: 'self'

Disabling the ClientForwardRefsPlugin solves that issue, but I'd love the performance gain...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants