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

Updated to Pydantic 2 (superset of Jason's) #1030

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#
# pip-compile --allow-unsafe requirements/dev.in
#
annotated-types==0.6.0
# via
# -r requirements/prod.txt
# pydantic
attrs==23.2.0
# via
# jsonschema
Expand Down Expand Up @@ -48,7 +52,7 @@ docopt==0.6.2
# via coveralls
exceptiongroup==1.2.0
# via pytest
faker==24.4.0
faker==24.7.1
# via
# -r requirements/prod.txt
# faker-edu
Expand Down Expand Up @@ -143,7 +147,7 @@ pluggy==1.4.0
# tox
pre-commit==3.5.0
# via -r requirements/dev.in
pydantic==1.10.14
pydantic==2.6.4
# via -r requirements/prod.txt
pygments==2.17.2
# via diff-cover
Expand Down
2 changes: 1 addition & 1 deletion requirements/prod.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ PyYAML
click
python-dateutil
gvgen
pydantic<2.0.0
pydantic
python-baseconv
requests<=2.29.0 # There are issues with urllib on latest version
12 changes: 8 additions & 4 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#
# This file is autogenerated by pip-compile with Python 3.8
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --allow-unsafe requirements/prod.in
#
annotated-types==0.6.0
# via pydantic
certifi==2024.2.2
# via requests
charset-normalizer==3.3.2
# via requests
click==8.1.7
# via -r requirements/prod.in
faker==24.4.0
faker==24.7.1
# via
# -r requirements/prod.in
# faker-edu
Expand All @@ -29,8 +31,10 @@ jinja2==3.1.3
# via -r requirements/prod.in
markupsafe==2.1.5
# via jinja2
pydantic==1.10.14
pydantic==2.6.4
# via -r requirements/prod.in
pydantic-core==2.16.3
# via pydantic
python-baseconv==1.2.2
# via -r requirements/prod.in
python-dateutil==2.9.0.post0
Expand All @@ -47,7 +51,7 @@ sqlalchemy==1.4.52
# via -r requirements/prod.in
typing-extensions==4.10.0
# via
# faker
# pydantic
# pydantic-core
urllib3==1.26.18
# via requests
39 changes: 18 additions & 21 deletions snowfakery/cci_mapping_files/declaration_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path

import yaml
from pydantic import BaseModel, Extra, validator, PositiveInt
from pydantic import field_validator, ConfigDict, BaseModel, RootModel, PositiveInt


class AtomicDecl(T.NamedTuple):
Expand Down Expand Up @@ -59,24 +59,23 @@ def append(new_decl: AtomicDecl, existing_decl: T.Optional[AtomicDecl]):

class SObjectRuleDeclaration(BaseModel):
sf_object: str
priority: T.Literal["low", "medium", "high"] = None
priority: T.Optional[T.Literal["low", "medium", "high"]] = None

api: T.Literal["smart", "rest", "bulk"] = None
batch_size: int = None
bulk_mode: T.Literal["serial", "parallel"] = None
anchor_date: T.Union[str, date] = None
api: T.Optional[T.Literal["smart", "rest", "bulk"]] = None
batch_size: T.Optional[int] = None
bulk_mode: T.Optional[T.Literal["serial", "parallel"]] = None
anchor_date: T.Union[str, date, None] = None

load_after: str = None

class Config:
extra = Extra.forbid
load_after: T.Optional[str] = None
model_config = ConfigDict(extra="forbid")

@property
def priority_number(self):
values = {"low": 1, "medium": 2, "high": 3, None: 2}
return values[self.priority]

@validator("priority", "api", "bulk_mode", pre=True)
@field_validator("priority", "api", "bulk_mode", mode="before")
@classmethod
def case_normalizer(cls, val):
if hasattr(val, "lower"):
return val.lower()
Expand Down Expand Up @@ -105,12 +104,10 @@ def as_mapping(self):
class ChannelDeclaration(BaseModel):
"Channel declarations are only of relevance to Salesforce employees"
user: str
recipe_options: T.Dict[str, T.Any] = None
num_generators: PositiveInt = None
num_loaders: PositiveInt = None

class Config:
extra = Extra.forbid
recipe_options: T.Optional[T.Dict[str, T.Any]] = None
num_generators: T.Optional[PositiveInt] = None
num_loaders: T.Optional[PositiveInt] = None
model_config = ConfigDict(extra="forbid")


class ChannelDeclarationList(BaseModel):
Expand All @@ -125,8 +122,8 @@ class LoadDeclarationsTuple(T.NamedTuple):
] # Channel declarations are only of relevance to Salesforce employees


class SObjectRuleDeclarationFile(BaseModel):
__root__: T.List[T.Union[ChannelDeclarationList, SObjectRuleDeclaration]]
class SObjectRuleDeclarationFile(RootModel):
root: T.List[T.Union[ChannelDeclarationList, SObjectRuleDeclaration]]

@classmethod
def parse_from_yaml(cls, f: T.Union[Path, T.TextIO]):
Expand All @@ -139,12 +136,12 @@ def parse_from_yaml(cls, f: T.Union[Path, T.TextIO]):

sobject_decls = [
obj
for obj in cls.parse_obj(data).__root__
for obj in cls.model_validate(data).root
if isinstance(obj, SObjectRuleDeclaration)
]
channel_decls = [
obj
for obj in cls.parse_obj(data).__root__
for obj in cls.model_validate(data).root
if isinstance(obj, ChannelDeclarationList)
]
if len(channel_decls) > 1:
Expand Down