brentyi / tyro

CLI interfaces & config objects, from types
https://brentyi.github.io/tyro
MIT License
467 stars 23 forks source link

Fix default values in Pydantic #109

Closed kevinddchen closed 8 months ago

kevinddchen commented 8 months ago

Default values are now properly parsed when an instance of a pydantic.BaseModel is provided.

# a.py

import pydantic
import tyro

class Config(pydantic.BaseModel):
    a: int = 1

def main(config: Config = Config(a=2)) -> None:
    pass

tyro.cli(main)
$ python a.py -h
usage: a.py [-h] [--config.a INT]

╭─ arguments ───────────────────────────────────────────╮
│ -h, --help            show this help message and exit │
╰───────────────────────────────────────────────────────╯
╭─ config arguments ────────────────────────────────────╮
│ --config.a INT        (default: 2)                    │  # <-- good!
╰───────────────────────────────────────────────────────╯

This fixes #107 and fixes #108.

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (ed0eaac) 98.93% compared to head (89feabb) 99.52%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #109 +/- ## ========================================== + Coverage 98.93% 99.52% +0.59% ========================================== Files 24 24 Lines 2061 1891 -170 ========================================== - Hits 2039 1882 -157 + Misses 22 9 -13 ``` | [Flag](https://app.codecov.io/gh/brentyi/tyro/pull/109/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Brent+Yi) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/brentyi/tyro/pull/109/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Brent+Yi) | `99.52% <100.00%> (+0.59%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Brent+Yi#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

kevinddchen commented 8 months ago

@brentyi Github won't let me add you as a reviewer, but this is ready. Feel free to edit as you see fit.

brentyi commented 8 months ago

Thanks @kevinddchen!! This was really helpful. 🙂