Skip to content

Commit

Permalink
Extras - Cleaning up (#329)
Browse files Browse the repository at this point in the history
* Clean transformers
* Rename EsmeraldSignature to SignatureModel
* Rewrite the signature.py
* Fix transformer model dependencies
* Fix warnings for check_password
* Add more tests to encoders
  • Loading branch information
tarsil committed May 21, 2024
1 parent 91737a9 commit fa332bf
Show file tree
Hide file tree
Showing 20 changed files with 834 additions and 360 deletions.
8 changes: 8 additions & 0 deletions esmerald/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,14 @@ def extend(self, config: PluggableConfig) -> None:
self._configure()

def _register_application_encoders(self) -> None:
"""
Registers the default Esmerald encoders.
This allows backwards compatibility with the previous
versions of Esmerald supporting Pydantic and MsgSpec by default.
This way, the support still remains but using the Lilya Encoders.
"""
self.register_encoder(cast(Encoder[Any], PydanticEncoder))
self.register_encoder(cast(Encoder[Any], MsgSpecEncoder))

Expand Down
2 changes: 1 addition & 1 deletion esmerald/contrib/auth/edgy/base_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def setter(raw_password: str) -> None:
self._password = None
await self.update(password=self.password)

return check_password(raw_password, str(self.password), setter)
return await check_password(raw_password, str(self.password), setter)

async def set_unusable_password(self) -> None:
# Set a value that will never be a valid hash
Expand Down
4 changes: 2 additions & 2 deletions esmerald/contrib/auth/hashers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def is_password_usable(encoded: Optional[str]) -> bool:
return encoded is None or not encoded.startswith(UNUSABLE_PASSWORD_PREFIX)


def check_password(
async def check_password(
password: str,
encoded: str,
setter: Callable[..., Any] = None,
Expand Down Expand Up @@ -54,7 +54,7 @@ def check_password(
is_correct: bool = hasher_handler.hasher.verify(password, encoded)

if setter and is_correct and must_update:
setter(password)
await setter(password)
return is_correct


Expand Down
2 changes: 1 addition & 1 deletion esmerald/contrib/auth/mongoz/base_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def setter(raw_password: str) -> None:
self._password = None
await self.update(password=self.password)

return check_password(raw_password, str(self.password), setter)
return await check_password(raw_password, str(self.password), setter)

async def set_unusable_password(self) -> None:
# Set a value that will never be a valid hash
Expand Down
2 changes: 1 addition & 1 deletion esmerald/contrib/auth/saffier/base_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def setter(raw_password: str) -> None:
self._password = None
await self.update(password=self.password)

return check_password(raw_password, str(self.password), setter)
return await check_password(raw_password, str(self.password), setter)

async def set_unusable_password(self) -> None:
# Set a value that will never be a valid hash
Expand Down
11 changes: 9 additions & 2 deletions esmerald/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ def is_type(self, value: Any) -> bool:
Function that checks if the function is
an instance of a given type
"""
raise NotImplementedError("All Esmerald encoders must implement is_type() method")
raise NotImplementedError("All Esmerald encoders must implement is_type() method.")

def serialize(self, obj: Any) -> Any:
"""
Function that transforms a data structure into a serializable
object.
"""
raise NotImplementedError("All Esmerald encoders must implement serialize() method.")

def encode(self, annotation: Any, value: Any) -> Any:
"""
Function that transforms the kwargs into a structure
"""
raise NotImplementedError("All Esmerald encoders must implement encode() method")
raise NotImplementedError("All Esmerald encoders must implement encode() method.")


class MsgSpecEncoder(Encoder):
Expand Down
4 changes: 2 additions & 2 deletions esmerald/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from esmerald.core.di.provider import load_provider
from esmerald.parsers import ArbitraryHashableBaseModel
from esmerald.transformers.datastructures import Signature
from esmerald.transformers.signature import SignatureModel
from esmerald.typing import Void
from esmerald.utils.helpers import is_async_callable

Expand Down Expand Up @@ -56,7 +56,7 @@ class Inject(ArbitraryHashableBaseModel):
def __init__(self, dependency: "AnyCallable", use_cache: bool = False, **kwargs: Any):
super().__init__(**kwargs)
self.dependency = dependency
self.signature_model: Optional["Type[Signature]"] = None
self.signature_model: Optional["Type[SignatureModel]"] = None
self.use_cache = use_cache
self.value: Any = Void

Expand Down
2 changes: 1 addition & 1 deletion esmerald/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
from esmerald.routing.base import BaseInterceptorMixin
from esmerald.routing.events import handle_lifespan_events
from esmerald.routing.gateways import Gateway, WebhookGateway, WebSocketGateway
from esmerald.transformers.datastructures import EsmeraldSignature as SignatureModel
from esmerald.transformers.model import TransformerModel
from esmerald.transformers.signature import SignatureModel
from esmerald.transformers.utils import get_signature
from esmerald.typing import Void, VoidType
from esmerald.utils.constants import DATA, PAYLOAD, REDIRECT_STATUS_CODES, REQUEST, SOCKET
Expand Down
139 changes: 0 additions & 139 deletions esmerald/transformers/datastructures.py

This file was deleted.

Loading

0 comments on commit fa332bf

Please sign in to comment.