Skip to content

Commit

Permalink
fix: let model_validator return self (#205)
Browse files Browse the repository at this point in the history
* fix: let `model_validator` return `self`

UserWarning: A custom validator is returning a value other than `self`.
Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.
See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.

* fix mypy

---------

Co-authored-by: Konstantin <konstantin.klein+github@hochfrequenz.de>
  • Loading branch information
hf-kklein and Konstantin authored Jan 13, 2025
1 parent fcc5138 commit 0fbed1d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/tmdsclient/client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
contains a class with which the TMDS client is instantiated/configured
"""

from typing import Any
from typing import Any, Self

from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator, model_validator
from yarl import URL
Expand Down Expand Up @@ -89,23 +89,24 @@ class OAuthTmdsConfig(TmdsConfig):
This is useful when you have ways to get a token but not a client id and secret.
"""

@model_validator(mode="after") # type:ignore[misc, no-untyped-def]
def check_secret_or_token_is_present(cls, values) -> None: # pylint:disable=no-self-argument
@model_validator(mode="after")
def check_secret_or_token_is_present(self) -> Self: # pylint:disable=no-self-argument
"""
Ensures that either (id+secret) or a bare token are present
"""
token_is_present = values.bearer_token is not None and values.bearer_token.strip()
token_is_present = self.bearer_token is not None and self.bearer_token.strip()
client_id_and_secret_are_present = (
values.client_id is not None
and values.client_id.strip()
and values.client_secret is not None
and values.client_secret.strip()
self.client_id is not None
and self.client_id.strip()
and self.client_secret is not None
and self.client_secret.strip()
)
if not token_is_present and not client_id_and_secret_are_present:
raise ValueError(
# pylint:disable=line-too-long
"You must provide either client id and secret or a bearer token, but not None of both"
)
return self

@field_validator("bearer_token")
def validate_bearer_token(cls, value: str) -> str:
Expand Down

0 comments on commit 0fbed1d

Please sign in to comment.