Skip to content

Add NonNegativeFloat, NonPositiveFloat etc #1975

@mdavis-xyz

Description

@mdavis-xyz

Checks

  • [y] I added a descriptive title to this issue
  • [y] I have searched (google, github) for similar issues and couldn't find anything
  • [y] I have read and followed the docs and still think this feature/change is needed
  • [y] After submitting this, I commit to one of:
    • Look through open issues and helped at least one other person
    • Hit the "watch" button on this repo to receive notifications and I commit to help at least 2 people that ask questions in the future
    • Implement a Pull Request for a confirmed bug

I'll write this PR myself. Just checking here in case anyone else has feedback.

Feature Request

Pydantic already has types PositiveFloat, NegativeFloat, PositiveInt, NegativeInt.
These values all exclude zero.

I want to have a class that allows all positive numbers, including zero.

Proposed Solution

After eyeballing types.py I think we should have:


class NonPositiveFloat(ConstrainedFloat):
    le = 0


class NonNegativeFloat(ConstrainedFloat):
    ge = 0


class NonPositiveInt(ConstrainedInt):
    le = 0


class NonNegativeInt(ConstrainedInt):
    ge = 0

Yes I know the end user can do that. This just seems like the kind of thing that's probably quite common, so would be nice out of the box.

One thing I'm not sure about is the capitalisation. NonPositiveInt or NonpositiveInt?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions