Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion polyapi/deployables.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def update_deployment_comments(file_content: str, deployable: dict) -> str:
if deployable['deployments']:
deployment_comments = write_deploy_comments(deployable['deployments'])
deployable['deploymentCommentRanges'] = [(0, len(deployment_comments) + 1)]
file_content = f"{deployment_comments}{file_content}"
file_content = f"{deployment_comments}\n{file_content}"
return file_content

def update_deployable_function_comments(file_content: str, deployable: dict, disable_docs: bool = False) -> str:
Expand Down
1 change: 0 additions & 1 deletion polyapi/function_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def function_add_or_update(
"code": code,
"language": "python",
"returnType": get_jsonschema_type(return_type),
"returnTypeSchema": parsed["types"]["returns"]["typeSchema"],
"arguments": [{**p, "key": p["name"], "type": get_jsonschema_type(p["type"]) } for p in parsed["types"]["params"]],
"logsEnabled": logs_enabled,
}
Expand Down
13 changes: 7 additions & 6 deletions polyapi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _get_schemas(code: str) -> List[Dict]:

def get_jsonschema_type(python_type: str):
if python_type == "Any":
return "Any"
return "any"

if python_type == "List":
return "array"
Expand Down Expand Up @@ -338,6 +338,7 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
"params": [],
"returns": {
"type": "",
"typeSchema": None,
"description": "",
}
},
Expand Down Expand Up @@ -435,13 +436,14 @@ def _extract_docstring_from_function(self, node: ast.FunctionDef):

def _extract_deploy_comments(self):
for i in range(len(self._lines)):
line = self._lines[i].strip()
line = self._lines[i]
if line and not line.startswith("#"):
return
deployment = _parse_deploy_comment(line)
deployment = _parse_deploy_comment(line.strip())
if deployment:
start = self._line_offsets[i]
deployable["deployments"].append(deployment)
deployable["deploymentCommentRanges"].append([self._line_offsets[i], len(line)])
deployable["deploymentCommentRanges"].append([start, start + len(line)])

def visit_Import(self, node: ast.Import):
# TODO maybe handle `import foo.bar` case?
Expand Down Expand Up @@ -471,8 +473,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef):
"type": python_type,
"description": "",
}
if type_schema:
json_arg["typeSchema"] = json.dumps(type_schema)
json_arg["typeSchema"] = json.dumps(type_schema) if type_schema else None

if docstring_params:
try:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]

[project]
name = "polyapi-python"
version = "0.3.2.dev1"
version = "0.3.2.dev2"
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
dependencies = [
Expand Down
12 changes: 11 additions & 1 deletion tests/test_deployables.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def foobar() -> int:

EXPECTED_SERVER_FN_DEPLOYMENTS = '''# Poly deployed @ 2024-11-12T14:43:22.631113 - testing.foobar - https://na1.polyapi.io/canopy/polyui/collections/server-functions/jh23h5g3h5b24jh5b2j3h45v2jhg43v52j3h - 086aedd
# Poly deployed @ 2024-11-11T14:43:22.631113 - testing.foobar - https://dev.polyapi.io/canopy/polyui/collections/server-functions/jh23h5g3h5b24jh5b2j3h45v2jhg43v52j3h - 086aedd

from polyapi.typedefs import PolyServerFunction

polyConfig: PolyServerFunction = {
Expand Down Expand Up @@ -76,6 +77,15 @@ def foobar(foo: str, bar: Dict[str, str]) -> int:
'''

class T(unittest.TestCase):
def test_parse_and_write_deployment_comment(self):
test_deployable = parse_function_code(EXPECTED_SERVER_FN_DEPLOYMENTS, "foobar")
deployable_comment_ranges = test_deployable["deploymentCommentRanges"]
updated_file_contents = update_deployment_comments(EXPECTED_SERVER_FN_DEPLOYMENTS, test_deployable)
self.assertEqual(updated_file_contents, EXPECTED_SERVER_FN_DEPLOYMENTS)
# Deployment comment ranges collapsed into one of equal size
self.assertEqual(test_deployable["deploymentCommentRanges"][0][0], deployable_comment_ranges[0][0])
self.assertEqual(test_deployable["deploymentCommentRanges"][0][1], deployable_comment_ranges[1][1])

def test_write_deployment_comment(self):
test_deployable = {
"deployments": [
Expand All @@ -98,7 +108,7 @@ def test_write_deployment_comment(self):
'type': 'server-function'
}
],
"deploymentCommentRanges": [[0, 178]]
"deploymentCommentRanges": [[0, 177]]
}
updated_file_contents = update_deployment_comments(INITIAL_SERVER_FN_DEPLOYMENTS, test_deployable)
self.assertEqual(updated_file_contents, EXPECTED_SERVER_FN_DEPLOYMENTS)
Expand Down
25 changes: 22 additions & 3 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from polyapi.parser import parse_function_code


CODE_NO_TYPES = """
def foobar(a, b):
return a + b
"""

SIMPLE_CODE = """
def foobar(n: int) -> int:
return 9
Expand Down Expand Up @@ -124,11 +129,21 @@ def foobar(foo: str, bar: Dict[str, str]) -> int:
'''

class T(unittest.TestCase):
def test_no_types(self):
deployable = parse_function_code(CODE_NO_TYPES, "foobar")
types = deployable["types"]
self.assertEqual(len(types["params"]), 2)
self.assertEqual(types["params"][0], {"name": "a", "type": "Any", "typeSchema": None, "description": ""})
self.assertEqual(types["params"][1], {"name": "b", "type": "Any", "typeSchema": None, "description": ""})
self.assertEqual(types["returns"]["type"], "Any")
self.assertIsNone(types["returns"]["typeSchema"])
self.assertEqual(deployable["dependencies"], [])

def test_simple_types(self):
deployable = parse_function_code(SIMPLE_CODE, "foobar")
types = deployable["types"]
self.assertEqual(len(types["params"]), 1)
self.assertEqual(types["params"][0], {"name": "n", "type": "int", "description": ""})
self.assertEqual(types["params"][0], {"name": "n", "type": "int", "typeSchema": None, "description": ""})
self.assertEqual(types["returns"]["type"], "int")
self.assertIsNone(types["returns"]["typeSchema"])
self.assertEqual(deployable["dependencies"], [])
Expand All @@ -137,7 +152,7 @@ def test_complex_return_type(self):
deployable = parse_function_code(COMPLEX_RETURN_TYPE, "foobar")
types = deployable["types"]
self.assertEqual(len(types["params"]), 1)
self.assertEqual(types["params"][0], {"name": "n", "type": "int", "description": ""})
self.assertEqual(types["params"][0], {"name": "n", "type": "int", "typeSchema": None, "description": ""})
self.assertEqual(types["returns"]["type"], "Barbar")
self.assertEqual(types["returns"]["typeSchema"]['title'], "Barbar")

Expand All @@ -153,7 +168,7 @@ def test_list_complex_return_type(self):
deployable = parse_function_code(LIST_COMPLEX_RETURN_TYPE, "foobar")
types = deployable["types"]
self.assertEqual(len(types["params"]), 1)
self.assertEqual(types["params"][0], {"name": "n", "type": "int", "description": ""})
self.assertEqual(types["params"][0], {"name": "n", "type": "int", "typeSchema": None, "description": ""})
self.assertEqual(types["returns"]["type"], "List[Barbar]")
self.assertEqual(types["returns"]["typeSchema"]["items"]['title'], "Barbar")

Expand Down Expand Up @@ -186,11 +201,13 @@ def test_parse_glide_server_function_bad_docstring(self):
self.assertEqual(deployable["types"]["params"][0], {
"name": "foo",
"type": "Any",
"typeSchema": None,
"description": "The foo in question"
})
self.assertEqual(deployable["types"]["params"][1], {
"name": "bar",
"type": "Any",
"typeSchema": None,
"description": "Configuration of bars"
})
self.assertEqual(deployable["types"]["returns"], {
Expand All @@ -205,11 +222,13 @@ def test_parse_glide_server_function_ok_docstring(self):
self.assertEqual(deployable["types"]["params"][0], {
"name": "foo",
"type": "str",
"typeSchema": None,
"description": "The foo in question"
})
self.assertEqual(deployable["types"]["params"][1], {
"name": "bar",
"type": "Dict[str, str]",
"typeSchema": None,
"description": "Configuration of bars"
})
self.assertEqual(deployable["types"]["returns"], {
Expand Down