polyapi-python 0.3.14.dev4__py3-none-any.whl → 0.3.16.dev2__py3-none-any.whl

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.
polyapi/generate.py CHANGED
@@ -16,7 +16,7 @@ from .webhook import render_webhook_handle
16
16
  from .typedefs import PropertySpecification, SchemaSpecDto, SpecificationDto, VariableSpecDto, TableSpecDto
17
17
  from .api import render_api_function
18
18
  from .server import render_server_function
19
- from .utils import add_import_to_init, get_auth_headers, init_the_init, print_green, to_func_namespace
19
+ from .utils import add_import_to_init, get_auth_headers, init_the_init, print_green, to_func_namespace, to_type_module_alias
20
20
  from .variables import generate_variables
21
21
  from .poly_tables import generate_tables
22
22
  from . import http_client
@@ -506,8 +506,10 @@ def add_function_file(
506
506
  with open(init_path, "r", encoding='utf-8') as f:
507
507
  init_content = f.read()
508
508
 
509
- # Prepare new content to append to __init__.py
510
- new_init_content = init_content + f"\n\nfrom . import {func_namespace}\n\n{func_str}"
509
+ # Import the generated type module under a private alias so PascalCase function
510
+ # names do not shadow their own module symbol in __init__.py.
511
+ type_module_alias = to_type_module_alias(function_name)
512
+ new_init_content = init_content + f"\n\nfrom . import {func_namespace} as {type_module_alias}\n\n{func_str}"
511
513
 
512
514
  # Use temporary files for atomic writes
513
515
  # Write to __init__.py atomically
polyapi/http_client.py CHANGED
@@ -3,6 +3,7 @@ import httpx
3
3
 
4
4
  _sync_client: httpx.Client | None = None
5
5
  _async_client: httpx.AsyncClient | None = None
6
+ _async_client_loop: asyncio.AbstractEventLoop | None = None
6
7
 
7
8
 
8
9
  def _get_sync_client() -> httpx.Client:
@@ -13,9 +14,11 @@ def _get_sync_client() -> httpx.Client:
13
14
 
14
15
 
15
16
  def _get_async_client() -> httpx.AsyncClient:
16
- global _async_client
17
- if _async_client is None:
17
+ global _async_client, _async_client_loop
18
+ current_loop = asyncio.get_running_loop()
19
+ if _async_client is None or _async_client_loop is not current_loop:
18
20
  _async_client = httpx.AsyncClient(timeout=None)
21
+ _async_client_loop = current_loop
19
22
  return _async_client
20
23
 
21
24
 
@@ -66,8 +69,15 @@ def close():
66
69
  _sync_client = None
67
70
 
68
71
  async def close_async():
69
- global _sync_client, _async_client
72
+ global _sync_client, _async_client, _async_client_loop
70
73
  close()
71
- if _async_client is not None:
72
- await _async_client.aclose()
73
- _async_client = None
74
+ client = _async_client
75
+ client_loop = _async_client_loop
76
+ _async_client = None
77
+ _async_client_loop = None
78
+ if client is None:
79
+ return
80
+
81
+ current_loop = asyncio.get_running_loop()
82
+ if client_loop is current_loop:
83
+ await client.aclose()
polyapi/schema.py CHANGED
@@ -101,8 +101,11 @@ def generate_schema_types(input_data: Dict, root=None):
101
101
  return output
102
102
 
103
103
 
104
- # Regex to match everything between "# example: {\n" and "^}$"
105
- MALFORMED_EXAMPLES_PATTERN = re.compile(r"# example: \{\n.*?^\}$", flags=re.DOTALL | re.MULTILINE)
104
+ # Matches commented example headers emitted by jsonschema-gentypes before a raw
105
+ # multiline JSON object/array body that is not commented out.
106
+ MALFORMED_EXAMPLE_HEADER_PATTERN = re.compile(
107
+ r"^\s*#\s*(?:\|\s*)?example:\s*([\[{])\s*$"
108
+ )
106
109
 
107
110
  # Regex to fix invalid escape sequences in docstrings
108
111
  INVALID_ESCAPE_PATTERNS = [
@@ -117,8 +120,24 @@ def clean_malformed_examples(example: str) -> str:
117
120
  """ there is a bug in the `jsonschmea_gentypes` library where if an example from a jsonchema is an object,
118
121
  it will break the code because the object won't be properly commented out. Also fixes invalid escape sequences.
119
122
  """
120
- # Remove malformed examples
121
- cleaned_example = MALFORMED_EXAMPLES_PATTERN.sub("", example)
123
+ cleaned_lines = []
124
+ balance = 0
125
+ skipping_example = False
126
+
127
+ for line in example.splitlines(keepends=True):
128
+ if not skipping_example:
129
+ if MALFORMED_EXAMPLE_HEADER_PATTERN.match(line):
130
+ skipping_example = True
131
+ balance = line.count("{") + line.count("[") - line.count("}") - line.count("]")
132
+ continue
133
+ cleaned_lines.append(line)
134
+ continue
135
+
136
+ balance += line.count("{") + line.count("[") - line.count("}") - line.count("]")
137
+ if balance <= 0:
138
+ skipping_example = False
139
+
140
+ cleaned_example = "".join(cleaned_lines)
122
141
 
123
142
  # Fix invalid escape sequences in docstrings
124
143
  for pattern, replacement in INVALID_ESCAPE_PATTERNS:
polyapi/utils.py CHANGED
@@ -71,11 +71,17 @@ def print_red(s: str):
71
71
  print(Fore.RED + s + Style.RESET_ALL)
72
72
 
73
73
 
74
+ def to_type_module_alias(function_name: str) -> str:
75
+ """Return the internal alias used for a function's generated type module."""
76
+ return f"_{to_func_namespace(function_name)}_types"
77
+
78
+
74
79
  def add_type_import_path(function_name: str, arg: str) -> str:
75
80
  """if not basic type, coerce to camelCase and add the import path"""
76
81
  # from now, we start qualifying non-basic types :))
77
82
  # e.g. Callable[[EmailAddress, Dict, Dict, Dict], None]
78
83
  # becomes Callable[[Set_profile_email.EmailAddress, Dict, Dict, Dict], None]
84
+ type_module_alias = to_type_module_alias(function_name)
79
85
 
80
86
  if arg.startswith("Callable"):
81
87
  inner = arg[len("Callable["):-1] # strip outer Callable[...]
@@ -84,7 +90,7 @@ def add_type_import_path(function_name: str, arg: str) -> str:
84
90
  for p in parts:
85
91
  clean = p.strip("[] ")
86
92
  if clean and clean not in BASIC_PYTHON_TYPES:
87
- replacement = f"{to_func_namespace(function_name)}.{camelCase(clean)}"
93
+ replacement = f"{type_module_alias}.{camelCase(clean)}"
88
94
  p = p.replace(clean, replacement)
89
95
  qualified.append(p)
90
96
  return "Callable[" + ",".join(qualified) + "]"
@@ -100,11 +106,11 @@ def add_type_import_path(function_name: str, arg: str) -> str:
100
106
  else:
101
107
  if '"' in sub:
102
108
  sub = sub.replace('"', "")
103
- return f'List["{to_func_namespace(function_name)}.{camelCase(sub)}"]'
109
+ return f'List["{type_module_alias}.{camelCase(sub)}"]'
104
110
  else:
105
- return f"List[{to_func_namespace(function_name)}.{camelCase(sub)}]"
111
+ return f"List[{type_module_alias}.{camelCase(sub)}]"
106
112
 
107
- return f"{to_func_namespace(function_name)}.{camelCase(arg)}"
113
+ return f"{type_module_alias}.{camelCase(arg)}"
108
114
 
109
115
 
110
116
  def get_type_and_def(
@@ -352,4 +358,4 @@ def return_type_already_defined_in_args(return_type_name: str, args_def: str) ->
352
358
  basic_match = bool(re.search(basic_pattern, args_def, re.MULTILINE))
353
359
  class_pattern = rf"^class {re.escape(return_type_name)}\(TypedDict"
354
360
  class_match = bool(re.search(class_pattern, args_def, re.MULTILINE))
355
- return basic_match or class_match
361
+ return basic_match or class_match
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.14.dev4
3
+ Version: 0.3.16.dev2
4
4
  Summary: The Python Client for PolyAPI, the IPaaS by Developers for Developers
5
5
  Author-email: Dan Fellin <dan@polyapi.io>
6
6
  License: MIT License
@@ -12,23 +12,23 @@ polyapi/error_handler.py,sha256=I_e0iz6VM23FLVQWJljxs2NGcl_OODbi43OcbnqBlp8,2398
12
12
  polyapi/exceptions.py,sha256=Zh7i7eCUhDuXEdUYjatkLFTeZkrx1BJ1P5ePgbJ9eIY,89
13
13
  polyapi/execute.py,sha256=MeAmc5qsMQfnQPiHJK9Khkn_wrBxMoyMYYqmuFFPsRg,8779
14
14
  polyapi/function_cli.py,sha256=kKrecKCDNMmYtcOOCVF2KKOGjFUfWD2TbiVsyYQ84y8,4323
15
- polyapi/generate.py,sha256=TjMlxxjWN1WnFEmOjSygFcl9RkHCZeCq79R-K_7GYRU,21059
16
- polyapi/http_client.py,sha256=dfsfFzHlhScQgYMf3zJshJJCwgJGQqM8B5peY9O4NPU,1872
15
+ polyapi/generate.py,sha256=Bti1GlCi6yzv8pqsnJ95_KDES-L_1jwYGmsugZ4_hhA,21271
16
+ polyapi/http_client.py,sha256=Pd6FYHGB6plWlBbwZD9LPP6FEicdp0wyLWHd_9jxsaY,2273
17
17
  polyapi/parser.py,sha256=pAkeuwp1Kn6bHXiIoj-DuXQ518PztQThIa8vztgsKLQ,21525
18
18
  polyapi/poly_schemas.py,sha256=fZ6AGvHcOKQJtlrzSuzeBNed5DxPMA2dJGdJvuFCHWM,9066
19
19
  polyapi/poly_tables.py,sha256=T07j4H-HdcIFP5HwUq0uljpzzYzMtJvdqYvOdC0oZ4I,19728
20
20
  polyapi/prepare.py,sha256=DBlrQu_A0PqdaQ3KlMs-C6PkV3qncatkAiBlC7j2hnk,7441
21
21
  polyapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
22
  polyapi/rendered_spec.py,sha256=J2Num7MXnW_mKAe2MSVIL1coWopNYZ-6nNNwQRfseV0,2262
23
- polyapi/schema.py,sha256=-mtRV5iL3CV0X3phXhGYFV8sLz0KouTACOKWyGO9Pwc,5309
23
+ polyapi/schema.py,sha256=2ehUgs4IcNvqsg5cUjZReg-X83hbh4l0eStKmJhFsBY,5921
24
24
  polyapi/server.py,sha256=35l3f9d6jWaa60MfX5n7KF4UCAQR-Xg8WLc0NL9dlfY,2479
25
25
  polyapi/sync.py,sha256=t0Khh8B5GylxB7KWRP_xvkrvkuyVarSLCOvtmaz7lcc,6766
26
26
  polyapi/typedefs.py,sha256=mfll-KrngOW0wzbvDNiIDaDkFMwbsT-MY5y5hzWj9RE,5642
27
- polyapi/utils.py,sha256=VN8n_jvphderplI5MDYjSutxVnYbJjHhJznYkwjgstw,13349
27
+ polyapi/utils.py,sha256=Z4zx43bJ09ihKhcSrwXKcQrIFLUkF-0rEWI6PyqQ-rA,13542
28
28
  polyapi/variables.py,sha256=hfSDPGQK6YphNCa9MqE0W88WIFfqCQWgpBDWkExMq-A,7582
29
29
  polyapi/webhook.py,sha256=0ceLwHNjNd2Yx_8MX5jOIxiQ5Lwhy2pe81ProAuKon0,5108
30
- polyapi_python-0.3.14.dev4.dist-info/licenses/LICENSE,sha256=6b_I7aPVp8JXhqQwdw7_B84Ca0S4JGjHj0sr_1VOdB4,1068
31
- polyapi_python-0.3.14.dev4.dist-info/METADATA,sha256=_SPOiz8v0LAzfWBJQw4XwS7w0Z5-sJuPW7WghBXnT38,5928
32
- polyapi_python-0.3.14.dev4.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
33
- polyapi_python-0.3.14.dev4.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
34
- polyapi_python-0.3.14.dev4.dist-info/RECORD,,
30
+ polyapi_python-0.3.16.dev2.dist-info/licenses/LICENSE,sha256=6b_I7aPVp8JXhqQwdw7_B84Ca0S4JGjHj0sr_1VOdB4,1068
31
+ polyapi_python-0.3.16.dev2.dist-info/METADATA,sha256=Et6WT-Vie2lHIxdhZz3JhEdiw_yAoP70lYeWBQiIEbg,5928
32
+ polyapi_python-0.3.16.dev2.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
33
+ polyapi_python-0.3.16.dev2.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
34
+ polyapi_python-0.3.16.dev2.dist-info/RECORD,,