Skip to content

Feature request: Positional arguments from pydantic model #165

@ukch

Description

@ukch

According to the documentation, the way to make positional arguments in TAP is to define a configure method and call self.add_argument("argument_name"). However, that does not work with a pydantic model (using either tapify or to_tap_class) as the TAP class is auto-generated without the option to customise.

Here are a few things I have tried:

  1. Add configure method to the pydantic model:
class MyModel(pydantic.BaseModel):
    foo: str

    def configure(self):
        assert False  # this is never called

tapify(MyModel)  # the following arguments are required: foo
  1. Call add_argument manually:
class MyModel(pydantic.BaseModel):
    foo: str

parser = to_tap_class(MyModel)()
parser.add_argument("foo")  # Raises "ValueError: add_argument cannot be called after initialization."
  1. Subclass workaround
class MyModel(pydantic.BaseModel):
    foo: str

class MyModelTap(to_tap_class(MyModel)):  # type: ignore
    def configure(self):
        self.add_argument("foo")

parser = MyModelTap()
parser.parse_args(argv)  # this does work, but breaks typing

Please can support for this use-case be added to the library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions