Skip to content

[Bug]: http-client-python - IndexError raised if there's an empty interface #7835

@akiles-esta-usado

Description

@akiles-esta-usado

Describe the bug

If the main namespace has a empty interface, the python client generator breaks.

error @typespec/http-client-python/unknown-error: Can't generate Python client code from this TypeSpec. Please open an issue on https://github.com/microsoft/typespec'.
========================================= error stack start ================================================
Error: Command failed: <project-path>/node_modules/@typespec/http-client-python/venv/bin/python <project-path>/node_modules/@typespec/http-client-python/eng/scripts/setup/run_tsp.py --package-version=1.0.0b1 --generate-packaging-files=true --validate-versioning=true --package-name= --flavor=undefined --package-mode=azure-dataplane --from-typespec=true --models-mode=dpg --output-folder=<project-path>/tsp-output/clients/python --tsp-file=/tmp/tsp-codegen/python-yaml-pathaee28f14-570c-4e78-8acf-cdad46280460.yaml
Traceback (most recent call last):
  File "<project-path>/node_modules/@typespec/http-client-python/eng/scripts/setup/run_tsp.py", line 40, in <module>
    codegen.CodeGenerator(output_folder=args.output_folder, tsp_file=args.tsp_file, **unknown_args).process()
  File "<project-path>/node_modules/@typespec/http-client-python/generator/pygen/codegen/__init__.py", line 84, in process
    serializer.serialize()
  File "<project-path>/node_modules/@typespec/http-client-python/generator/pygen/codegen/serializers/__init__.py", line 122, in serialize
    for client_namespace, client_namespace_type in self.code_model.client_namespace_types.items():
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<project-path>/node_modules/@typespec/http-client-python/generator/pygen/codegen/models/code_model.py", line 187, in client_namespace_types
    longest_namespace = sorted(self._client_namespace_types.keys())[-1]
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:892:11)
    at execSync (node:child_process:964:15)
    at onEmitMain (file://<project-path>/node_modules/@typespec/http-client-python/dist/emitter/emitter.js:207:13)
    at async Object.$onEmit [as emitFunction] (file://<project-path>/node_modules/@typespec/http-client-python/dist/emitter/emitter.js:95:9)
    at async runEmitter (file://<project-path>/node_modules/@typespec/compiler/dist/src/core/program.js:711:9)
    at async file://<project-path>/node_modules/@typespec/compiler/dist/src/core/program.js:684:9
    at async trackAction (file://<project-path>/node_modules/@typespec/compiler/dist/src/core/logger/console-sink.js:133:24)
    at async emit (file://<project-path>/node_modules/@typespec/compiler/dist/src/core/program.js:682:12)
========================================= error stack end ================================================

Found 1 error.

Reproduction

import "@typespec/http";
@service(#{ title: "Employee Management" })
namespace EmployeeManagement {
  interface EmployeeAPI {}
}

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingemitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions