agency icon indicating copy to clipboard operation
agency copied to clipboard

Optional args

Open hidaris opened this issue 1 year ago • 3 comments

Hello, I noticed that in version 1.3, the help method converts Python objects into a JSON structure. However, the current implementation prevents the use of UnionType in action definitions, which makes the args unable to be optional.

I looked at the code, and the main reason for this is:

# type
 sig_annotation = signature.parameters[arg_name].annotation
 if sig_annotation is not None and sig_annotation.__name__ != "_empty":

When the argument type is a UnionType, sig_annotation does not have the __name__ attribute.

Is this a design decision?

hidaris avatar Aug 10 '23 08:08 hidaris

Add a possible reference https://json-schema.org/understanding-json-schema/reference/combining.html#oneof

hidaris avatar Aug 10 '23 08:08 hidaris

You mean individual optional arguments? Yeah those are not supported yet. It's only because I haven't gotten to it.

The API currently assumes all method args are required and are plain JSON serializable types like (dict, list, string, ...). I should add that to the documentation I think.

This wasn't so much a design decision but just that I haven't explored it yet. I will tinker with this to see how the API can be adjusted to allow optional args.

operand avatar Aug 10 '23 21:08 operand

Yes, because after Python 3.10, the optional type is a special case of the union type, which can be written as int | None. In many scenarios, this is convenient for code reuse. However, what may need to be considered here is whether the action tends to be designed as a function with a single responsibility, which is also the reason I'm asking if it is a design decision.

hidaris avatar Aug 11 '23 04:08 hidaris