2929_env .filters ["pascal" ] = _to_pascal_case
3030
3131
32+ def _result_type (proc ) -> str : # noqa: ANN001
33+ """Build the typed result annotation for a procedure."""
34+ ok = f"OkResult[{ proc .output_type .annotation } ]"
35+ if proc .error_type :
36+ err = f"ErrResult[{ proc .error_type .annotation } | ProtocolError]"
37+ else :
38+ err = "ErrResult[ProtocolError]"
39+ return f"{ ok } | { err } "
40+
41+
42+ _env .filters ["result_type" ] = _result_type
43+
44+
3245# ---------------------------------------------------------------------------
3346# Helpers
3447# ---------------------------------------------------------------------------
@@ -51,6 +64,9 @@ def _collect_used_type_names(svc: ServiceDef, ir: SchemaIR) -> list[str]:
5164 _extract_names (proc .init_type .annotation , td_names , names )
5265 if proc .input_type :
5366 _extract_names (proc .input_type .annotation , td_names , names )
67+ _extract_names (proc .output_type .annotation , td_names , names )
68+ if proc .error_type :
69+ _extract_names (proc .error_type .annotation , td_names , names )
5470
5571 return sorted (names )
5672
@@ -110,22 +126,20 @@ def render_service_client(svc: ServiceDef, ir: SchemaIR, import_prefix: str) ->
110126 type_names = _collect_used_type_names (svc , ir )
111127 types_module = "._types" if import_prefix == "." else f"{ import_prefix } _types"
112128
113- needs_readable = any (
114- p .proc_type in ("stream" , "subscription" ) for p in svc .procedures
115- )
116- needs_writable = any (p .proc_type in ("stream" , "upload" ) for p in svc .procedures )
117-
118- wrappers = [
119- p for p in svc .procedures if p .proc_type in ("stream" , "upload" , "subscription" )
120- ]
129+ proc_types = {p .proc_type for p in svc .procedures }
130+ has_rpc = "rpc" in proc_types
131+ has_stream = "stream" in proc_types
132+ has_upload = "upload" in proc_types
133+ has_subscription = "subscription" in proc_types
121134
122135 return _env .get_template ("service_client.py.j2" ).render (
123136 service = svc ,
124137 type_names = type_names ,
125138 types_module = types_module ,
126- needs_readable = needs_readable ,
127- needs_writable = needs_writable ,
128- wrappers = wrappers ,
139+ has_rpc = has_rpc ,
140+ has_stream = has_stream ,
141+ has_upload = has_upload ,
142+ has_subscription = has_subscription ,
129143 )
130144
131145
0 commit comments