Skip to content

Commit

Permalink
[bento][ads_ranking] update pydantic to 2.7.4 and keep modules in `…
Browse files Browse the repository at this point in the history
…ads_ranking` to be compatible with `pydantic` v1/v2

Summary:
as title
* update `pydantic` to 2.7.4 in `ads_ranking` bento kernel
* below 3 modules will follow [code](https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/pydantic_v1/__init__.py) to be compatible with `pydantic` v1/v2 (they still use pydantic v1)
  - `fbcode/admarket/ads_copilot`
  - `fbcode/admarket/revenue/scripts/forecast/model`
  - `fbcode/admarket/signals/integrity/hackathon/`

This will enable:
- `pydantic` v2 is accessible in `ads_ranking` bento kernel
- current modules relying on `pydantic` v1 keep using v1
- current modules relying on `pydantic` v1 can plan/test migrating to v2 as needed

Test Plan:
* sandcastle
* build local `ads_ranking` bento kernel and test import
  - build local kernel
  ```
  [[email protected] ~/fbsource/fbcode (bfd9942bb)]$ bento kernel build ads_ranking
  ... ...
  Buck UI: https://www.internalfb.com/buck2/3f0c71df-0f66-4844-b53e-9ae74405d0cc
  ... ...
  BUILD SUCCEEDED
  ```
  - test import in local kernel (N5605155)
   {F1760989728}

Differential Revision: D59764837

fbshipit-source-id: 96e0fb99f9bea0b2ab1836389c661f1108f1e08d
  • Loading branch information
shiquanwang authored and facebook-github-bot committed Jul 19, 2024
1 parent 5fecea8 commit 06640da
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions antlir/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
import os
from typing import Type, TypeVar, Union

import pydantic
from antlir.freeze import DoNotFreeze, freeze
from antlir.fs_utils import Path

try:
from pydantic.v1 import BaseModel # pragma: no cover # noqa: F403
from pydantic.v1.main import ModelMetaclass # pragma: no cover # type: ignore
except ImportError:
from pydantic import BaseModel # type: ignore # noqa: F403
from pydantic.main import ModelMetaclass # type: ignore # noqa: F403


S = TypeVar("S")


class ShapeMeta(pydantic.main.ModelMetaclass):
class ShapeMeta(ModelMetaclass):
def __new__(metacls, name, bases, dct): # noqa: B902
cls = super().__new__(metacls, name, bases, dct)
# Only apply shape meta hacks to generated classes, not user-written
Expand Down Expand Up @@ -61,7 +67,7 @@ def __repr__(cls) -> str: # noqa: B902
return f"{clsname}({fields})"


class Shape(pydantic.BaseModel, DoNotFreeze, metaclass=ShapeMeta):
class Shape(BaseModel, DoNotFreeze, metaclass=ShapeMeta):
class Config:
allow_mutation = False

Expand Down

0 comments on commit 06640da

Please sign in to comment.