@@ -139,7 +139,7 @@ class WithSerializationContext(ABC):
139139 during serialization and deserialization.
140140 """
141141
142- def with_context (self , context : Optional [ SerializationContext ] ) -> Self :
142+ def with_context (self , context : SerializationContext ) -> Self :
143143 """Return a copy of this object configured to use the given context.
144144
145145 Args:
@@ -401,7 +401,7 @@ def from_payloads(
401401 ) from err
402402 return values
403403
404- def with_context (self , context : Optional [ SerializationContext ] ) -> Self :
404+ def with_context (self , context : SerializationContext ) -> Self :
405405 """Return a new instance with the given context."""
406406 converters = [
407407 c .with_context (context ) if isinstance (c , WithSerializationContext ) else c
@@ -1313,12 +1313,13 @@ def _with_context(self, context: Optional[SerializationContext]) -> Self:
13131313 payload_converter = self .payload_converter
13141314 payload_codec = self .payload_codec
13151315 failure_converter = self .failure_converter
1316- if isinstance (payload_converter , WithSerializationContext ):
1317- payload_converter = payload_converter .with_context (context )
1318- if isinstance (payload_codec , WithSerializationContext ):
1319- payload_codec = payload_codec .with_context (context )
1320- if isinstance (failure_converter , WithSerializationContext ):
1321- failure_converter = failure_converter .with_context (context )
1316+ if context :
1317+ if isinstance (payload_converter , WithSerializationContext ):
1318+ payload_converter = payload_converter .with_context (context )
1319+ if isinstance (payload_codec , WithSerializationContext ):
1320+ payload_codec = payload_codec .with_context (context )
1321+ if isinstance (failure_converter , WithSerializationContext ):
1322+ failure_converter = failure_converter .with_context (context )
13221323 object .__setattr__ (cloned , "payload_converter" , payload_converter )
13231324 object .__setattr__ (cloned , "payload_codec" , payload_codec )
13241325 object .__setattr__ (cloned , "failure_converter" , failure_converter )
0 commit comments