Skip to content

bug: Provided name cannot be used as python identifier. #712

Closed

Description

@BaptisteGi

Component

No response

Infrahub SDK version

1.17.0

Current Behavior

When running the command infrahubctl graphql generate-return-types query1.gql I got the following exception:

Error: Provided name  cannot be used as python identifier.
    raise InvalidConfiguration(
ariadne_codegen.exceptions.InvalidConfiguration: Provided name  cannot be used as python identifier.

Expected Behavior

I expect the command to work

Steps to Reproduce

  • Setup project
mkdir some-project
cd some-project
uv init
uv add 'infrahub-sdk[all]'
export INFRAHUB_API_TOKEN=06438eb2-8019-4776-878c-0941b1f1d1ec
  • Create a query1.gql with the following content:
query Test {
  BuiltinTag {
    edges {
      node {
        name {
          value
        }
      }
    }
  }
}
  • Run command infrahubctl graphql export-schema
  • Then infrahubctl graphql generate-return-types query1.gql

=> This fails with Error: Provided name cannot be used as python identifier.

Additional Information

The problem is that directory.name is empty when the GraphQL file is in the current directory.

target_package_name=directory.name,

So either we force people to put their queries in a folder, or we adjust code to account for local folder.

Activity

  1. coderabbitai commented on Dec 29, 2025

    @coderabbitai

    📝 CodeRabbit Plan Mode

    Generate an implementation plan and prompts that you can use with your favorite coding agent.

    ✅ Successfully generated implementation plan.

    💡 Use @coderabbitai <feedback> to replan

    Examples

    🔗 Similar Issues

    Related Issues

    👤 Suggested Assignees

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. coderabbitai commented on Dec 29, 2025

    @coderabbitai

    Coding Plan

    Approach

    Fix the generate-return-types command to work when GraphQL query files are located in the current directory by providing a valid default package name instead of an empty string.

    Observations

    The infrahub-sdk uses Typer for CLI commands organized into modular apps under infrahub_sdk/ctl/. The GraphQL module (infrahub_sdk/ctl/graphql.py) integrates with ariadne-codegen v0.15.3 to generate Pydantic models from GraphQL queries. The command processes .gql files grouped by directory and generates separate client packages for each directory using ariadne-codegen's ClientSettings with a target_package_name parameter.

    Assumptions
    Assumption 1: What package name should be used for queries in the current directory?

    Options Considered:

    • Use "graphql_client" (ariadne-codegen's default package name)
    • Use "queries" (a custom descriptive name)
    • Use the actual current directory name from filesystem (requires sanitization)

    Chosen Option: Use "graphql_client" as the default

    Rationale: This aligns with ariadne-codegen's own default behavior, provides a predictable and valid Python package name, and requires minimal code change. Developers familiar with ariadne-codegen will recognize this conventional name, and it avoids complexity around sanitizing arbitrary directory names.

    💡 User Tips

    Regenerate the plan with different choices with @coderabbitai <feedback>.

    Implementation Steps

    Fix Empty Package Name Bug

    This phase addresses the core issue by ensuring a valid package name is always provided to ariadne-codegen.

    Add Default Package Name Fallback

    Modify the package name generation to use a fallback when the directory name is empty.

    • Update infrahub_sdk/ctl/graphql.py at line 160 to change target_package_name=directory.name to target_package_name=directory.name or "graphql_client"
    • This ensures that when .gql files are in the current directory (where Path.cwd().name or Path('.').name returns empty string), a valid default package name is used
    • The fallback value "graphql_client" matches ariadne-codegen's default convention and is guaranteed to be a valid Python identifier
    🤖 Prompt for AI agents
    Fix the empty package name bug in the generate-return-types command.
    
    In `infrahub_sdk/ctl/graphql.py`:
    - Modify the `target_package_name` assignment to use a fallback value
    - Change from `target_package_name=directory.name` to
    `target_package_name=directory.name or "graphql_client"`
    - This ensures a valid package name when `.gql` files are in the current
    directory
    

    🚀 Next Steps

    🤖 All AI agent prompts combined
    Fix the empty package name bug in the generate-return-types command.
    
    In `infrahub_sdk/ctl/graphql.py`:
    - Modify the `target_package_name` assignment to use a fallback value
    - Change from `target_package_name=directory.name` to
    `target_package_name=directory.name or "graphql_client"`
    - This ensures a valid package name when `.gql` files are in the current
    directory
    
    💡 Iterate on the plan with: @coderabbitai <feedback>
    Example Feedback
    - @coderabbitai You can skip phase 3. Add a simple unit test case for phase 2.
    - @coderabbitai For assumption 1 go ahead with option 3 and replan.
    

    💬 Have feedback or questions? Drop into our discord!

  3. linked a pull request that will close this issueFix issue #713 and #712 #714on Dec 29, 2025
  4. added a commit that references this issue on Dec 29, 2025
  5. added a commit that references this issue on Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions