Skip to content

Commit

Permalink
Merge pull request #51 from VianneyMI/50-migrate-to-pydantic-v2
Browse files Browse the repository at this point in the history
 50-migrate-to-pydantic-v2
  • Loading branch information
VianneyMI committed Jul 23, 2023
2 parents 38c9661 + aaad07f commit 8a4385e
Show file tree
Hide file tree
Showing 38 changed files with 143 additions and 154 deletions.
18 changes: 18 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Release Notes

## 0.14.0

### Upgrades

* Make package compatible with pydantic V2

### Refactoring

* Use an import trick to still use pydantic V1 even on environments using pydantic V2
* Centralized pydantic import into base.py in order to avoid having to use import trick on multiple files

### Documentation

* Updated readme to better reflect current state of the pacakge.
* Started a changelog ! :champagne:
* Major change in the doc
2 changes: 1 addition & 1 deletion monggregate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from monggregate.pipeline import Pipeline
from monggregate.expressions.expressions import Expression

__version__ = "0.13.0"
__version__ = "0.14.0"
__author__ = "Vianney Mixtur"
__contact__ = "prenom.nom@outlook.fr"
__copyright__ = "Copyright © 2022 Vianney Mixtur"
Expand Down
7 changes: 6 additions & 1 deletion monggregate/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

# 3rd Party imports
# ---------------------------
from pydantic import BaseModel as PydanticBaseModel, BaseConfig
import pydantic
if pydantic.__version__.startswith("1"):
from pydantic import BaseModel as PydanticBaseModel, BaseConfig, ValidationError, Field, validator
else:
from pydantic.v1 import BaseModel as PydanticBaseModel, BaseConfig, ValidationError, Field, validator

from humps import camelize

class BaseModel(PydanticBaseModel, ABC):
Expand Down
10 changes: 5 additions & 5 deletions monggregate/expressions/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# 3rd Party imports
# ---------------------------
from pydantic import validator
from monggregate.base import validator

# Local imports
# ----------------------------
Expand Down Expand Up @@ -67,11 +67,11 @@ class Expression(BaseModel):
Expressions can be nested.
"""

constant : int | float | str | bool | None
field : FieldPath | None
variable : Variable | None
constant : int | float | str | bool | None = None
field : FieldPath | None = None
variable : Variable | None = None

content : Content | None
content : Content | None = None

@validator("variable", pre=True, always=True)
@classmethod
Expand Down
6 changes: 5 additions & 1 deletion monggregate/expressions/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

# 3rd Party imports
# -------------------------------------------
from pydantic import ConstrainedStr
import pydantic
if pydantic.__version__.startswith("1"):
from pydantic import ConstrainedStr
else:
from pydantic.v1.types import ConstrainedStr

# Types definition
# -------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions monggregate/operators/array/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
$filter returns all matching array elements.
"""

from pydantic import validator, Field
from typing import Any
from monggregate.base import Field
from monggregate.operators.array.array import ArrayOperator

class Filter(ArrayOperator):
Expand Down Expand Up @@ -71,7 +71,7 @@ class Filter(ArrayOperator):
expression : Any = Field(alias="input")
query : Any = Field(alias="cond")
let : str | None = Field("this", alias="as")
limit : int | None = Field(ge=1) # NOTE : limit can actually be an expression but constraints are invalid with any type
limit : int | None = Field(None, ge=1) # NOTE : limit can actually be an expression but constraints are invalid with any type

@property
def statement(self) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion monggregate/operators/array/max_n.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"""

from pydantic import Field
from typing import Any
from monggregate.base import Field
from monggregate.operators.array.array import ArrayOperator

class MaxN(ArrayOperator):
Expand Down
2 changes: 1 addition & 1 deletion monggregate/operators/array/min_n.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"""

from pydantic import Field
from typing import Any
from monggregate.base import Field
from monggregate.operators.array.array import ArrayOperator

class MinN(ArrayOperator):
Expand Down
6 changes: 3 additions & 3 deletions monggregate/operators/array/sort_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
"""

from typing import Literal
from pydantic import Field
from typing import Any
from typing import Any, Literal

from monggregate.base import Field
from monggregate.operators.array.array import ArrayOperator

class SortArray(ArrayOperator):
Expand Down
4 changes: 2 additions & 2 deletions monggregate/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class Pipeline(BaseModel): # pylint: disable=too-many-public-methods
"""

stages : list[Stage] = []
_db : Database | None # necessary to execute the pipeline
collection : str | None
_db : Database | None = None# necessary to execute the pipeline
collection : str | None =None


@property
Expand Down
4 changes: 1 addition & 3 deletions monggregate/search/collectors/facet.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@
from datetime import datetime
from typing import Literal

from pydantic import validator

from monggregate.base import BaseModel
from monggregate.base import BaseModel, validator
from monggregate.expressions.fields import FieldName
from monggregate.search.collectors.collector import SearchCollector
from monggregate.search.operators import(
Expand Down
4 changes: 2 additions & 2 deletions monggregate/search/commons/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"""

from typing import Literal
from pydantic import Field
from monggregate.base import BaseModel

from monggregate.base import BaseModel, Field

class CountOptions(BaseModel):
"""Class defining the count parameters."""
Expand Down
3 changes: 1 addition & 2 deletions monggregate/search/commons/fuzzy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module defining an interface to define the fuzzy search parameters."""

from pydantic import Field
from monggregate.base import BaseModel
from monggregate.base import BaseModel, Field

class FuzzyOptions(BaseModel):
"""Class defining the fuzzy search parameters."""
Expand Down
4 changes: 2 additions & 2 deletions monggregate/search/commons/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"""

from typing import Literal
from pydantic import Field
from monggregate.base import BaseModel

from monggregate.base import BaseModel, Field

class HighlightOptions(BaseModel):
"""Class defining the highlighting parameters."""
Expand Down
2 changes: 1 addition & 1 deletion monggregate/search/operators/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"""
from datetime import datetime
from typing import Literal
from pydantic import Field
from monggregate.base import Field
from monggregate.search.operators.operator import SearchOperator, Clause
from monggregate.search.operators.clause import (
Autocomplete,
Expand Down
2 changes: 1 addition & 1 deletion monggregate/search/operators/more_like_this.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"""

from pydantic import validator
from monggregate.base import validator
from monggregate.search.operators.operator import SearchOperator


Expand Down
10 changes: 5 additions & 5 deletions monggregate/search/operators/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@


from datetime import datetime
from pydantic import validator
from monggregate.base import validator
from monggregate.search.operators.operator import SearchOperator

class Range(SearchOperator, smart_union=True):
Expand All @@ -90,10 +90,10 @@ class Range(SearchOperator, smart_union=True):
"""

path : str | list[str]
gt : int | float | datetime | None
lt : int | float | datetime | None
gte : int | float | datetime | None
lte : int | float | datetime | None
gt : int | float | datetime | None = None
lt : int | float | datetime | None = None
gte : int | float | datetime | None = None
lte : int | float | datetime | None = None
score : dict|None

@validator("gte", pre=True, always=True)
Expand Down
6 changes: 3 additions & 3 deletions monggregate/search/operators/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class Text(SearchOperator):

query : str|list[str]
path : str | list[str]
fuzzy : dict | None
score : dict | None
synonyms : str | None
fuzzy : dict | None = None
score : dict | None = None
synonyms : str | None = None

@property
def statement(self) -> dict:
Expand Down
4 changes: 2 additions & 2 deletions monggregate/search/operators/wildcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"""

from pydantic import Field
from monggregate.base import Field
from monggregate.search.operators.operator import SearchOperator

class Wilcard(SearchOperator):
Expand All @@ -89,7 +89,7 @@ class Wilcard(SearchOperator):
query : str | list[str]
path : str | list[str]
allow_analyzed_field : bool = Field(False, alias="allowAnalyzedField")
score : dict | None
score : dict | None = None

@property
def statement(self) -> dict:
Expand Down
8 changes: 4 additions & 4 deletions monggregate/stages/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"""

from typing import Any
from pydantic import Field, validator

from monggregate.base import Field, validator

from monggregate.stages.stage import Stage
from monggregate.expressions.content import Content, Const, Consts
Expand Down Expand Up @@ -100,8 +100,8 @@ class Bucket(Stage):

by : Content = Field(...,alias="group_by")
boundaries : Consts
default : Const | None
output : dict[FieldName, AccumulatorExpression] | None
default : Const | None = None
output : dict[FieldName, AccumulatorExpression] | None = None

# Validators
# ------------------------------
Expand Down
6 changes: 3 additions & 3 deletions monggregate/stages/bucket_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"""

from typing import Any
from pydantic import Field, validator
from monggregate.base import Field, validator
from monggregate.stages.stage import Stage
from monggregate.expressions.content import Content
from monggregate.expressions.fields import FieldName
Expand Down Expand Up @@ -139,8 +139,8 @@ class BucketAuto(Stage):
# ----------------------------------------------------------------------------
by : Content = Field(...,alias="group_by") # probably should restrict type to field_paths an operator expressions
buckets : int = Field(..., gt=0)
output : dict[FieldName, AccumulatorExpression] | None # Accumulator Expressions #TODO : Define type and use it here
granularity : GranularityEnum | None
output : dict[FieldName, AccumulatorExpression] | None = None# Accumulator Expressions #TODO : Define type and use it here
granularity : GranularityEnum | None = None


# Validators
Expand Down
2 changes: 1 addition & 1 deletion monggregate/stages/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"""
from typing import Any
from pydantic import Field, validator
from monggregate.base import Field, validator
from monggregate.stages.stage import Stage
from monggregate.expressions.content import Content
from monggregate.utils import validate_field_path
Expand Down
2 changes: 1 addition & 1 deletion monggregate/stages/limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"""

from pydantic import Field
from monggregate.base import Field
from monggregate.stages.stage import Stage

class Limit(Stage):
Expand Down
2 changes: 1 addition & 1 deletion monggregate/stages/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
"""

from pydantic import Field, validator
from monggregate.base import Field, validator
from monggregate.stages.stage import Stage
from monggregate.utils import StrEnum

Expand Down
2 changes: 1 addition & 1 deletion monggregate/stages/out.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"""

from pydantic import Field
from monggregate.base import Field
from monggregate.stages.stage import Stage

class Out(Stage):
Expand Down
8 changes: 4 additions & 4 deletions monggregate/stages/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
# NOTE : Would be nice and useful to have something keywords arguments based to generate the projection <VM, 16/09/2022>
# (on top[on the side] of the below)

from pydantic import validator
from monggregate.base import validator
from monggregate.stages.stage import Stage
from monggregate.utils import to_unique_list

Expand All @@ -144,9 +144,9 @@ class Project(Stage):
"""

include : list[str] | dict | bool | None
exclude : list[str] | dict | bool | None
fields : list[str] | None
include : list[str] | dict | bool | None = None
exclude : list[str] | dict | bool | None = None
fields : list[str] | None = None
projection : dict = {}

@validator("include", "exclude", pre=True, always=True)
Expand Down
2 changes: 1 addition & 1 deletion monggregate/stages/replace_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"""

from pydantic import Field, validator
from monggregate.base import Field, validator
from monggregate.stages.stage import Stage
from monggregate.utils import validate_field_path

Expand Down
2 changes: 1 addition & 1 deletion monggregate/stages/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"""

from pydantic import Field
from monggregate.base import Field
from monggregate.stages.stage import Stage

class Sample(Stage):
Expand Down
3 changes: 2 additions & 1 deletion monggregate/stages/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
from typing import Self
except ImportError:
from typing_extensions import Self
from pydantic import Field, validator

from monggregate.base import Field, validator
from monggregate.stages.stage import Stage
from monggregate.search.collectors import Facet, Facets
from monggregate.search.operators import(
Expand Down
Loading

0 comments on commit 8a4385e

Please sign in to comment.