-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
subcommand parsing with pydantic v1 models does not work. #71
Comments
Hi @sander76!
The error persists if we remove the from pydantic import BaseModel
class SubCommandOne(BaseModel):
name: str
class SubCommandTwo(BaseModel):
name: str
class ModelWithSubcommand(BaseModel):
sub_command: SubCommandOne | SubCommandTwo
# Prints `SubcommandOne`.
print(
type(
ModelWithSubcommand(
sub_command=SubCommandTwo(name="myname"),
).sub_command
)
) Bot Given this, do you think there's anything we could / should do here? |
Hmm. Didn't expect your example to happen. But I think I do know why it does and I have seen it before, but not in your case where you explicitly define the sub_command. But I guess the following is happening (and I think this is the case for tyro too) model = ModelWithSubcommand(sub_command={"name":"myname"}) Pydantic has no way of knowing what the type of Pydantic has the option of from pydantic import BaseModel
class SubCommandOne(BaseModel):
name: str
class SubCommandTwo(BaseModel):
name: str
class ModelWithSubcommand(BaseModel):
sub_command: SubCommandOne | SubCommandTwo
class Config:
smart_union = True
# Prints `SubcommandOne`.
print(
ModelWithSubcommand(
sub_command=SubCommandTwo(name="myname"),
)
) |
Cool! Adding the from pydantic import BaseModel
from tyro import cli
class SubCommandOne(BaseModel):
name: str
class SubCommandTwo(BaseModel):
name: str
class ModelWithSubcommand(BaseModel):
sub_command: SubCommandOne | SubCommandTwo
class Config:
smart_union = True
model = cli(
ModelWithSubcommand,
args=["sub-command:sub-command-two", "--sub-command.name", "myname"],
)
assert isinstance(model.sub_command, SubCommandTwo)
assert model.sub_command.name == "myname" |
To comment a bit on what's happening internally: I'm not sure that a workaround is even possible, but since this is resolved in newer versions of Pydantic my feeling is we probably don't need to worry about it on our end? |
Ah. I see. I guess you're right. But to prevent you from doing a lot of "needless" work, maybe even drop support for pydantic v1 altogether ? |
When using pydantic v1 and creating a model with subcommands, the first model is always used.
See the test below. Using pydantic v1 this fails.
The text was updated successfully, but these errors were encountered: