polyapi-python 0.3.20.dev3__tar.gz → 0.3.21__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/PKG-INFO +18 -3
  2. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/README.md +7 -0
  3. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/ai_function.py +1 -2
  4. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/cli.py +0 -17
  5. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/cli_constants.py +0 -1
  6. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/deployables.py +6 -24
  7. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/error_handler.py +7 -5
  8. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/generate.py +9 -9
  9. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/http_client.py +66 -2
  10. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/parser.py +74 -18
  11. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/poly_tables.py +9 -2
  12. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/prepare.py +25 -7
  13. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/server.py +1 -2
  14. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/sync.py +2 -2
  15. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/utils.py +11 -4
  16. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/variables.py +6 -4
  17. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/webhook.py +2 -1
  18. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi_python.egg-info/PKG-INFO +18 -3
  19. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi_python.egg-info/SOURCES.txt +0 -2
  20. polyapi_python-0.3.21/polyapi_python.egg-info/requires.txt +18 -0
  21. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/pyproject.toml +14 -3
  22. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_async_proof.py +4 -3
  23. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_dependency_pins.py +3 -11
  24. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_deployables.py +10 -2
  25. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_generate.py +25 -13
  26. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_parser.py +4 -2
  27. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_server.py +6 -4
  28. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_utils.py +3 -1
  29. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_variables.py +3 -1
  30. polyapi_python-0.3.20.dev3/polyapi/rendered_spec.py +0 -69
  31. polyapi_python-0.3.20.dev3/polyapi_python.egg-info/requires.txt +0 -9
  32. polyapi_python-0.3.20.dev3/tests/test_rendered_spec.py +0 -52
  33. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/LICENSE +0 -0
  34. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/__init__.py +0 -0
  35. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/__main__.py +0 -0
  36. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/api.py +0 -0
  37. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/auth.py +0 -0
  38. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/client.py +0 -0
  39. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/config.py +0 -0
  40. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/constants.py +0 -0
  41. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/exceptions.py +0 -0
  42. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/execute.py +0 -0
  43. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/function_cli.py +0 -0
  44. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/poly_schemas.py +0 -0
  45. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/py.typed +0 -0
  46. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/schema.py +0 -0
  47. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi/typedefs.py +0 -0
  48. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi_python.egg-info/dependency_links.txt +0 -0
  49. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/polyapi_python.egg-info/top_level.txt +0 -0
  50. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/setup.cfg +0 -0
  51. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_api.py +0 -0
  52. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_auth.py +0 -0
  53. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_cli.py +0 -0
  54. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_generated_poly_boundary.py +0 -0
  55. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_poly_custom.py +0 -0
  56. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_poly_schemas.py +0 -0
  57. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_schema.py +0 -0
  58. {polyapi_python-0.3.20.dev3 → polyapi_python-0.3.21}/tests/test_tabi.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.20.dev3
3
+ Version: 0.3.21
4
4
  Summary: The Python SDK for the integration platform PolyAPI
5
5
  Author-email: PolyAPI <support@polyapi.io>
6
6
  License: MIT License
@@ -28,15 +28,23 @@ Project-URL: Homepage, https://github.com/polyapi/polyapi-python
28
28
  Requires-Python: >=3.10
29
29
  Description-Content-Type: text/markdown
30
30
  License-File: LICENSE
31
- Requires-Dist: requests==2.32.3
31
+ Requires-Dist: requests==2.33.0
32
32
  Requires-Dist: typing_extensions==4.15.0
33
33
  Requires-Dist: jsonschema-gentypes==2.13.0
34
34
  Requires-Dist: pydantic==2.13.4
35
35
  Requires-Dist: stdlib_list==0.11.1
36
36
  Requires-Dist: colorama==0.4.4
37
- Requires-Dist: python-socketio[asyncio_client]==5.11.1
37
+ Requires-Dist: python-socketio[asyncio_client]==5.16.2
38
38
  Requires-Dist: truststore==0.9.1
39
39
  Requires-Dist: httpx==0.28.1
40
+ Provides-Extra: dev
41
+ Requires-Dist: mock==5.2.0; extra == "dev"
42
+ Requires-Dist: pytest; extra == "dev"
43
+ Requires-Dist: flask==3.1.3; extra == "dev"
44
+ Requires-Dist: mypy; extra == "dev"
45
+ Requires-Dist: types-requests; extra == "dev"
46
+ Requires-Dist: types-colorama; extra == "dev"
47
+ Requires-Dist: pip-audit; extra == "dev"
40
48
  Dynamic: license-file
41
49
 
42
50
  # PolyAPI Python Library
@@ -98,10 +106,17 @@ client per event loop. New clients read these optional environment variables:
98
106
  | `POLY_HTTP_READ_TIMEOUT` | `none` | Read timeout in seconds |
99
107
  | `POLY_HTTP_WRITE_TIMEOUT` | `none` | Write timeout in seconds |
100
108
  | `POLY_HTTP_POOL_TIMEOUT` | `none` | Connection-pool timeout in seconds |
109
+ | `POLY_HTTP_CLOSE_TIMEOUT` | `5` | Deadline in seconds for closing a client's pool |
101
110
 
102
111
  An empty value, `none`, or `null` means unbounded. Limits are per client, so
103
112
  processes with multiple event loops can create one configured pool per loop.
104
113
 
114
+ `POLY_HTTP_CLOSE_TIMEOUT` is read at shutdown rather than at client creation.
115
+ It bounds pool teardown so a single unresponsive socket cannot stall shutdown:
116
+ if the deadline passes, the SDK logs a warning, abandons the remaining
117
+ connections to the operating system, and retires the client so it is never
118
+ reused. Set it to `none` to wait indefinitely.
119
+
105
120
  Call `polyapi.http_client.close()` during synchronous shutdown or await
106
121
  `polyapi.http_client.close_async()` on each owning event loop during
107
122
  asynchronous shutdown. The shutdown functions wait for active SDK requests
@@ -57,10 +57,17 @@ client per event loop. New clients read these optional environment variables:
57
57
  | `POLY_HTTP_READ_TIMEOUT` | `none` | Read timeout in seconds |
58
58
  | `POLY_HTTP_WRITE_TIMEOUT` | `none` | Write timeout in seconds |
59
59
  | `POLY_HTTP_POOL_TIMEOUT` | `none` | Connection-pool timeout in seconds |
60
+ | `POLY_HTTP_CLOSE_TIMEOUT` | `5` | Deadline in seconds for closing a client's pool |
60
61
 
61
62
  An empty value, `none`, or `null` means unbounded. Limits are per client, so
62
63
  processes with multiple event loops can create one configured pool per loop.
63
64
 
65
+ `POLY_HTTP_CLOSE_TIMEOUT` is read at shutdown rather than at client creation.
66
+ It bounds pool teardown so a single unresponsive socket cannot stall shutdown:
67
+ if the deadline passes, the SDK logs a warning, abandons the remaining
68
+ connections to the operating system, and retires the client so it is never
69
+ reused. Set it to `none` to wait indefinitely.
70
+
64
71
  Call `polyapi.http_client.close()` during synchronous shutdown or await
65
72
  `polyapi.http_client.close_async()` on each owning event loop during
66
73
  asynchronous shutdown. The shutdown functions wait for active SDK requests
@@ -75,8 +75,7 @@ def render_ai_function(
75
75
 
76
76
 
77
77
  def _normalize_return_type_for_annotation(function_name: str, return_type_name: str) -> str:
78
- if return_type_name == "ReturnType":
79
- return "ReturnType"
78
+ # "ReturnType" lives in a separate module, so it must be qualified (e.g. _SFX.ReturnType).
80
79
  return add_type_import_path(function_name, return_type_name)
81
80
 
82
81
 
@@ -9,7 +9,6 @@ from .cli_constants import CLI_COMMANDS
9
9
  from .config import initialize_config, set_api_key_and_url
10
10
  from .generate import generate, clear
11
11
  from .function_cli import function_add_or_update, function_execute
12
- from .rendered_spec import get_and_update_rendered_spec
13
12
  from .prepare import prepare_deployables
14
13
  from .sync import sync_deployables
15
14
 
@@ -196,22 +195,6 @@ def execute_from_cli():
196
195
  clear_parser.set_defaults(command=clear_command)
197
196
 
198
197
 
199
- ###########################################################################
200
- # Update rendered spec command
201
- update_spec_parser = subparsers.add_parser("update_rendered_spec", help="Update the rendered spec file")
202
- update_spec_parser.add_argument("spec", help="Specification file to update")
203
-
204
- def update_rendered_spec(args):
205
- updated = get_and_update_rendered_spec(args.spec)
206
- if updated:
207
- print("Updated rendered spec!")
208
- else:
209
- print("Failed to update rendered spec!")
210
- exit(1)
211
-
212
- update_spec_parser.set_defaults(command=update_rendered_spec)
213
-
214
-
215
198
  ###########################################################################
216
199
  # Prepare command
217
200
  prepare_parser = subparsers.add_parser('prepare', help="Find and prepare all Poly deployables")
@@ -4,7 +4,6 @@ CLI_COMMANDS = (
4
4
  "function",
5
5
  "clear",
6
6
  "help",
7
- "update_rendered_spec",
8
7
  "prepare",
9
8
  "sync",
10
9
  )
@@ -51,7 +51,7 @@ class DeployableFunctionTypes(TypedDict):
51
51
  params: List[DeployableFunctionParam]
52
52
  returns: DeployableFunctionParamBase
53
53
 
54
- class DeployableRecord(ParsedDeployableConfig, total=False):
54
+ class DeployableRecord(ParsedDeployableConfig):
55
55
  gitRevision: str
56
56
  fileRevision: str
57
57
  file: str
@@ -64,7 +64,7 @@ class DeployableRecord(ParsedDeployableConfig, total=False):
64
64
  docEndIndex: int
65
65
  dirty: Optional[bool]
66
66
 
67
- class SyncDeployment(TypedDict, total=False):
67
+ class SyncDeployment(TypedDict):
68
68
  context: str
69
69
  name: str
70
70
  description: str
@@ -195,30 +195,12 @@ def write_cache_revision(git_revision: Optional[str] = None) -> None:
195
195
  with open(CACHE_VERSION_FILE, 'w', encoding='utf-8') as file:
196
196
  file.write(git_revision)
197
197
 
198
- def is_cache_up_to_date() -> bool:
199
- if not Path(CACHE_VERSION_FILE).exists():
200
- return False
201
- with open(CACHE_VERSION_FILE, 'r', encoding='utf-8') as file:
202
- cached_revision = file.read().strip()
203
- git_revision = get_git_revision()
204
- return cached_revision == git_revision
205
-
206
198
  def is_cache_up_to_date() -> bool:
207
199
  """Check if the cached revision matches the current Git revision."""
208
200
  cached_revision = get_cache_deployments_revision()
209
201
  git_revision = get_git_revision() # This function needs to be defined or imported
210
202
  return cached_revision == git_revision
211
203
 
212
- def write_deploy_comments(deployments: List[Dict]) -> str:
213
- """Generate a string of deployment comments for each deployment."""
214
- canopy_path = 'polyui/collections' if 'localhost' in os.getenv('POLY_API_BASE_URL', '') else 'canopy/polyui/collections'
215
- comments = []
216
- for d in deployments:
217
- instance_url = d['instance'].replace(':8000', ':3000') if d['instance'].endswith(':8000') else d['instance']
218
- comment = f"# Poly deployed @ {d['deployed']} - {d['context']}.{d['name']} - {instance_url}/{canopy_path}/{d['type']}s/{d['id']} - {d['fileRevision']}"
219
- comments.append(comment)
220
- return '\n'.join(comments)
221
-
222
204
  def print_docstring_function_comment(description: str, args: list, returns: dict) -> str:
223
205
  docstring = f'"""{description}\n\n'
224
206
  if args:
@@ -243,7 +225,7 @@ def print_docstring_function_comment(description: str, args: list, returns: dict
243
225
  return docstring
244
226
 
245
227
 
246
- def update_deployment_comments(file_content: str, deployable: dict) -> str:
228
+ def update_deployment_comments(file_content: str, deployable: DeployableRecord) -> str:
247
229
  """
248
230
  Remove old deployment comments based on the provided ranges and add new ones.
249
231
  """
@@ -255,7 +237,7 @@ def update_deployment_comments(file_content: str, deployable: dict) -> str:
255
237
  file_content = f"{deployment_comments}\n{file_content}"
256
238
  return file_content
257
239
 
258
- def update_deployable_function_comments(file_content: str, deployable: dict, disable_docs: bool = False) -> str:
240
+ def update_deployable_function_comments(file_content: str, deployable: DeployableRecord, disable_docs: bool = False) -> str:
259
241
  """
260
242
  Update the docstring in the file content based on the deployable's documentation data.
261
243
  """
@@ -263,7 +245,7 @@ def update_deployable_function_comments(file_content: str, deployable: dict, dis
263
245
  docstring = print_docstring_function_comment(
264
246
  deployable['types']['description'],
265
247
  deployable['types']['params'],
266
- deployable['types']['returns']
248
+ deployable['types']['returns'] # pyright: ignore[reportArgumentType]
267
249
  )
268
250
  if deployable["docStartIndex"] == deployable["docEndIndex"]:
269
251
  # Function doesn't yet have any docstrings so we need to add additional whitespace
@@ -272,7 +254,7 @@ def update_deployable_function_comments(file_content: str, deployable: dict, dis
272
254
  return f"{file_content[:deployable['docStartIndex']]}{docstring}{file_content[deployable['docEndIndex']:]}"
273
255
  return file_content
274
256
 
275
- def write_updated_deployable(deployable: dict, disable_docs: bool = False) -> dict:
257
+ def write_updated_deployable(deployable: DeployableRecord, disable_docs: bool = False) -> DeployableRecord:
276
258
  """
277
259
  Read the deployable's file, update its comments and docstring, and write back to the file.
278
260
  """
@@ -42,7 +42,8 @@ async def unregister_all():
42
42
  _, base_url = get_api_key_and_url()
43
43
  # need to reconnect because maybe socketio client disconnected after Ctrl+C?
44
44
  try:
45
- await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
45
+ if client:
46
+ await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
46
47
  except ConnectionError:
47
48
  pass
48
49
  await asyncio.gather(*[unregister(handler) for handler in active_handlers])
@@ -63,10 +64,11 @@ async def on(
63
64
  data["apiKey"] = api_key
64
65
 
65
66
  def registerCallback(id: int):
66
- nonlocal handler_id
67
- handler_id = id
68
- client.on(f"handleError:{handler_id}", callback, namespace="/events")
69
- active_handlers.append({"path": path, "id": handler_id, "apiKey": api_key})
67
+ if client:
68
+ nonlocal handler_id
69
+ handler_id = id
70
+ client.on(f"handleError:{handler_id}", callback, namespace="/events")
71
+ active_handlers.append({"path": path, "id": handler_id, "apiKey": api_key})
70
72
 
71
73
  await client.emit("registerErrorHandler", data, "/events", registerCallback)
72
74
 
@@ -7,7 +7,7 @@ import tempfile
7
7
  import stat
8
8
 
9
9
  from copy import deepcopy
10
- from typing import Any, List, Optional, Tuple, cast
10
+ from typing import Any, Dict, List, Optional, Tuple, cast
11
11
 
12
12
  from .auth import render_auth_function
13
13
  from .client import render_client_function
@@ -104,7 +104,7 @@ def replace_poly_refs_in_functions(specs: List[SpecificationDto], schema_index):
104
104
  func = spec.get("function")
105
105
  if func:
106
106
  try:
107
- spec["function"] = resolve_poly_refs(func, schema_index)
107
+ spec["function"] = resolve_poly_refs(func, schema_index) # pyright: ignore[reportGeneralTypeIssues]
108
108
  except Exception:
109
109
  # print()
110
110
  # print(f"{spec['context']}.{spec['name']} (id: {spec['id']}) failed to resolve poly refs, skipping!")
@@ -123,7 +123,7 @@ def replace_poly_refs_in_schemas(specs: List[SchemaSpecDto], schema_index):
123
123
  spec_idxs_to_remove = []
124
124
  for idx, spec in enumerate(specs):
125
125
  try:
126
- spec["definition"] = resolve_poly_refs(spec["definition"], schema_index)
126
+ spec["definition"] = cast(Dict[str, Any], resolve_poly_refs(spec["definition"], schema_index))
127
127
  except Exception:
128
128
  # print()
129
129
  print(f"{spec['context']}.{spec['name']} (id: {spec['id']}) failed to resolve poly refs, skipping!")
@@ -445,16 +445,16 @@ def render_spec(spec: SpecificationDto) -> Tuple[str, str]:
445
445
  arguments: List[PropertySpecification] = []
446
446
  return_type: Any = {}
447
447
  if spec.get("function"):
448
- assert spec["function"]
448
+ function_spec = cast(Dict[str, Any], spec["function"]) # pyright: ignore[reportTypedDictNotRequiredAccess]
449
449
  # Handle cases where arguments might be missing or None
450
- if spec["function"].get("arguments"):
450
+ if function_spec.get("arguments"):
451
451
  arguments = [
452
- arg for arg in spec["function"]["arguments"]
452
+ arg for arg in function_spec["arguments"]
453
453
  ]
454
-
454
+
455
455
  # Handle cases where returnType might be missing or None
456
- if spec["function"].get("returnType"):
457
- return_type = spec["function"]["returnType"]
456
+ if function_spec.get("returnType"):
457
+ return_type = function_spec["returnType"]
458
458
  else:
459
459
  # Provide a fallback return type when missing
460
460
  return_type = {"kind": "any"}
@@ -45,6 +45,12 @@ def _build_timeout() -> httpx.Timeout:
45
45
  )
46
46
 
47
47
 
48
+ def _close_deadline() -> "float | None":
49
+ # Bound pool teardown so one wedged socket cannot hang invocation exit.
50
+ # None (POLY_HTTP_CLOSE_TIMEOUT=none) restores the old unbounded wait.
51
+ return _env_opt_float("POLY_HTTP_CLOSE_TIMEOUT", 5.0)
52
+
53
+
48
54
  # Import-time snapshot for reference/config surface. Clients re-read env at creation
49
55
  DEFAULT_LIMITS = _build_limits()
50
56
  DEFAULT_TIMEOUT = _build_timeout()
@@ -166,6 +172,40 @@ def _sweep_dead_loops() -> None:
166
172
  _retire_async_client(_async_clients.pop(loop, None), loop)
167
173
 
168
174
 
175
+ def _abandon_close_task(
176
+ close_task: "asyncio.Future[None]",
177
+ client: httpx.AsyncClient,
178
+ loop: asyncio.AbstractEventLoop,
179
+ deadline: "float | None",
180
+ context: str = "",
181
+ ) -> None:
182
+ """Detach a pool close that blew its deadline.
183
+
184
+ The task is cancelled and dropped rather than awaited: waiting for an
185
+ unresponsive pool to unwind is the stall the deadline exists to prevent.
186
+ Cancelling is what keeps loop shutdown bounded too, since asyncio.run()
187
+ tears down by cancelling every task and gathering them; a cancelled close
188
+ completes there instead of blocking. Whatever stays open is released when
189
+ the coroutine is collected, and by the OS at exit.
190
+ """
191
+ def _swallow(finished: "asyncio.Future[None]") -> None:
192
+ # Retrieve the result so a late failure is not reported as an
193
+ # unretrieved task exception.
194
+ if finished.cancelled():
195
+ return
196
+ error = finished.exception()
197
+ if error is not None:
198
+ logger.debug(
199
+ f"Abandoned aclose later failed with {error!r} id={id(client)} "
200
+ f"pid={os.getpid()} loop={id(loop)}")
201
+
202
+ close_task.add_done_callback(_swallow)
203
+ close_task.cancel()
204
+ logger.warning(
205
+ f"aclose exceeded {deadline}s deadline; abandoning pool "
206
+ f"id={id(client)} pid={os.getpid()} loop={id(loop)}{context}")
207
+
208
+
169
209
  def _register_loop_close_hook(loop: asyncio.AbstractEventLoop) -> None:
170
210
  """aclose the loop's async client right before the loop is torn down.
171
211
  """
@@ -177,8 +217,21 @@ def _register_loop_close_hook(loop: asyncio.AbstractEventLoop) -> None:
177
217
  _hooked_loops.discard(loop)
178
218
  cached = _async_clients.pop(loop, None)
179
219
  if cached is not None and not loop.is_closed():
220
+ deadline = _close_deadline()
180
221
  try:
181
- loop.run_until_complete(cached.aclose())
222
+ # asyncio.wait returns at the deadline without cancelling, so
223
+ # loop.close() proceeds even if the pool ignores cancellation.
224
+ close_task = loop.create_task(cached.aclose())
225
+ loop.run_until_complete(
226
+ asyncio.wait({close_task}, timeout=deadline))
227
+ if close_task.done():
228
+ # Surface real aclose failures, including a TimeoutError
229
+ # raised by aclose itself, rather than treating them as
230
+ # our deadline.
231
+ close_task.result()
232
+ else:
233
+ _abandon_close_task(
234
+ close_task, cached, loop, deadline, " on teardown")
182
235
  except Exception:
183
236
  logger.warning(
184
237
  f"Failed to aclose async client id={id(cached)} pid={os.getpid()} "
@@ -267,8 +320,19 @@ async def _close_clients_for_loop(
267
320
  return
268
321
 
269
322
  await _drain_inflight(client)
323
+ deadline = _close_deadline()
324
+ close_task = asyncio.ensure_future(client.aclose())
270
325
  try:
271
- await client.aclose()
326
+ # asyncio.wait returns at the deadline without cancelling the
327
+ # close, so the bound holds even when the pool never processes
328
+ # cancellation. wait_for would block on that unwind instead.
329
+ await asyncio.wait({close_task}, timeout=deadline)
330
+ if close_task.done():
331
+ # Real aclose errors, including a TimeoutError raised by aclose
332
+ # itself, still propagate to the close_async caller.
333
+ close_task.result()
334
+ else:
335
+ _abandon_close_task(close_task, client, current_loop, deadline)
272
336
  finally:
273
337
  if _async_clients.get(current_loop) is client:
274
338
  _async_clients.pop(current_loop, None)
@@ -5,13 +5,18 @@ import sys
5
5
  import re
6
6
  from typing import Dict, List, Mapping, Optional, Tuple, Any, Union
7
7
  from typing import _TypedDictMeta as BaseTypedDict # type: ignore
8
- from typing_extensions import _TypedDictMeta, cast # type: ignore
8
+ from typing_extensions import TypedDict, _TypedDictMeta, cast # type: ignore
9
9
  from stdlib_list import stdlib_list
10
10
  from pydantic import TypeAdapter
11
11
  from importlib.metadata import packages_distributions
12
12
  from polyapi.constants import PYTHON_TO_JSONSCHEMA_TYPE_MAP
13
13
  from polyapi.utils import print_red
14
- from polyapi.deployables import Deployment, DeployableRecord, get_deployable_file_revision
14
+ from polyapi.deployables import (
15
+ Deployment,
16
+ DeployableRecord,
17
+ DeployableTypes,
18
+ get_deployable_file_revision,
19
+ )
15
20
 
16
21
 
17
22
  # these libraries are already installed in the base docker image
@@ -116,15 +121,53 @@ def _parse_sphinx_docstring(docstring: str) -> Dict[str, Any]:
116
121
  }
117
122
 
118
123
 
119
- def _parse_google_docstring(docstring: str) -> Dict[str, Any]:
124
+ class _GoogleParamState(TypedDict):
125
+ name: str
126
+ type: str
127
+ description: List[str]
128
+
129
+
130
+ class _GoogleReturnsState(TypedDict):
131
+ type: str
132
+ description: List[str]
133
+
134
+
135
+ class GoogleDocstringParam(TypedDict):
136
+ name: str
137
+ type: str
138
+ typeSchema: Optional[Dict[str, Any]]
139
+ description: str
140
+
141
+
142
+ class GoogleDocstringReturns(TypedDict):
143
+ type: str
144
+ typeSchema: Optional[Dict[str, Any]]
145
+ description: str
146
+
147
+
148
+ class GoogleDocstring(TypedDict):
149
+ description: str
150
+ params: List[GoogleDocstringParam]
151
+ returns: GoogleDocstringReturns
152
+ raises: Dict[str, str]
153
+
154
+
155
+ class _GoogleDocstringState(TypedDict):
156
+ description: List[str]
157
+ params: Dict[str, _GoogleParamState]
158
+ returns: _GoogleReturnsState
159
+ raises: Dict[str, str]
160
+
161
+
162
+ def _parse_google_docstring(docstring: str) -> GoogleDocstring:
120
163
  import re
121
164
  lines = docstring.split('\n')
122
165
  mode = None
123
- params = {}
124
- parsed = {
166
+ params: Dict[str, _GoogleParamState] = {}
167
+ parsed: _GoogleDocstringState = {
125
168
  'description': [],
126
- 'params': [],
127
- 'returns': {'description': []},
169
+ 'params': params,
170
+ 'returns': {'type': '', 'description': []},
128
171
  'raises': {}
129
172
  }
130
173
  current_key = None
@@ -169,12 +212,25 @@ def _parse_google_docstring(docstring: str) -> Dict[str, Any]:
169
212
  elif mode is None:
170
213
  parsed['description'].append(line.strip())
171
214
 
172
- # Consolidate descriptions
173
- parsed['description'] = ' '.join(parsed['description']).strip()
174
- parsed['returns']['description'] = ' '.join(parsed['returns']['description']).strip()
175
- parsed['params'] = [{ **v, 'description': ' '.join(v['description']).strip() } for v in params.values()]
215
+ parsed_result: GoogleDocstring = {
216
+ 'description': ' '.join(parsed['description']).strip(),
217
+ 'params': [
218
+ {
219
+ **value,
220
+ 'typeSchema': None,
221
+ 'description': ' '.join(value['description']).strip(),
222
+ }
223
+ for value in params.values()
224
+ ],
225
+ 'returns': {
226
+ 'type': parsed['returns']['type'],
227
+ 'typeSchema': None,
228
+ 'description': ' '.join(parsed['returns']['description']).strip(),
229
+ },
230
+ 'raises': parsed['raises'],
231
+ }
176
232
 
177
- return parsed
233
+ return parsed_result
178
234
 
179
235
 
180
236
  def _get_schemas(code: str) -> List[Dict]:
@@ -299,12 +355,12 @@ def _parse_deploy_comment(comment: str) -> Optional[Deployment]:
299
355
 
300
356
  # Local development puts canopy on a different port than the poly-server
301
357
  if instance.endswith("localhost:3000"):
302
- instance = instance.replace(":3000', ':8000")
358
+ instance = instance.replace(":3000", ":8000")
303
359
 
304
360
  return {
305
361
  "name": name,
306
362
  "context": context,
307
- "type": deploy_type,
363
+ "type": DeployableTypes(deploy_type),
308
364
  "id": id,
309
365
  "deployed": deployed,
310
366
  "fileRevision": file_revision,
@@ -398,9 +454,9 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
398
454
  ):
399
455
  # We've found a polyConfig dictionary assignment
400
456
  if node.annotation.id == "PolyServerFunction":
401
- deployable["type"] = "server-function"
457
+ deployable["type"] = "server-function" # pyright: ignore[reportGeneralTypeIssues]
402
458
  elif node.annotation.id == "PolyClientFunction":
403
- deployable["type"] = "client-function"
459
+ deployable["type"] = "client-function" # pyright: ignore[reportGeneralTypeIssues]
404
460
  else:
405
461
  print_red("ERROR")
406
462
  print(f"Unsupported polyConfig type '${node.annotation.id}'")
@@ -466,7 +522,7 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
466
522
  if deployment:
467
523
  start = self._line_offsets[i]
468
524
  deployable["deployments"].append(deployment)
469
- deployable["deploymentCommentRanges"].append([start, start + len(line)])
525
+ deployable["deploymentCommentRanges"].append((start, start + len(line)))
470
526
 
471
527
  def visit_Import(self, node: ast.Import):
472
528
  # TODO maybe handle `import foo.bar` case?
@@ -539,7 +595,7 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
539
595
 
540
596
  def generic_visit(self, node):
541
597
  if hasattr(node, 'lineno') and hasattr(node, 'col_offset'):
542
- self._current_offset = self._line_offsets[node.lineno - 1] + node.col_offset
598
+ self._current_offset = self._line_offsets[node.lineno - 1] + node.col_offset # pyright: ignore[reportAttributeAccessIssue]
543
599
  super().generic_visit(node)
544
600
 
545
601
  tree = ast.parse(code)
@@ -74,7 +74,12 @@ def scrub_keys(e: Exception) -> Dict[str, Any]:
74
74
 
75
75
  def execute_query(table_id, method, query):
76
76
  from polyapi import polyCustom
77
- from polyapi.poly.client_id import client_id
77
+
78
+ client_id: Optional[str] = None
79
+ try:
80
+ from polyapi.poly.client_id import client_id # pyright: ignore[reportMissingImports]
81
+ except ImportError:
82
+ pass
78
83
 
79
84
  try:
80
85
  api_key, base_url = get_api_key_and_url()
@@ -84,7 +89,9 @@ def execute_query(table_id, method, query):
84
89
  )
85
90
 
86
91
  auth_key = polyCustom.get("executionApiKey") or api_key
87
- url = f"{base_url.rstrip('/')}/tables/{table_id}/{method}?clientId={client_id}"
92
+ url = f"{base_url.rstrip('/')}/tables/{table_id}/{method}"
93
+ if client_id:
94
+ url += f"?clientId={client_id}"
88
95
  headers = {"x-poly-execution-id": polyCustom.get("executionId") or ""}
89
96
  if auth_key:
90
97
  headers["Authorization"] = f"Bearer {auth_key}"
@@ -1,7 +1,7 @@
1
1
  import os
2
2
  import sys
3
3
  import subprocess
4
- from typing import List, Tuple, Literal
4
+ from typing import Any, Dict, List, Tuple, Literal, TypeGuard
5
5
  from polyapi import http_client
6
6
  from polyapi.utils import get_auth_headers
7
7
  from polyapi.config import get_api_key_and_url
@@ -10,7 +10,7 @@ from polyapi.deployables import (
10
10
  prepare_deployable_directory, write_cache_revision,
11
11
  save_deployable_records, get_all_deployable_files,
12
12
  is_cache_up_to_date, get_git_revision,
13
- write_updated_deployable, DeployableRecord
13
+ write_updated_deployable, DeployableRecord, PolyDeployConfig
14
14
  )
15
15
 
16
16
  class FunctionArgumentDto:
@@ -19,7 +19,15 @@ class FunctionArgumentDto:
19
19
  self.type = type
20
20
  self.description = description
21
21
 
22
- def get_function_description(deploy_type: Literal["server-function", "client-function"], description: str, arguments, code: str) -> str:
22
+
23
+ FunctionDeployType = Literal["server-function", "client-function"]
24
+
25
+
26
+ def _is_function_deploy_type(deploy_type: str) -> TypeGuard[FunctionDeployType]:
27
+ return deploy_type in ("server-function", "client-function")
28
+
29
+
30
+ def get_function_description(deploy_type: FunctionDeployType, description: str, arguments, code: str) -> Dict[str, Any]:
23
31
  if deploy_type == "server-function":
24
32
  return get_server_function_description(description, arguments, code)
25
33
  elif deploy_type == "client-function":
@@ -27,14 +35,14 @@ def get_function_description(deploy_type: Literal["server-function", "client-fun
27
35
  else:
28
36
  raise ValueError("Unsupported deployable type")
29
37
 
30
- def get_server_function_description(description: str, arguments, code: str) -> str:
38
+ def get_server_function_description(description: str, arguments, code: str) -> Dict[str, Any]:
31
39
  api_key, api_url = get_api_key_and_url()
32
40
  headers = get_auth_headers(api_key)
33
41
  data = {"description": description, "arguments": arguments, "code": code}
34
42
  response = http_client.post(f"{api_url}/functions/server/description-generation", headers=headers, json=data)
35
43
  return response.json()
36
44
 
37
- def get_client_function_description(description: str, arguments, code: str) -> str:
45
+ def get_client_function_description(description: str, arguments, code: str) -> Dict[str, Any]:
38
46
  api_key, api_url = get_api_key_and_url()
39
47
  headers = get_auth_headers(api_key)
40
48
  # Simulated API call to generate client function descriptions
@@ -50,8 +58,12 @@ def fill_in_missing_function_details(deployable: DeployableRecord, code: str) ->
50
58
  )
51
59
  if is_missing_descriptions:
52
60
  try:
61
+ deploy_type = deployable["type"]
62
+ if not _is_function_deploy_type(deploy_type):
63
+ raise ValueError(f'Unsupported deployable type: "{deploy_type}"')
64
+
53
65
  ai_generated = get_function_description(
54
- deployable["type"],
66
+ deploy_type,
55
67
  deployable["types"]["description"],
56
68
  [{"name": p["name"], "type": p["type"], "description": p.get("description")} for p in deployable["types"]["params"]],
57
69
  code
@@ -84,7 +96,13 @@ def get_base_url() -> str:
84
96
  def get_all_deployables(disable_docs: bool, disable_ai: bool, git_revision: str) -> List[DeployableRecord]:
85
97
  print("Searching for poly deployables.")
86
98
  base_url = get_base_url() or "."
87
- possible_deployables = get_all_deployable_files({"includeDirs": [base_url]})
99
+ deployable_search_config: PolyDeployConfig = {
100
+ "type_names": [],
101
+ "include_dirs": [base_url],
102
+ "include_files_or_extensions": [],
103
+ "exclude_dirs": [],
104
+ }
105
+ possible_deployables = get_all_deployable_files(deployable_search_config)
88
106
  print(f'Found {len(possible_deployables)} possible deployable file{"s" if len(possible_deployables) != 1 else ""}.')
89
107
 
90
108
  found = {}
@@ -75,8 +75,7 @@ def render_server_function(
75
75
 
76
76
 
77
77
  def _normalize_return_type_for_annotation(function_name: str, return_type_name: str) -> str:
78
- if return_type_name == "ReturnType":
79
- return "ReturnType"
78
+ # "ReturnType" lives in a separate module, so it must be qualified (e.g. _SFX.ReturnType).
80
79
  return add_type_import_path(function_name, return_type_name)
81
80
 
82
81