Skip to content

Commit

Permalink
Update bare import of pydantic to use v1 shim
Browse files Browse the repository at this point in the history
Signed-off-by: Mattt Zmuda <mattt@replicate.com>
  • Loading branch information
mattt authored and technillogue committed May 2, 2024
1 parent a42f280 commit 6806ca6
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions python/cog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
from enum import Enum
from types import ModuleType

import pydantic
try:
from pydantic.v1 import AnyHttpUrl, BaseModel, Extra, create_model # type: ignore
except ImportError:
from pydantic import ( # pylint: disable=W0404
AnyHttpUrl,
BaseModel,
Extra,
create_model,
)


BUNDLED_SCHEMA_PATH = ".cog/schema.py"

Expand Down Expand Up @@ -38,7 +47,7 @@ def default_events(cls) -> t.List["WebhookEvent"]:
return [cls.START, cls.OUTPUT, cls.LOGS, cls.COMPLETED]


class PredictionBaseModel(pydantic.BaseModel, extra=pydantic.Extra.allow):
class PredictionBaseModel(BaseModel, extra=Extra.allow):
input: t.Dict[str, t.Any]


Expand All @@ -49,7 +58,7 @@ class PredictionRequest(PredictionBaseModel):
# TODO: deprecate this
output_file_prefix: t.Optional[str]

webhook: t.Optional[pydantic.AnyHttpUrl]
webhook: t.Optional[AnyHttpUrl]
webhook_events_filter: t.Optional[t.List[WebhookEvent]] = (
WebhookEvent.default_events()
)
Expand All @@ -59,7 +68,7 @@ def with_types(cls, input_type: t.Type[t.Any]) -> t.Any:
# [compat] Input is implicitly optional -- previous versions of the
# Cog HTTP API allowed input to be omitted (e.g. for models that don't
# have any inputs). We should consider changing this in future.
return pydantic.create_model(
return create_model(
cls.__name__, __base__=cls, input=(t.Optional[input_type], None)
)

Expand All @@ -85,7 +94,7 @@ def with_types(cls, input_type: t.Type[t.Any], output_type: t.Type[t.Any]) -> t.
# [compat] Input is implicitly optional -- previous versions of the
# Cog HTTP API allowed input to be omitted (e.g. for models that don't
# have any inputs). We should consider changing this in future.
return pydantic.create_model(
return create_model(
cls.__name__,
__base__=cls,
input=(t.Optional[input_type], None),
Expand Down

0 comments on commit 6806ca6

Please sign in to comment.