Skip to content

Updates for newest pydantic #40

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 8 additions & 10 deletions pydantic2ts/cli/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def is_concrete_pydantic_model(obj) -> bool:
if not inspect.isclass(obj):
return False
elif obj is BaseModel:
return False
elif GenericModel and issubclass(obj, GenericModel):
return bool(obj.__concrete__)
return getattr(obj, "__concrete__", False)
else:
return issubclass(obj, BaseModel)

Expand Down Expand Up @@ -152,18 +150,18 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
'[k: string]: any' from being added to every interface. This change is reverted
once the schema has been generated.
"""
model_extras = [getattr(m.Config, "extra", None) for m in models]
model_extras = [m.model_config.get("extra", None) for m in models]

try:
for m in models:
if getattr(m.Config, "extra", None) != Extra.allow:
m.Config.extra = Extra.forbid
if m.model_config.get("extra", None) != Extra.allow:
m.model_config["extra"] = Extra.forbid

master_model = create_model(
"_Master_", **{m.__name__: (m, ...) for m in models}
"_Master_", **{m.__name__: (m, ...) for m in models}, __base__=m
)
master_model.Config.extra = Extra.forbid
master_model.Config.schema_extra = staticmethod(clean_schema)
master_model.model_config["extra"] = Extra.forbid
master_model.model_config["schema_extra"] = staticmethod(clean_schema)

schema = json.loads(master_model.schema_json())

Expand All @@ -175,7 +173,7 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
finally:
for m, x in zip(models, model_extras):
if x is not None:
m.Config.extra = x
m.model_config["extra"] = x


def generate_typescript_defs(
Expand Down