Skip to content

Commit

Permalink
Fix python3.10 problem with Self type
Browse files Browse the repository at this point in the history
  • Loading branch information
berrydenhartog committed May 8, 2024
1 parent 76a2f2e commit d03903e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ __pypackages__/

#mypyr
.mypy_cache/

# macos
.DS_Store
9 changes: 6 additions & 3 deletions tad/core/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import secrets
import warnings
from typing import Annotated, Any, Literal, Self
from typing import Annotated, Any, Literal, TypeVar

from pydantic import (
AnyUrl,
Expand All @@ -13,6 +13,9 @@

from tad.core.exceptions import SettingsError

# Self type is not available in Python 3.10 so create our own with TypeVar
SelfSettings = TypeVar("SelfSettings", bound="Settings")


def parse_cors(v: Any) -> list[str] | str:
if isinstance(v, str) and not v.startswith("["):
Expand Down Expand Up @@ -81,14 +84,14 @@ def _check_default_secret(self, var_name: str, value: str | None) -> None:
raise SettingsError(message)

@model_validator(mode="after")
def _enforce_non_default_secrets(self) -> Self:
def _enforce_non_default_secrets(self: SelfSettings) -> SelfSettings:
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
self._check_default_secret("APP_DATABASE_PASSWORD", self.APP_DATABASE_PASSWORD)

return self

@model_validator(mode="after")
def _enforce_database_rules(self) -> Self:
def _enforce_database_rules(self: SelfSettings) -> SelfSettings:
if self.ENVIRONMENT != "local" and self.APP_DATABASE_SCHEME == "sqlite":
raise SettingsError("SQLite is not supported in production") # noqa: TRY003
return self
Expand Down
3 changes: 1 addition & 2 deletions tad/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

from tad.main import app

# todo(berry): add database fixtures


# todo(berry): add database fixtures
@pytest.fixture(scope="module")
def client() -> Generator[TestClient, None, None]:
with TestClient(app, raise_server_exceptions=True) as c:
Expand Down

0 comments on commit d03903e

Please sign in to comment.