-
Notifications
You must be signed in to change notification settings - Fork 3
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
Move to pydantic 2 #122
Comments
From discussion with @coretl: The main remaining complication is that We're thinking about converting the scanspecs and regions to base models and putting in some sort of shimming to allow positional args. Options are:
|
In order of preference:
3 was mostly rejected because of the sheer volume of tests and docs that use positional args 2 could be accomplished messily at runtime only (type checker will still moan that it doesn't support positional args): from pydantic import BaseModel, Field
class Spec(BaseModel):
@classmethod
def __pydantic_init_subclass__(cls, **kwargs):
original_init = cls.__init__
def patched_init(self, *args, **kwargs):
for k, v in zip(cls.model_fields, args):
kwargs[k] = v
original_init(self, **kwargs)
cls.__init__ = patched_init
class Line(Spec):
start: float = Field(description="Midpoint of the first point of the line")
stop: float = Field(description="Midpoint of the last point of the line")
num: int = Field(min=1, description="Number of frames to produce")
obj = Line(3, 4, 5) 1 would be the neatest solution if someone could make it work... |
One of the main issues is that I think fixing this by post-dataclass-initialisation hacking over the validator is possible, but would be really messy and fragile, and I think option 3 is actually what would end up with the most maintainable, correctly typed result in the end |
I agree that messing with pydantic under the bonnet is increasingly fragile. Happy with 2. or 3. @dperl-dls do you have a particular objection to 2.? |
I have no specific objection to it as such, I just think it might not be that easy. Anyway, I might have to give up on this, I've tried a few different ways for a few days and I can't really get it to work. Starting from @evalott100 's WIP I have a branch here: The main problem I'm running into is that I'm finding it impossible to rebuild the model on finalising without pydantic complaining that I've provided multiple options for each tag BTW when researching I found this discussion: pydantic/pydantic#8789 which would have been useful, so linking here for reference |
Just to say I spent a couple of hours on this today and made a bit of progress. The dataclass approach seems to work with some hacky workarounds (I can prod the |
Ok, I now have a working dataclass implementation here: @ZohebShaikh will take this ticket on to apply the strategy in this repo to scanspec There is one remaining bug, if all |
I've fixed that issue, and I've also simplified and commented while I'm at it: |
No description provided.
The text was updated successfully, but these errors were encountered: