Code Monkey home page Code Monkey logo

pydantic-argparse's Introduction

SupImDos

🐍 Python developer from πŸ“ Perth, WA

@dataclass
class SupImDos:
    name: str = "Hayden Richards"
    username: str = "SupImDos"
    age: int = 25
    location: str = "Perth, WA"

if __name__ == "__main__":
    me = SupImDos()

πŸ”­ Languages

  • Python

🌱 Learning

  • Rust
  • HTML / CSS / JavaScript / TypeScript

pydantic-argparse's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pydantic-argparse's Issues

ArgumentParser PyLance Issue

Pylance is reporting the argument parser is not properly exported from the module, causing it to fail checks.

"ArgumentParser" is not exported from module "pydantic_argparse" Pylance[reportPrivateImportUsage](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)

Python 3.10 Support

I noticed the package restricts installation to python3.9 exclusively, are there any known compatibility problems with other versions?

Enhancement: Create project logo

Problem

  • pydantic-argparse should have a project logo, rather than the current standard # h1 text heading.

Solutions

  • Make a logo myself?
  • Wait for a contributor to make a logo?
  • Pay someone to make a logo?

Enhancement: Investigate `.env` file support

Summary

  • As raised by @ericchansen in #34, pydantic-argparse should be able to piggy-back off pydantic and support loading environment variables from .env files.
  • This can probably be accomplished by defining arguments as shown below:
    class Args(pydantic.BaseSettings):
        my_arg: int = pydantic.Field(description="this is an int")
    
        class Config:
            env_file = ".env"
            env_file_encoding = "utf-8"
  • See: https://docs.pydantic.dev/latest/usage/settings/#dotenv-env-support
  • If this does work, it might be nice to provide a dotenv extra for pydantic-argparse (similar to pydantic) so people can install it with:
    $ pip install pydantic-argparse[dotenv]

Enhancement: Implement real Pydantic v2 support

Summary

  • As raised by @lucidfrontier45 in #48, we need Pydantic v2 support in pydantic-argparse.
  • We now have pseudo-Pydantic v2 support, as the pydantic dependency is at least unpinned.
  • However this is just a shim to the Pydantic v1 API.
  • We should implement real Pydantic v2 support.
  • I think maintaining both Pydantic v1 and v2 compatibility is probably unreasonable (TBD).
  • More information to follow...

Notes

Metavar support

Hi great project! I was wondering if it is possible to implement something similar to the argparse packages metavar argument. I strongly prefer my arguments to specify what type should be input, and I would think this is doable since the pydantic backend is already keeping track of types.

Envision something like this:

from pydantic import BaseModel, Field, FilePath

class Args
    intarg: int = Field(..., description="an integer")
    file: FilePath = Field(..., description="a file")

then at cli:

./example_program.py --help

--intarg INT:    an integer
--file FilePath: a file

Do not set values for arguments not provided by the user

We need to know if a particular argument has been explicitly set by the user, or it is just the default value.
pydantic provides model.__fields_set__ to check for those, however pydantic-argparsecurrently sets all values, regardless of the user input.

A way of doing this is by not setting defaults in argparse and leave pydantic processing those instead.

I've modified the code in my fork, and I would be happy to contribute a PR.

Thanks.

Enhancement: Add subcommand parsing

Problem

  • A very useful feature of Python's argparse module is adding subparser commands.
  • pydantic-argparse currently does not offer this feature in a complete and/or typed way.
  • It is highly desirable for pydantic-argparse to implement all of the features of Python's argparse module.
  • Therefore, pydantic-argparse should implement this feature before an initial stable release.

Solution

  • Subparser commands should be implemented in pydantic-argparse as nested optional pydantic models.
  • For example:
    class Command1(pydantic.BaseModel):
        # Command 1 Arguments
        flag_a: bool = False
    
    class Command2(pydantic.BaseModel):
        # Command 2 Arguments
        flag_b: bool = False
    
    class Arguments(pydantic.BaseModel):
        # Global Arguments
        global_flag: bool = False
    
        # Sub-Commands
        command_1: Optional[Command1] = None
        command_2: Optional[Command2] = None
  • The case shown above would parse $ app --global-flag command_2 --flag-b as:
     Arguments(global_flag=True, command_1=None, command_2=Command2(flag_b=True))
  • This functionality should be recursive, and should work correctly for any n levels deep.

Enhancement: Implement full unit test coverage

Problem

  • It is critical that pydantic-argparse has 100% unit test coverage before an initial stable release.

Solution

  • Implement full unit test coverage before the initial stable release.

Consider supporting older Python versions?

Hello! Firstly, thank you for this package. I really like it and I want to use it in a project I'm developing.

The project I work on has the minimal Python version of 3.7 (currently the lowest supported version), but your package uses the version 3.9. Hence, my question: would you consider β€œdowngrading” your project to also support Python 3.7 and 3.8? I understand, that you may not want to do this, and it's fine! I could do all the work myself. If you do not want to maintain older Python version at all, it's fine too: I would then fork it and do it on my end. Thanks!

Support for positional arguments?

Is there currently a way to allow an argument to be passed positionally? E.g.,

class Args(BaseModel):
    x: str = pydantic.Field(description='The x.', positional=True)
    y: int = pydantic.Field(description='The y.')
./foo.py hello --y 5

Enhancement: Add hosted documentation with full library coverage

Problem

  • pydantic-argparse must have full documentation coverage before the stable release.
  • This documentation must include working examples and a quick-start guide.
  • This documentation must be hosted online, and easily accessible and readable for users.

Solution

  • There exist multiple competitors in the hosted Python documentation space:
  • The options should be evaluated, and the chosen one implemented and hosted online.

Subcommands support

Is it possible to generate subcommands in some way?

class BenchmarkArguments(BaseModel):
    type: Literal["benchmark"]
    benchmark: str


class AnalyzeArguments(BaseModel):
    type: Literal["analyze"]
    analyze: str


class Command(BaseModel):
    command: Union[AnalyzeArguments, BenchmarkArguments] = Field(..., discriminator="type")

parser = ArgumentParser(
    model=Command,
    description="arguments",
)
benchmark: Command = parser.parse_typed_args()

Enhancement: Add `lint`, `type check` and `unit test` merge checks

Problem

  • pydantic-argparse releases must always completely pass linting, type checking and unit testing.
  • pydantic-argparse must also adhere to the Google Python styleguide.
  • It is highly desirable that these checks are performed before a release is made.
  • These checks must also be performed on all Python versions that pydantic-argparse supports (3.9, 3.10)

Solution

  • CI such as GitHub actions should be setup.
  • CI should run linting, type checking and unit testing on all appropriate Python versions and OS's

Bad support for nested models

For example, I have a class like this.

class SubArgs(pydantic.BaseModel):
    name: str

class Arguments(pydantic.BaseModel):
    a: SubArgs
    b: SubArgs

this will give the following usage hints.
usage: Example Program [-h] [-v] {a,b} ...

a or b is a sub command.

How can I set the value of both a and b at the same time?
image

Add possiblity to address the same option via several alias names

It would be nice to have the possibility to define in the pydantic.BaseModel Field definition the possible names for the cli arguments. It would be possible to have both: the short and long versions of the same argument.

example:

class Arguments(pydantic.BaseModel):
    debug: bool = Field (..., description="Shows the debug information", alias="my-custom-debug")

above would give the possibility to specify the argument in the command line as command --my-custom-debug

How to add the short version of the same argument i.e: -d

If support for aliases=('d', 'my-custom-debug') would be added it would be nice as pydantic.Field does support extra arguments
Above would be interpreted to add the additional names to the parsed arguments

required field marked as optional

I got the following model

Arg(BaseModel):
    f = Field(default=os.environ.get("F"),  description="description of F", required=True)

The field is marked as optional even though I provide him with required option. I know I have to remove the default option but I would like to force it to be required.

Otherwise is there a chance to include a field to read variables from env ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.