mirumee / ariadne-codegen

Generate fully typed Python client for any GraphQL API from schema, queries and mutations
BSD 3-Clause "New" or "Revised" License
274 stars 35 forks source link

include_all_inputs = false leads to IndexError in isort #308

Open weblab-misha opened 3 months ago

weblab-misha commented 3 months ago

ariadne-codegen --config ariadne-codegen.toml runs fine unless I add include_all_inputs = false in my ariadne-codegen.toml file. With include_all_inputs = false command fails with error:

  File "/Users/user/my-project/venv/bin/ariadne-codegen", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/ariadne_codegen/main.py", line 37, in main
    client(config_dict)
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/ariadne_codegen/main.py", line 81, in client
    generated_files = package_generator.generate()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/ariadne_codegen/client_generators/package.py", line 152, in generate
    self._generate_input_types()
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/ariadne_codegen/client_generators/package.py", line 307, in _generate_input_types
    code = self._add_comments_to_code(ast_to_str(module), self.schema_source)
                                      ^^^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/ariadne_codegen/utils.py", line 33, in ast_to_str
    return format_str(isort.code(code), mode=Mode())
                      ^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/isort/api.py", line 92, in sort_code_string
    sort_stream(
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/isort/api.py", line 210, in sort_stream
    changed = core.process(
              ^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/isort/core.py", line 422, in process
    parsed_content = parse.file_contents(import_section, config=config)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/my-project/venv/lib/python3.12/site-packages/isort/parse.py", line 522, in file_contents
    if "," in import_string.split(just_imports[-1])[-1]:
                                  ~~~~~~~~~~~~^^^^
IndexError: list index out of range

BTW, nothing is wrong with include_all_enums = false, it doesn't cause any issues.

UPD:

$ ariadne-codegen --version
ariadne-codegen, version 0.14.0
$ isort --version

                 _                 _
                (_) ___  ___  _ __| |_
                | |/ _/ / _ \/ '__  _/
                | |\__ \/\_\/| |  | |_
                |_|\___/\___/\_/   \_/

      isort your imports, so you don't have to.

                    VERSION 5.12.0
weblab-misha commented 3 months ago

I've managed to run ariadne-codegen in VSCode python debugger, this is a screenshot from just before exception is raised:

image

Looks like "line" == "import_string" == 'from .enums import ', so just_imports variable is empty list

This is the same moment of execution, but I've switched to higher function in call stack:

image

And this is ast_to_str() (the same moment of execution):

image

Here is the beginning of code variable:

from typing import Optional, Any, Union, List, Annotated
from pydantic import Field, PlainSerializer
from .base_model import BaseModel
from .base_model import Upload
from .enums import 

class ...

Looks like from .enums import line causes the issue when passed to isort.code().

I tried to create a simple file test.py with a wrong from ... import line:

from .enums import

class A:
    pass

and run isort test.py, but it works fine. Looks like isort CLI handles these issues, so ariadne-codegen may be using isort.code() in a wrong way (?)

DamianCzajkowski commented 1 month ago

Hi @weblab-misha, I know how to fix this issue, but I found it hard to recreate in my own schema. Could you provide a schema example so I can better understand where the issue lies?