Skip to content

Commit 4f914b5

Browse files
committed
fixing the minor bugs
1 parent fbb9d81 commit 4f914b5

File tree

7 files changed

+111
-114
lines changed

7 files changed

+111
-114
lines changed

src/dubbo/client.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
# limitations under the License.
1616

1717
import threading
18-
from typing import Optional
18+
from typing import Optional, List, Type
1919

2020
from dubbo.bootstrap import Dubbo
2121
from dubbo.classes import MethodDescriptor
22-
from dubbo.codec import DubboTransportService
2322
from dubbo.configs import ReferenceConfig
2423
from dubbo.constants import common_constants
2524
from dubbo.extension import extensionLoader
@@ -33,6 +32,7 @@
3332
SerializingFunction,
3433
)
3534
from dubbo.url import URL
35+
from dubbo.codec import DubboSerializationService
3636

3737
__all__ = ["Client"]
3838

@@ -88,21 +88,22 @@ def _create_rpc_callable(
8888
self,
8989
rpc_type: str,
9090
method_name: str,
91-
params_types: list[type],
92-
return_type: type,
91+
params_types: List[Type],
92+
return_type: Type,
9393
codec: Optional[str] = None,
9494
request_serializer: Optional[SerializingFunction] = None,
9595
response_deserializer: Optional[DeserializingFunction] = None,
9696
) -> RpcCallable:
9797
"""
9898
Create RPC callable with the specified type.
9999
"""
100+
print("2", params_types)
100101
# Determine serializers
101102
if request_serializer and response_deserializer:
102103
req_ser = request_serializer
103104
res_deser = response_deserializer
104105
else:
105-
req_ser, res_deser = DubboTransportService.create_serialization_functions(
106+
req_ser, res_deser = DubboSerializationService.create_serialization_functions(
106107
codec or "json",
107108
parameter_types=params_types,
108109
return_type=return_type,
@@ -118,7 +119,8 @@ def _create_rpc_callable(
118119

119120
return self._callable(descriptor)
120121

121-
def unary(self, method_name: str, params_types: list[type], return_type: type, **kwargs) -> RpcCallable:
122+
def unary(self, method_name: str, params_types: List[Type], return_type: Type, **kwargs) -> RpcCallable:
123+
print("1", params_types)
122124
return self._create_rpc_callable(
123125
rpc_type=RpcTypes.UNARY.value,
124126
method_name=method_name,
@@ -127,7 +129,7 @@ def unary(self, method_name: str, params_types: list[type], return_type: type, *
127129
**kwargs,
128130
)
129131

130-
def client_stream(self, method_name: str, params_types: list[type], return_type: type, **kwargs) -> RpcCallable:
132+
def client_stream(self, method_name: str, params_types: List[Type], return_type: Type, **kwargs) -> RpcCallable:
131133
return self._create_rpc_callable(
132134
rpc_type=RpcTypes.CLIENT_STREAM.value,
133135
method_name=method_name,
@@ -136,7 +138,7 @@ def client_stream(self, method_name: str, params_types: list[type], return_type:
136138
**kwargs,
137139
)
138140

139-
def server_stream(self, method_name: str, params_types: list[type], return_type: type, **kwargs) -> RpcCallable:
141+
def server_stream(self, method_name: str, params_types: List[Type], return_type: Type, **kwargs) -> RpcCallable:
140142
return self._create_rpc_callable(
141143
rpc_type=RpcTypes.SERVER_STREAM.value,
142144
method_name=method_name,
@@ -145,7 +147,7 @@ def server_stream(self, method_name: str, params_types: list[type], return_type:
145147
**kwargs,
146148
)
147149

148-
def bi_stream(self, method_name: str, params_types: list[type], return_type: type, **kwargs) -> RpcCallable:
150+
def bi_stream(self, method_name: str, params_types: List[Type], return_type: Type, **kwargs) -> RpcCallable:
149151
return self._create_rpc_callable(
150152
rpc_type=RpcTypes.BI_STREAM.value,
151153
method_name=method_name,

src/dubbo/codec/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from .dubbo_codec import DubboTransportService
17+
from .dubbo_codec import DubboSerializationService
1818

19-
__all__ = ["DubboTransportService"]
19+
__all__ = ["DubboSerializationService"]

src/dubbo/codec/dubbo_codec.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
@dataclass
2626
class ParameterDescriptor:
27-
"""Detailed information about a method parameter"""
27+
"""Information about a method parameter"""
2828

2929
name: str
3030
annotation: Any
@@ -34,7 +34,7 @@ class ParameterDescriptor:
3434

3535
@dataclass
3636
class MethodDescriptor:
37-
"""Complete method descriptor with all necessary information"""
37+
"""Method descriptor with function details"""
3838

3939
function: Callable
4040
name: str
@@ -44,13 +44,16 @@ class MethodDescriptor:
4444

4545

4646
class DubboSerializationService:
47-
"""Dubbo serialization service with robust type handling"""
47+
"""Dubbo serialization service with type handling"""
4848

4949
@staticmethod
5050
def create_transport_codec(
51-
transport_type: str = "json", parameter_types: list[type] = None, return_type: type = None, **codec_options
51+
transport_type: str = "json",
52+
parameter_types: Optional[list[type]] = None,
53+
return_type: Optional[type] = None,
54+
**codec_options,
5255
):
53-
"""Create transport codec with enhanced parameter structure"""
56+
"""Create transport codec"""
5457

5558
try:
5659
from dubbo.classes import CodecHelper
@@ -67,13 +70,19 @@ def create_transport_codec(
6770

6871
@staticmethod
6972
def create_encoder_decoder_pair(
70-
transport_type: str, parameter_types: list[type] = None, return_type: type = None, **codec_options
73+
transport_type: str,
74+
parameter_types: Optional[list[type]] = None,
75+
return_type: Optional[type] = None,
76+
**codec_options,
7177
) -> tuple[Any, Any]:
72-
"""Create separate encoder and decoder instances"""
78+
"""Create encoder and decoder instances"""
7379

7480
try:
7581
codec_instance = DubboSerializationService.create_transport_codec(
76-
transport_type=transport_type, parameter_types=parameter_types, return_type=return_type, **codec_options
82+
transport_type=transport_type,
83+
parameter_types=parameter_types,
84+
return_type=return_type,
85+
**codec_options,
7786
)
7887

7988
encoder = codec_instance.get_encoder()
@@ -90,13 +99,19 @@ def create_encoder_decoder_pair(
9099

91100
@staticmethod
92101
def create_serialization_functions(
93-
transport_type: str, parameter_types: list[type] = None, return_type: type = None, **codec_options
102+
transport_type: str,
103+
parameter_types: Optional[list[type]] = None,
104+
return_type: Optional[type] = None,
105+
**codec_options,
94106
) -> tuple[Callable, Callable]:
95-
"""Create serializer and deserializer functions for RPC (backward compatibility)"""
107+
"""Create serializer and deserializer functions"""
96108

97109
try:
98110
parameter_encoder, return_decoder = DubboSerializationService.create_encoder_decoder_pair(
99-
transport_type=transport_type, parameter_types=parameter_types, return_type=return_type, **codec_options
111+
transport_type=transport_type,
112+
parameter_types=parameter_types,
113+
return_type=return_type,
114+
**codec_options,
100115
)
101116

102117
def serialize_method_parameters(*args) -> bytes:
@@ -125,17 +140,17 @@ def deserialize_method_return(data: bytes):
125140
def create_method_descriptor(
126141
func: Callable,
127142
method_name: Optional[str] = None,
128-
parameter_types: list[type] = None,
129-
return_type: type = None,
130-
interface: Callable = None,
143+
parameter_types: Optional[list[type]] = None,
144+
return_type: Optional[type] = None,
145+
interface: Optional[Callable[..., Any]] = None,
131146
) -> MethodDescriptor:
132147
"""Create a method descriptor from function and configuration"""
133148

134149
if not callable(func):
135150
raise TypeError("func must be callable")
136151

137152
# Use interface signature if provided, otherwise use func signature
138-
target_function = interface if interface else func
153+
target_function = interface if interface is not None else func
139154
name = method_name or target_function.__name__
140155

141156
try:
@@ -166,14 +181,17 @@ def create_method_descriptor(
166181

167182
parameters.append(
168183
ParameterDescriptor(
169-
name=param_name, annotation=param_type, is_required=is_required, default_value=default_value
184+
name=param_name,
185+
annotation=param_type,
186+
is_required=is_required,
187+
default_value=default_value,
170188
)
171189
)
172190

173191
param_index += 1
174192

175193
# Resolve return type
176-
if return_type:
194+
if return_type is not None:
177195
resolved_return_type = return_type
178196
elif sig.return_annotation != inspect.Signature.empty:
179197
resolved_return_type = sig.return_annotation

src/dubbo/codec/json_codec/json_transport_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from typing import Any, Callable, Protocol
16+
from typing import Any, Callable, Protocol, Optional
1717

1818

1919
class JsonSerializerPlugin(Protocol):
@@ -36,18 +36,18 @@ class SimpleRegistry:
3636

3737
def __init__(self):
3838
# Simple dict mapping: type -> handler function
39-
self.type_handlers: dict[type, Callable] = {}
39+
self.type_handlers: dict[type, Callable[..., Any]] = {}
4040
self.plugins: list[TypeHandlerPlugin] = []
4141

42-
def register_type_handler(self, obj_type: type, handler: Callable):
42+
def register_type_handler(self, obj_type: type, handler: Callable[..., Any]) -> None:
4343
"""Register a simple type handler function"""
4444
self.type_handlers[obj_type] = handler
4545

46-
def register_plugin(self, plugin: TypeHandlerPlugin):
46+
def register_plugin(self, plugin: TypeHandlerPlugin) -> None:
4747
"""Register a plugin"""
4848
self.plugins.append(plugin)
4949

50-
def get_handler(self, obj: Any) -> Callable:
50+
def get_handler(self, obj: Any) -> Optional[Callable[..., Any]]:
5151
"""Get handler for object - check dict first, then plugins"""
5252
obj_type = type(obj)
5353
if obj_type in self.type_handlers:

src/dubbo/codec/json_codec/json_transport_codec.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
from pathlib import Path
2424
from typing import Any, Union
2525
from uuid import UUID
26+
from typing import Optional
2627

2728

2829
class StandardJsonPlugin:
29-
"""Standard library JSON plugin"""
30+
"""Standard library JSON codec"""
3031

3132
def encode(self, obj: Any) -> bytes:
3233
return json.dumps(obj, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
@@ -39,7 +40,7 @@ def can_handle(self, obj: Any) -> bool:
3940

4041

4142
class OrJsonPlugin:
42-
"""orjson plugin independent implementation"""
43+
"""orjson Codec independent implementation"""
4344

4445
def __init__(self):
4546
try:
@@ -131,12 +132,12 @@ def _default_handler(self, obj):
131132

132133

133134
class DateTimeHandler:
134-
"""DateTime handler - implements TypeHandlerPlugin protocol"""
135+
"""DateTime handler - implements TypeHandler Codec"""
135136

136137
def can_serialize_type(self, obj: Any, obj_type: type) -> bool:
137138
return isinstance(obj, (datetime, date, time))
138139

139-
def serialize_to_dict(self, obj: Union[datetime, date, time]) -> dict[str, str]:
140+
def serialize_to_dict(self, obj: Union[datetime, date, time]) -> dict[str, str | None]:
140141
if isinstance(obj, datetime):
141142
return {"__datetime__": obj.isoformat(), "__timezone__": str(obj.tzinfo) if obj.tzinfo else None}
142143
elif isinstance(obj, date):
@@ -193,7 +194,7 @@ def serialize_to_dict(self, obj: Union[UUID, Path]) -> dict[str, str]:
193194

194195

195196
class PydanticHandler:
196-
"""Separate Pydantic plugin with enhanced features"""
197+
"""Pydantic codec for handling advance serialization"""
197198

198199
def __init__(self):
199200
try:
@@ -219,9 +220,9 @@ def serialize_to_dict(self, obj) -> dict[str, Any]:
219220
"__model_data__": obj.dict(),
220221
}
221222

222-
def create_parameter_model(self, parameter_types: list[type]):
223+
def create_parameter_model(self, parameter_types: Optional[list[type]] = None):
223224
"""Enhanced parameter handling for both positional and keyword args"""
224-
if not self.available:
225+
if not self.available or parameter_types is None:
225226
return None
226227

227228
model_fields = {}

0 commit comments

Comments
 (0)