Skip to content

Remove SystemExit usage  #336

@mpkocher

Description

@mpkocher

SystemExit inherits from BaseException and should be used very sparingly.

https://docs.python.org/3/library/exceptions.html#SystemExit

Currently, the docs and examples are using it in several places.

https://docs.pydantic.dev/latest/concepts/pydantic_settings/#subcommands-and-positional-arguments

I suspect the core issue is the leaking of an argparse implementation detail (which internally calls sys.exit).

This is also perhaps the source of inconsistencies in exit codes when an error occurs. Sometimes it's 1 and sometimes it's 2.

Acceptance Criteria

  1. Remove usage of SystemExit in the docs
  2. CLI Examples should adhere to the standard style for Pydantic error catching (e.g., ValidationError)

Possible Solutions

  1. In Python 3.9, argparse added exit_on_error flag to help solve this friction point with exit being called.
  2. Subclass argparse.ArgumentParser and override .exit() or error() to raise the appropriate exception (from status != 0) instead of calling sys.exit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions