question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

--field-constraints add decimal points for integers constraints

See original GitHub issue

Describe the bug An option --field-constraints add decimal points for integers constraints

To Reproduce

Example schema:

{
  "type":  "object",
  "properties": {
    "dataSize": {
      "type": "integer",
      "minimum": 10,
      "maximum": 64
    }
  },
}
from __future__ import annotations
from typing import Optional
from pydantic import BaseModel, Field

class Model(BaseModel):
    dataSize: Optional[int] = Field(None, ge=10.0, le=64.0)

Used commandline:

$ datamodel-codegen --input model.json --input-file-type jsonschema --field-constraints > model.py

Expected behavior Expected ge=10 not ge=10.0, same for le dataSize: Optional[int] = Field(None, ge=10, le=64)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kinicommented, Nov 5, 2021

@koxudaxi Thanks for looking into it. I also found that section of code when investigating this issue. I considered that maybe we can change the type annotations there to

        gt: Optional[Union[int, float]] = ...
        ...

But this idea doesn’t work because of an upstream Pydantic issue, samuelcolvin/pydantic#1423. And even if that worked, it would cause some questionable behavior, e.g.:

{
    "type": "object",
    "properties": {
        "foo": {
            "type": "integer",
            "minimum": 10.0,
            "maximum": 64
        },
        "bar": {
            "type": "number",
            "minimum": 10.0,
            "maximum": 64
        }
    }
}

would result in:

class Model(BaseModel):
    foo: Optional[int] = Field(None, ge=10.0, le=64)
    bar: Optional[float] = Field(None, ge=10.0, le=64)

where instead, one would maybe expect:

class Model(BaseModel):
    foo: Optional[int] = Field(None, ge=10, le=64)
    bar: Optional[float] = Field(None, ge=10.0, le=64.0)

I think ideally the type of gt, ge, lt, and le should match the structural type of the value being constrained. That is, if the schema specifies a value as an integer, its range bounds should be integers, and if the schema specifies a value as a float, its range bounds should be floats.

But this would be difficult because the class Constraints is defined independently from the specific value being constrained.

Also, it is somewhat unclear how to handle this in the case where the schema specifies a union type that contains int and/or float. Right now, we have the following behavior, but I don’t know that it really makes sense, nor am I sure how other consumers of the jsonschema format treat this kind of schema:

{
    "type": "object",
    "properties": {
        "foo": {
            "type": ["integer", "number"],
            "minimum": 10.0,
            "maximum": 64
        }
    }
}

becomes

class Model(BaseModel):
    foo: Optional[Union[int, float]] = Field(None, ge=10.0, le=64.0)

I.e. should 10.0 and 64.0 be used here, or 10 and 64? I don’t know. 😅

0reactions
koxudaxicommented, Nov 8, 2022

I hope the problem was fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Definitive Way to Constrain Response to X Decimal Places
I'm at my wit's end and need some help coming up with a definitive solution to this problem that I've seen pop up...
Read more >
Can you enter a constraint value with decimal in IB?
As of Xcode 6.3, constraint constant values seem to be restricted to integers in the storyboard editor (unlike multiplier values, which can be...
Read more >
11.1.3 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC
When such a column is assigned a value with more digits following the decimal point than are permitted by the specified scale, the...
Read more >
Understanding the SQL Decimal data type - SQLShack
One of the easiest workarounds is to increase the precision level of the columns to store bigger numbers. We can alter the data...
Read more >
Number fields | Blockly - Google Developers
On this page · Creation · Serialization · Constraints. Minimum value; Maximum value; Rounding · Common constraints. Positive numbers; Integers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found