Skip to content

Add support for async objects in CLI #517

@rusmux

Description

@rusmux

🚀 Feature request

Add support for objects that require asynchronous context for their creation.

Motivation

Currently I cannot instantiate objects like aiokafka.AIOKafkaProducer. For example:

In main.py:

import aiokafka
from jsonargparse import CLI


async def main(producer: aiokafka.AIOKafkaProducer) -> None:
    print(producer)


if __name__ == "__main__":
    CLI(main)

In config.yaml:

producer:
  class_path: aiokafka.AIOKafkaProducer
  init_args:
    bootstrap_servers: ["localhost:9092"]

In terminal:

python -m main --config config.yaml

Gives error:

RuntimeError: The object should be created within an async function or provide loop directly.

Pitch

The above code should runs without errors.

Alternatives

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions