Skip to content

Commit

Permalink
fix(radiance-modifier): update ModifierBase
Browse files Browse the repository at this point in the history
This is a change merely to fix the issue for generating the OpenAPI docs with inheritance. From Pydantic point of view the code is good as is but it's not the case for generating the schema with inheritance.

For some mysterious reason the `update_forward_ref` for `_REFERENCE_UNION_MODIFIERS` doesn't get triggered in `ModifierBase` class when creating the subclasses. Moving the fields that are dependent on `_REFERENCE_UNION_MODIFIERS` to the subclasses resolves the issue.

I know that this is adding some code duplication but it fixes the documentation issue for now. I'm willing to spend more time in the near future to find a better fix.

resolves #102
  • Loading branch information
mostaphaRoudsari committed May 4, 2020
1 parent a0a51e4 commit 8da64bb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
78 changes: 66 additions & 12 deletions honeybee_schema/radiance/modifier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Modfier Schema"""
"""Modifier Schema"""
from __future__ import annotations
from pydantic import Field, constr, validator, root_validator
from typing import List, Union, Optional
Expand All @@ -14,7 +14,15 @@ class Void(BaseModel):
class ModifierBase(IDdRadianceBaseModel):
"""Base class for Radiance Modifiers"""

modifier: _REFERENCE_UNION_MODIFIERS = Field(
type: constr(regex='^ModifierBase$') = 'ModifierBase'


class Mirror(ModifierBase):
"""Radiance mirror material."""

type: constr(regex='^mirror$') = 'mirror'

modifier: _REFERENCE_UNION_MODIFIERS = Field(
default=None,
description='Material modifier (default: Void).'
)
Expand All @@ -27,12 +35,6 @@ class ModifierBase(IDdRadianceBaseModel):
'(default: []).'
)


class Mirror(ModifierBase):
"""Radiance mirror material."""

type: constr(regex='^mirror$') = 'mirror'

r_reflectance: float = Field(
default=1,
ge=0,
Expand Down Expand Up @@ -73,6 +75,19 @@ class Plastic(ModifierBase):

type: constr(regex='^plastic$') = 'plastic'

modifier: _REFERENCE_UNION_MODIFIERS = Field(
default=None,
description='Material modifier (default: Void).'
)

dependencies: List[_REFERENCE_UNION_MODIFIERS] = Field(
default=[],
description='List of modifiers that this modifier depends on. '
'This argument is only useful for defining advanced modifiers '
'where the modifier is defined based on other modifiers '
'(default: []).'
)

r_reflectance: float = Field(
default=0.0,
ge=0,
Expand Down Expand Up @@ -158,6 +173,19 @@ class Glass(ModifierBase):

type: constr(regex='^glass$') = 'glass'

modifier: _REFERENCE_UNION_MODIFIERS = Field(
default=None,
description='Material modifier (default: Void).'
)

dependencies: List[_REFERENCE_UNION_MODIFIERS] = Field(
default=[],
description='List of modifiers that this modifier depends on. '
'This argument is only useful for defining advanced modifiers '
'where the modifier is defined based on other modifiers '
'(default: []).'
)

r_transmissivity: float = Field(
default=0.0,
ge=0,
Expand Down Expand Up @@ -195,6 +223,19 @@ class BSDF(ModifierBase):

type: constr(regex='^BSDF$') = 'BSDF'

modifier: _REFERENCE_UNION_MODIFIERS = Field(
default=None,
description='Material modifier (default: Void).'
)

dependencies: List[_REFERENCE_UNION_MODIFIERS] = Field(
default=[],
description='List of modifiers that this modifier depends on. '
'This argument is only useful for defining advanced modifiers '
'where the modifier is defined based on other modifiers '
'(default: []).'
)

up_orientation: List[float] = Field(
default=(0.01, 0.01, 1.00),
min_items=3,
Expand Down Expand Up @@ -282,6 +323,19 @@ class Light(ModifierBase):

type: constr(regex='^light$') = 'light'

modifier: _REFERENCE_UNION_MODIFIERS = Field(
default=None,
description='Material modifier (default: Void).'
)

dependencies: List[_REFERENCE_UNION_MODIFIERS] = Field(
default=[],
description='List of modifiers that this modifier depends on. '
'This argument is only useful for defining advanced modifiers '
'where the modifier is defined based on other modifiers '
'(default: []).'
)

r_emittance: float = Field(
default=0.0,
ge=0,
Expand Down Expand Up @@ -320,9 +374,10 @@ class Glow(Light):
'to scene illumination.'
)

# Unioned Modifier Schema objects defined for type reference
_REFERENCE_UNION_MODIFIERS = Union[Plastic, Glass, BSDF, Glow, Light, Trans, Void,
Mirror]

# Union Modifier Schema objects defined for type reference
_REFERENCE_UNION_MODIFIERS = \
Union[Plastic, Glass, BSDF, Glow, Light, Trans, Void, Mirror]

# Required for self.referencing model
# see https://pydantic-docs.helpmanual.io/#self-referencing-models
Expand All @@ -334,4 +389,3 @@ class Glow(Light):
Light.update_forward_refs()
Trans.update_forward_refs()
Void.update_forward_refs()

2 changes: 1 addition & 1 deletion tests/test_openapi_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@


def test_gen_openapi():
rc = os.system('python ./docs.py')
rc = os.system('python ./docs.py --version 0.0.1')
assert rc == 0

0 comments on commit 8da64bb

Please sign in to comment.