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

Rework casting mechanics #52

Open
simontaurus opened this issue Feb 22, 2024 · 2 comments
Open

Rework casting mechanics #52

simontaurus opened this issue Feb 22, 2024 · 2 comments

Comments

@simontaurus
Copy link
Contributor

simontaurus commented Feb 22, 2024

Current state:
robotController = robotModel.cast(RobotController, args)

Wish:
robotController = RobotController(robotModel, args..)
the would require to override the __init__ to accept another base model that is converted to a dict that is de-packed to the actual contructor
__init__(Union[BaseModel, kwargs])

see also: https://stackoverflow.com/questions/71895185/convert-derived-class-to-base-class-in-python/77446717#77446717

@LukasGold
Copy link
Contributor

LukasGold commented Feb 22, 2024

Idea:
Overwrite pydantic __init__ with:

__init__(*args, **kwargs):
    data = kwargs
    if len(args) > 1:
        raiseError("Only one positional argument is allowed!")
    elif isinstance(args[0], OswBaseModel):
        data = {**args[0].dict(), **kwargs}
    else:
         raiseError(f"Can't cast {type(args[0]} to {type(self)}!")
    super().__init__(**data)

Potential issue: Static code checking might be altered / validation might be corrupted

@simontaurus
Copy link
Contributor Author

simontaurus commented Jul 8, 2024

Potential issue: Static code checking might be altered / validation might be corrupted

validation not as long as we call the actual class constructor.
Static code checking should work since the return type of the target class contructor is the target class

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

No branches or pull requests

2 participants