Skip to content

Commit

Permalink
Merge pull request #36 from mongkok/pydantic-v2
Browse files Browse the repository at this point in the history
Pydantic v2 support
  • Loading branch information
mongkok authored Jul 23, 2023
2 parents e531103 + ad470b4 commit c87586b
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 180 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### 0.5.0

* Added Pydantic v2 support
* Removed Pydantic v1 support
* Removed `PydanticPanel`

### 0.4.0

* Fixed middleware `url_path_for`
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.0"
__version__ = "0.5.0"
7 changes: 3 additions & 4 deletions debug_toolbar/panels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import typing as t

from fastapi import Request, Response
from starlette.datastructures import URL
from starlette.middleware.base import RequestResponseEndpoint

from debug_toolbar.types import ServerTiming, Stats
Expand Down Expand Up @@ -67,11 +66,11 @@ def content(self) -> str:
def render(self, **context: t.Any) -> str:
return self.toolbar.render(self.template, **context)

def url_for(self, name: str, **path_params: t.Any) -> URL:
return self.toolbar.request.url_for(name, **path_params)
def url_for(self, name: str, **path_params: t.Any) -> str:
return str(self.toolbar.request.url_for(name, **path_params))

@property
def scripts(self) -> t.List[URL]:
def scripts(self) -> t.List[str]:
return []

async def process_request(self, request: Request) -> Response:
Expand Down
108 changes: 0 additions & 108 deletions debug_toolbar/panels/pydantic.py

This file was deleted.

2 changes: 1 addition & 1 deletion debug_toolbar/panels/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sqlparse
from fastapi import Request, Response
from fastapi.encoders import jsonable_encoder
from pydantic.color import Color
from pydantic_extra_types.color import Color
from sqlparse import tokens as T

from debug_toolbar.panels import Panel
Expand Down
3 changes: 1 addition & 2 deletions debug_toolbar/panels/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from time import perf_counter

from fastapi import Request, Response
from starlette.datastructures import URL

from debug_toolbar.panels import Panel
from debug_toolbar.types import ServerTiming, Stats
Expand Down Expand Up @@ -46,7 +45,7 @@ def content(self) -> str:
return self.render(rows=rows)

@property
def scripts(self) -> t.List[URL]:
def scripts(self) -> t.List[str]:
scripts = super().scripts
scripts.append(self.url_for("debug_toolbar.static", path="js/timer.js"))
return scripts
Expand Down
3 changes: 1 addition & 2 deletions debug_toolbar/panels/versions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import typing as t

from fastapi import Request, Response, __version__
from starlette.datastructures import URL

from debug_toolbar.panels import Panel
from debug_toolbar.types import Stats
Expand All @@ -16,7 +15,7 @@ def nav_subtitle(self) -> str:
return f"FastAPI {__version__}"

@property
def scripts(self) -> t.List[URL]:
def scripts(self) -> t.List[str]:
scripts = super().scripts
scripts.append(self.url_for("debug_toolbar.static", path="js/versions.js"))
return scripts
Expand Down
23 changes: 12 additions & 11 deletions debug_toolbar/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

from jinja2 import BaseLoader, ChoiceLoader, Environment, PackageLoader
from jinja2.ext import Extension
from pydantic import BaseSettings, Field, IPvAnyAddress, root_validator
from pydantic.color import Color
from pydantic import Field, IPvAnyAddress, model_validator
from pydantic_extra_types.color import Color
from pydantic_settings import BaseSettings, SettingsConfigDict


class DebugToolbarSettings(BaseSettings):
model_config = SettingsConfigDict(
title="Debug Toolbar",
env_prefix="DT_",
case_sensitive=False,
)

DEFAULT_PANELS: t.List[str] = Field(
[
"debug_toolbar.panels.versions.VersionsPanel",
"debug_toolbar.panels.timer.TimerPanel",
"debug_toolbar.panels.settings.SettingsPanel",
"debug_toolbar.panels.request.RequestPanel",
"debug_toolbar.panels.headers.HeadersPanel",
"debug_toolbar.panels.pydantic.PydanticPanel",
"debug_toolbar.panels.routes.RoutesPanel",
"debug_toolbar.panels.logging.LoggingPanel",
"debug_toolbar.panels.profiling.ProfilingPanel",
Expand Down Expand Up @@ -129,11 +135,6 @@ class DebugToolbarSettings(BaseSettings):
),
)

class Config:
title = "Debug Toolbar"
env_prefix = "DT_"
case_sensitive = True

def __init__(self, **settings: t.Any) -> None:
super().__init__(**settings)
loaders = self.JINJA_LOADERS + [PackageLoader("debug_toolbar", "templates")]
Expand All @@ -144,6 +145,6 @@ def __init__(self, **settings: t.Any) -> None:
for extension in self.JINJA_EXTENSIONS:
self.JINJA_ENV.add_extension(extension)

@root_validator(pre=True)
def ci(cls, values: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
return {k.upper(): v for k, v in values.items()}
@model_validator(mode="before")
def ci(cls, data: dict):
return {k.upper(): v for k, v in data.items()}
34 changes: 0 additions & 34 deletions debug_toolbar/templates/panels/pydantic.html

This file was deleted.

2 changes: 1 addition & 1 deletion debug_toolbar/templates/panels/settings.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro pprint_settings(settings, exclude=None) %}
{% if settings.__config__.title %}<h4>{{ settings.__config__.title }}</h4>{% endif %}
{% if settings.model_config.title %}<h4>{{ settings.model_config.title }}</h4>{% endif %}

<table>
<thead>
Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from fastapi import Request
from fastapi.routing import APIRoute
from pydantic.color import Color
from pydantic_extra_types.color import Color
from starlette.routing import Match


Expand Down
Binary file removed docs/img/panels/Pydantic.png
Binary file not shown.
4 changes: 0 additions & 4 deletions docs/panels/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ Add your pydantic's [BaseSettings](https://pydantic-docs.helpmanual.io/usage/set

![Headers panel](../img/panels/Headers.png)

## Pydantic

![Pydantic panel](../img/panels/Pydantic.png)

## Routes

![Routes panel](../img/panels/Routes.png)
Expand Down
3 changes: 2 additions & 1 deletion docs/src/panels/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from debug_toolbar.middleware import DebugToolbarMiddleware
from fastapi import FastAPI
from pydantic import BaseSettings, SecretStr
from pydantic import SecretStr
from pydantic_settings import BaseSettings


class APISettings(BaseSettings):
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ dependencies = [
"fastapi >=0.70.0",
"anyio >=3.0.0",
"Jinja2 >=2.9",
"pydantic >=2.0",
"pydantic-extra-types >=2.0.0",
"pydantic-settings >=2.0.0",
"pyinstrument >=3.0.0",
"sqlparse >=0.2.0",
]
Expand Down
10 changes: 0 additions & 10 deletions tests/panels/test_pydantic.py

This file was deleted.

0 comments on commit c87586b

Please sign in to comment.