Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-nextgen] optionally support float strict type #14618

Merged
merged 2 commits into from Feb 12, 2023

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Feb 4, 2023

  • optionally support float strict type via the option "floatStrictType" (default to true)
  • add a test

To close #14499

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date.sh
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

FYI @taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @arun-nalla (2019/11) @spacether (2019/11) @krjakbrjak (2023/02)

Copy link
Member

@tomplus tomplus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

bin/configs/python-nextgen-aiohttp.yaml Show resolved Hide resolved
@@ -443,12 +449,20 @@ private String getPydanticType(CodegenParameter cp,
fieldCustomization.add("multiple_of=" + cp.getMultipleOf());
}

if (floatStrictType) {
Copy link
Contributor

@spacether spacether Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code path is used for json schema number (float + int) and float only processing. Number should allow both in.

How about:

  • not adding the strict info when it is number with no format
  • adding it when it is float only
  • adding tests of both of those cases (number vs float only) with and without strict on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an option so users can choose whether they want float in strict type or not.

Copy link
Contributor

@spacether spacether Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but that option should only apply to floats right? The way it is now looks like you are applying float strict when the spec has type: number which should allow in float and int. Did I misunderstand something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-    number: confloat(strict=True, le=543.2, ge=32.1) = ...
-   float: Optional[confloat(strict=True, le=987.6, ge=54.3)] = None
-    double: Optional[confloat(strict=True, le=123.4, ge=67.8)] = None
+    number: confloat(le=543.2, ge=32.1) = ...
+    float: Optional[confloat(le=987.6, ge=54.3)] = None
+    double: Optional[confloat(le=123.4, ge=67.8)] = None

As illustrated in the sample diff as part of this change, the option (when set to false) simply removes strict=True.

Copy link
Contributor

@spacether spacether Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you applying strict float to type number with no format? Won't it throw a validation error if an integer input is given to it? Integer input is valid for that definition per
https://json-schema.org/understanding-json-schema/reference/numeric.html#number

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you applying strict float to type number with no format?

It's a user decision - they can switch on or off strict mode via an option.

You can find more about why some users prefer strict mode in https://docs.pydantic.dev/blog/pydantic-v2/#strict-mode.

@wing328 wing328 merged commit 72c02e4 into master Feb 12, 2023
@wing328 wing328 deleted the python-nextgen-float branch February 12, 2023 11:44
@aimxhaisse
Copy link

Thanks a lot for doing this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REQ][python-nextgen] make float parsing more lenient
4 participants