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

include_all_inputs = false leads to IndexError in isort #308

Open
weblab-misha opened this issue Aug 13, 2024 · 2 comments
Open

include_all_inputs = false leads to IndexError in isort #308

weblab-misha opened this issue Aug 13, 2024 · 2 comments

Comments

@weblab-misha
Copy link

weblab-misha commented Aug 13, 2024

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
Copy link
Author

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
Copy link
Contributor

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?

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

When branches are created from issues, their pull requests are automatically linked.

2 participants