Skip to content

Instantly share code, notes, and snippets.

@mg3146
Last active August 15, 2023 10:51
Show Gist options
  • Save mg3146/66e52060cbd58ee05312f75b7eb2986c to your computer and use it in GitHub Desktop.
Save mg3146/66e52060cbd58ee05312f75b7eb2986c to your computer and use it in GitHub Desktop.
pydantic arg example
class OriginalClass:
start : str
end : str
def __init__(self, gen_string = None, start = None, end = None):
if gen_string:
start = gen_string[0:2]
end = gen_string[2:4]
self.start = start
self.end = end
example = OriginalClass('1y2y')
print(example.start) # '1y'
print(example.end) # '2y'
class PydanticVersion:
start : str
end : str
@rootvalidator(pre=True)
def process_values(cls, values):
gen_string = values.get('gen_string', None)
start = values.get('start', None)
end = values.get('end', None)
if gen_string:
start = gen_string[0:2]
end = gen_string[3:4]
start = start
end = end
return values
# I would like the user to still be able to init with just a single string, and let the root validator (or another method) take care of the rest. There is only ever 1 unnamed arg at most.
# so something like this, PydanticVersion('1y1y') should be a valid call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment