0

I am refencing the answer on this other stackoverflow post on using the Typing library Literal to specify a unique array to strings to validate the data with Pydantic but I am running into a problem of calling another class recursively.

This is what my code looks like:

from pydantic import BaseModel, PydanticValueError, ValidationError, validator
from typing import Literal,Optional


ACTION_TYPE_MAPPING = Literal["read", "write", "release"]

OBJECT_TYPE_MAPPING = Literal["multiStateValue", "multiStateInput", "multiStateOutput",
                       "analogValue", "analogInput", "analogOutput",
                       "binaryValue", "binaryInput", "binaryOutput"]

BOOLEAN_ACTION_MAPPING = Literal["active", "inactive"]


# MAIN MODEL
class BacnetRequestModel(BaseModel):
    action_type: ACTION_TYPE_MAPPING
    object_type: OBJECT_TYPE_MAPPING
    object_instance: int
    value: Optional[ValueModel(object_type)]  <---- MESSED UP HERE, how to call ValueModel?


class ValueModel(BaseModel):
    multiStateValue: Optional[int]
    multiStateInput: Optional[int]
    multiStateOutput: Optional[int]
    analogValue: Optional[int]
    analogInput: Optional[int]
    analogOutput: Optional[int]
    binaryValue: Optional[BOOLEAN_ACTION_MAPPING]
    binaryInput: Optional[BOOLEAN_ACTION_MAPPING]
    binaryOutput: Optional[BOOLEAN_ACTION_MAPPING]



test = BacnetRequestModel(action_type="write",
                        object_type="binaryOutput",
                        object_instance="3",
                        value = "active"
                          )

How do I call the class ValueModel based on the objectType that was inputted to the function where in this case it was binaryOutput that should only accept a value of BOOLEAN_ACTION_MAPPING. Any tips help not a lot of wisdom here...

Traceback is:

    value = Optional[ValueModel(object_type)]
NameError: name 'ValueModel' is not defined
bbartling
  • 3,288
  • 9
  • 43
  • 88
  • Maybe you mean `value: Optional[ValueModel]` and `test = BacnetRequestModel([...], value = ValueModel(binaryOutput="active"))` – alex_noname Jul 04 '22 at 15:37
  • Okay I fixed the `= :` typo in the class. But does this look right? `test = BacnetRequestModel(["write","binaryOutput","3"],value=ValueModel(binaryOutput="active"))`? – bbartling Jul 04 '22 at 15:43
  • Sorry I use [...] as just placeholder, try to use `test = BacnetRequestModel(action_type="write", object_type="binaryOutput", object_instance="3", value = ValueModel(binaryOutput="active"))` – alex_noname Jul 04 '22 at 15:53
  • If I just comment out where I had the mistake of `: =` like `#value: Optional[ValueModel]` it works else if it is not commented out I get a `value: Optional[ValueModel] NameError: name 'ValueModel' is not defined` – bbartling Jul 04 '22 at 16:00
  • I could probably just run with `#value: Optional[ValueModel]` or removing it from the `class BacnetRequestModel` – bbartling Jul 04 '22 at 16:00
  • Thanks for your help if you post an answer ill hit green check box – bbartling Jul 04 '22 at 16:01

0 Answers0