-
Notifications
You must be signed in to change notification settings - Fork 9
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
Refraction index None results in type ValidationError #81
Comments
Update: this is also effecting "modifier_glass_air_boundary.json". |
Huh. Where did this default value of 1.52 come from, then? The only reason why that happens right now is because @mostaphaRoudsari sets it to Maybe we should change the default value in |
Actually, it seems that the use of None right now results in illegal (or at least very implicit) Radiance strings. I am going to change it. |
Re: the '1.52': It's interesting, in the docstrings the refraction index is said to be a default of 1.52, but the actual default is set as 'None'. |
It looks like it's a default that is built into the Radiance glass modifier. I made a change to honeybee-radiance so that we now explicitly say Please updated the JSON samples to use Explicit is better than implicit. |
This is not desired. I wish you would have waited for me a couple of hours before making the change and trusted that I might have had a reason for keeping the default to None. The refraction index is an option input in Radiance. This means that both of these definitions are valid in radiance:
You should be able to handle it by making the refraction_index optional in the Pydantic schema. refraction_index: Optional[float] = Field(
default=1.52,
ge=0,
description='A positive value for the index of refraction '
'(default: 1.52).'
) Also the description and the |
A few things to note:
|
One other thing to note about point 2. there is that @MingboPeng is going to get a headache on the honeybee-schema-dotnet side if |
Did you notice the use of There is no need for adding None as an option. Also you are confusing Pydantic model with OpenAPI specification. There will be no issues on OpenAPI and dotnet side if we use Optional. Here is a simple example that will hopefully clarify the differences. You should be able to run it as is. from typing import Optional
from pydantic import Field, constr, BaseModel
class Glass(BaseModel):
type: constr(regex='^glass$') = 'glass'
transmittance: float = Field(
default=0.0,
ge=0,
le=1
)
refraction_index: Optional[float] = Field(
default=1.52,
ge=0,
description='A positive value for the index of refraction '
'(default: 1.52).'
)
if __name__ == '__main__':
from pprint import pprint
glass_1 = {
"type": "glass",
"transmittance": 0.5,
"refraction_index": 1.4
}
glass_2 = {
"type": "glass",
"transmittance": 0.5,
"refraction_index": None
}
# will work fine
g1 = Glass.parse_obj(glass_1)
# will also work fine
g2 = Glass.parse_obj(glass_2)
# see below. There is no None in the default values.
pprint(Glass.schema()) {
'properties': {
'refraction_index': {'default': 1.52,
'description': 'A positive value for the '
'index of refraction '
'(default: 1.52).',
'minimum': 0,
'title': 'Refraction Index',
'type': 'number'},
'transmittance': {'default': 0.0,
'maximum': 1,
'minimum': 0,
'title': 'Transmittance',
'type': 'number'},
'type': {'default': 'glass',
'pattern': '^glass$',
'title': 'Type',
'type': 'string'}},
'title': 'Glass',
'type': 'object'} |
I didn't realize that. So, if we use |
It is good to know we can use Now I guess we can add all places with Union like this if it is not required. |
Ok. I changed the PR I was sending to honeybee-radiance to just elaborate the docstring so it's clear that this is a case where @MingboPeng , if you want to open a separate issue for wrapping all Unions with a default of |
If @MingboPeng can provide a list of items that are facing this issue or a pattern to find them it should be an easy fix. I'm also thinking that we might want to start writing tests to catch cases that are valid in Pydantic in general but are not desired for our OpenAPI schema. |
Given that the PR regarding |
For the glass sample json:
honeybee-schema/samples/modifier/modifier_glass_generic_exterior_window.json
Lines 2 to 12 in c1a894c
I currently have the Field set like this for the refraction index:
Which is resulting in a ValidationError, for the refraction index set to null, since 'none is not an allowed value'.
So it looks like the correct behaviour to define None/null conditions is to eliminate the property from the json completely, and then pydantic will automatically set the default value (in this case 1.52) when parsing the object.
Let me know if my interpretation is correct, I can fix it in the PR.
The text was updated successfully, but these errors were encountered: