Skip to content

Commit

Permalink
update to pydantic 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Jul 28, 2023
1 parent b221c04 commit c000f16
Show file tree
Hide file tree
Showing 10 changed files with 704 additions and 691 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ repos:
args: ["--fix"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.3.0
hooks:
- id: mypy
language_version: python
# No reason to run if only tests have changed. They intentionally break typing.
exclude: tests/.*
additional_dependencies:
- pydantic~=1.0
- pydantic~=2.0
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

Note: Minor version `0.X.0` update might break the API, It's recommended to pin `tipg` to minor version: `tipg>=0.1,<0.2`

## [unreleased]

## [0.3.0] - 2023-07-27

### Fixed
Expand Down
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ dependencies = [
"orjson",
"asyncpg>=0.23.0",
"buildpg>=0.3",
"fastapi>=0.95.1",
"fastapi>=0.100.0",
"jinja2>=2.11.2,<4.0.0",
"morecantile>=4.2,<5.0",
"pydantic[dotenv]~=1.0",
"geojson-pydantic>=0.4.3,<1.0",
"morecantile>=5.0,<6.0",
"pydantic~=2.0",
"pydantic-settings~=2.0",
"geojson-pydantic>=1.0,<2.0",
"pygeofilter>=0.2.0,<0.3.0",
"ciso8601~=2.3",
"starlette-cramjam>=0.3,<0.4",
Expand Down
28 changes: 14 additions & 14 deletions tipg/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from buildpg import logic, render
from ciso8601 import parse_rfc3339
from morecantile import Tile, TileMatrixSet
from pydantic import BaseModel, Field, root_validator
from pydantic import BaseModel, Field, model_validator
from pygeofilter.ast import AstType

from tipg.errors import (
Expand Down Expand Up @@ -89,14 +89,14 @@ class Column(BaseModel):

name: str
type: str
description: Optional[str]
geometry_type: Optional[str]
srid: Optional[int]
bounds: Optional[List[float]]
mindt: Optional[str]
maxdt: Optional[str]

@root_validator(pre=True)
description: Optional[str] = None
geometry_type: Optional[str] = None
srid: Optional[int] = None
bounds: Optional[List[float]] = None
mindt: Optional[str] = None
maxdt: Optional[str] = None

@model_validator(mode="before")
def sridbounds_default(cls, values):
"""Set default bounds and srid when this is a function."""
if values.get("geometry_type"):
Expand Down Expand Up @@ -160,12 +160,12 @@ class Collection(BaseModel):
id: str
table: str
dbschema: str = Field(..., alias="schema")
title: Optional[str]
description: Optional[str]
title: Optional[str] = None
description: Optional[str] = None
properties: List[Column] = []
id_column: Optional[str]
geometry_column: Optional[Column]
datetime_column: Optional[Column]
id_column: Optional[str] = None
geometry_column: Optional[Column] = None
datetime_column: Optional[Column] = None
parameters: List[Parameter] = []

@property
Expand Down
2 changes: 1 addition & 1 deletion tipg/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def connect_to_db(
con_init = connection_factory(schemas, user_sql_files)

app.state.pool = await asyncpg.create_pool_b(
settings.database_url,
str(settings.database_url),
min_size=settings.db_min_conn_size,
max_size=settings.db_max_conn_size,
max_queries=settings.db_max_queries,
Expand Down
28 changes: 11 additions & 17 deletions tipg/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""tipg dependencies."""

import re
import sys
from typing import Dict, List, Optional, Tuple
from typing import Dict, List, Optional, Tuple, get_args

from morecantile import Tile
from morecantile import tms as default_tms
from pygeofilter.ast import AstType
from pygeofilter.parsers.cql2_json import parse as cql2_json_parser
from pygeofilter.parsers.cql2_text import parse as cql2_text_parser
from typing_extensions import Annotated

from tipg.collections import Catalog, Collection
from tipg.errors import InvalidBBox, MissingCollectionCatalog, MissingFunctionParameter
Expand All @@ -19,12 +19,6 @@

from starlette.requests import Request

if sys.version_info >= (3, 9):
from typing import Annotated # pylint: disable=no-name-in-module
else:
from typing_extensions import Annotated


tms_settings = TMSSettings()


Expand Down Expand Up @@ -125,9 +119,9 @@ def OutputType(
) -> Optional[enums.MediaType]:
"""Output MediaType: json or html."""
if f:
return enums.MediaType[f.name]
return enums.MediaType[f]

accepted_media = [enums.MediaType[v] for v in enums.ResponseType.__members__]
accepted_media = [enums.MediaType[v] for v in get_args(enums.ResponseType)]
return accept_media_type(request.headers.get("accept", ""), accepted_media)


Expand All @@ -142,10 +136,10 @@ def QueryablesOutputType(
) -> Optional[enums.MediaType]:
"""Output MediaType: schemajson or html."""
if f:
return enums.MediaType[f.name]
return enums.MediaType[f]

accepted_media = [
enums.MediaType[v] for v in enums.QueryablesResponseType.__members__
enums.MediaType[v] for v in get_args(enums.QueryablesResponseType)
]
return accept_media_type(request.headers.get("accept", ""), accepted_media)

Expand All @@ -161,9 +155,9 @@ def ItemsOutputType(
) -> Optional[enums.MediaType]:
"""Output MediaType: geojson, html, json, csv, geojsonseq, ndjson."""
if f:
return enums.MediaType[f.name]
return enums.MediaType[f]

accepted_media = [enums.MediaType[v] for v in enums.ItemsResponseType.__members__]
accepted_media = [enums.MediaType[v] for v in get_args(enums.ItemsResponseType)]
return accept_media_type(request.headers.get("accept", ""), accepted_media)


Expand All @@ -178,9 +172,9 @@ def ItemOutputType(
) -> Optional[enums.MediaType]:
"""Output MediaType: geojson, json or html."""
if f:
return enums.MediaType[f.name]
return enums.MediaType[f]

accepted_media = [enums.MediaType[v] for v in enums.ItemResponseType.__members__]
accepted_media = [enums.MediaType[v] for v in get_args(enums.ItemResponseType)]
return accept_media_type(request.headers.get("accept", ""), accepted_media)


Expand Down Expand Up @@ -292,7 +286,7 @@ def filter_query(
) -> Optional[AstType]:
"""Parse Filter Query."""
if query is not None:
if filter_lang == enums.FilterLang.cql2_json:
if filter_lang == "cql2-json":
return cql2_json_parser(query)

# default to cql2-text
Expand Down
Loading

0 comments on commit c000f16

Please sign in to comment.