You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Since https://github.com/airtai/faststream/pull/1667/files, when trying to use a custom Context field that implements __eq__ returning NotImplemented in some cases, the dependency is not injected in subscriber.
Here is what happens when doing the comparison with EMPTY:
User.__eq__ is called, returning NotImplementError
So, the comparison User(1) != EMPTY evaluates to not(NotImplementedError) which is False, leading to not injecting user in kwargs.
I think that _EmptyPlaceHolder.__eq__ should return False instead of NotImplementedError when compared with an object which is not an _EmptyPlaceHolder object, because this kind of comparison is what would most commonly happen at
, and the fact is that any object that is not an _EmptyPlaceHolder instance should pass the test.
Additionaly, I think this comparison should be made with EMPTY as the lhs operand (i.e. EMPTY != ...), as this will ensure that _EmptyPlaceHolder.__eq__ is called first and will always return a result (True if the rhs is an _EmptyPlaceHolder object, False otherwise), avoiding potential side effects depending on implementation of __eq__ in the compared object
The text was updated successfully, but these errors were encountered:
Describe the bug
Since https://github.com/airtai/faststream/pull/1667/files, when trying to use a custom Context field that implements __eq__ returning
NotImplemented
in some cases, the dependency is not injected in subscriber.How to reproduce
Having following User class:
Having a middleware injecting a User object in the context:
Having a subscriber with
user
from context as a dependency:The
user
parameter is not injected leading to a pydantic validation error because field is missing.The issue arises because this (
faststream/faststream/utils/context/types.py
Line 63 in 689f674
if
statement always evaluates toFalse
.Here is what happens when doing the comparison with
EMPTY
:User.__eq__
is called, returningNotImplementError
User(1) != EMPTY
evaluates tonot(NotImplementedError)
which isFalse
, leading to not injectinguser
in kwargs.I think that
_EmptyPlaceHolder.__eq__
should returnFalse
instead ofNotImplementedError
when compared with an object which is not an_EmptyPlaceHolder
object, because this kind of comparison is what would most commonly happen atfaststream/faststream/utils/context/types.py
Line 63 in 689f674
_EmptyPlaceHolder
instance should pass the test.Additionaly, I think this comparison should be made with
EMPTY
as the lhs operand (i.e.EMPTY != ...
), as this will ensure that_EmptyPlaceHolder.__eq__
is called first and will always return a result (True
if the rhs is an_EmptyPlaceHolder
object,False
otherwise), avoiding potential side effects depending on implementation of__eq__
in the compared objectThe text was updated successfully, but these errors were encountered: