Skip to content

Commit

Permalink
Merge pull request #125 from angelcaru/mypy
Browse files Browse the repository at this point in the history
Add type annotations to Python codebase (REP-4 #70)
  • Loading branch information
Almas-Ali authored May 1, 2024
2 parents 09432cf + 85c3ccf commit e2d0f41
Show file tree
Hide file tree
Showing 30 changed files with 1,492 additions and 1,281 deletions.
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ help:
@echo "Usage: make [target]"
@echo "Targets:"
@echo " format - Format code"
@echo " format-check - Check code formatting"
@echo " lint - Check code quality"
@echo " lint - Check code quality and formattings"
@echo " test - Run tests"
@echo " test-record - Record tests"
@echo " test-diff [FILE] - Diff tests"
Expand All @@ -18,17 +17,13 @@ help:
@echo ""
@echo "Radon Software Foundation - https://radon-project.github.io/"


.PHONY: format-check
format-check:
@ruff format --check .

.PHONY: format
format:
@ruff format .

.PHONY: lint
lint:
@ruff format --check .
@ruff check .

.PHONY: test
Expand Down
1 change: 1 addition & 0 deletions core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

from core.builtin_funcs import run

__all__ = ["run"]
__version__ = "0.0.1"
7 changes: 1 addition & 6 deletions core/builtin_classes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from core.builtin_classes.base_classes import BuiltInClass
from core.builtin_funcs import global_symbol_table
from core.builtin_classes.file_object import FileObject
from core.builtin_classes.string_object import StringObject
from core.builtin_classes.json_object import JSONObject
from core.builtin_classes.requests_object import RequestsObject


global_symbol_table.set("File", BuiltInClass("File", FileObject))
global_symbol_table.set("String", BuiltInClass("String", StringObject))
global_symbol_table.set("Json", BuiltInClass("Json", JSONObject))
global_symbol_table.set("Requests", BuiltInClass("Requests", RequestsObject))
__all__ = ["BuiltInClass", "FileObject", "StringObject", "JSONObject", "RequestsObject"]
4 changes: 2 additions & 2 deletions core/builtin_classes/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class BuiltInClass(BaseClass):
def __init__(self, name, instance_class):
super().__init__(name)
super().__init__(name, instance_class.__symbol_table__)
self.instance_class = instance_class

def create(self, args):
Expand Down Expand Up @@ -67,7 +67,7 @@ def __new__(cls, class_name, bases, attrs):
elif hasattr(value, "__is_method__") and value.__is_method__:
assert hasattr(value, "arg_names"), "Make sure to use the args() decorator on any built-in methods!"
assert hasattr(value, "defaults"), "Unreachable. The first `assert` should have ensured this."
symbols[name] = bif = BuiltInFunction(value.__name__, value)
symbols[name] = BuiltInFunction(value.__name__, value)
symbol_table = SymbolTable(None)
symbol_table.symbols = symbols

Expand Down
2 changes: 1 addition & 1 deletion core/builtin_classes/file_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def close(ctx):
res = RTResult()
self = ctx.symbol_table.get("this")
self.file.close()
return res.success(Number.null)
return res.success(Number.null())

@args([])
@method
Expand Down
11 changes: 7 additions & 4 deletions core/builtin_classes/json_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import json


class JSONObject(BuiltInObject):
@operator("__constructor__")
@check([],[])
@check([], [])
def constructor(self):
return RTResult().success(None)

Expand All @@ -25,16 +26,18 @@ def dumps(ctx):
RTError(radon_object.pos_start, radon_object.pos_end, f"Error dumping object: {str(e)}", ctx)
)


@args(["radon_string"])
@method
def loads(ctx):
res = RTResult()
radon_string = ctx.symbol_table.get("radon_string")
try:
return res.success(radonify(json.loads(radon_string.value), radon_string.pos_start, radon_string.pos_end, radon_string.context))
return res.success(
radonify(
json.loads(radon_string.value), radon_string.pos_start, radon_string.pos_end, radon_string.context
)
)
except Exception as e:
return res.failure(
RTError(radon_string.pos_start, radon_string.pos_end, f"Error loading object: {str(e)}", ctx)
)

59 changes: 24 additions & 35 deletions core/builtin_classes/requests_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ def get(ctx):
url = ctx.symbol_table.get("url")
headers = ctx.symbol_table.get("headers")
try:
req = urllib.request.Request(
url.value, headers=deradonify(headers))
req = urllib.request.Request(url.value, headers=deradonify(headers))
with urllib.request.urlopen(req) as response:
response_data = response.read().decode('utf-8')
response_data = response.read().decode("utf-8")
return res.success(radonify(response_data, url.pos_start, url.pos_end, url.context))
except Exception as e:
return res.failure(
RTError(url.pos_start, url.pos_end,
f"Error sending GET request: {str(e)}", ctx)
)
return res.failure(RTError(url.pos_start, url.pos_end, f"Error sending GET request: {str(e)}", ctx))

@args(["url", "data", "headers"], [String, HashMap({}), HashMap({})])
@method
Expand All @@ -42,16 +38,14 @@ def post(ctx):
data = ctx.symbol_table.get("data")
headers = ctx.symbol_table.get("headers")
try:
req = urllib.request.Request(url.value, data=json.dumps(
deradonify(data)).encode('utf-8'), headers=deradonify(headers))
req = urllib.request.Request(
url.value, data=json.dumps(deradonify(data)).encode("utf-8"), headers=deradonify(headers)
)
with urllib.request.urlopen(req) as response:
response_data = response.read().decode('utf-8')
response_data = response.read().decode("utf-8")
return res.success(radonify(response_data, url.pos_start, url.pos_end, url.context))
except Exception as e:
return res.failure(
RTError(url.pos_start, url.pos_end,
f"Error sending POST request: {str(e)}", ctx)
)
return res.failure(RTError(url.pos_start, url.pos_end, f"Error sending POST request: {str(e)}", ctx))

@args(["url", "data", "headers"], [String, HashMap({}), HashMap({})])
@method
Expand All @@ -61,16 +55,14 @@ def put(ctx):
data = ctx.symbol_table.get("data")
headers = ctx.symbol_table.get("headers")
try:
req = urllib.request.Request(url.value, data=json.dumps(deradonify(
data)).encode('utf-8'), headers=deradonify(headers), method='PUT')
req = urllib.request.Request(
url.value, data=json.dumps(deradonify(data)).encode("utf-8"), headers=deradonify(headers), method="PUT"
)
with urllib.request.urlopen(req) as response:
response_data = response.read().decode('utf-8')
response_data = response.read().decode("utf-8")
return res.success(radonify(response_data, url.pos_start, url.pos_end, url.context))
except Exception as e:
return res.failure(
RTError(url.pos_start, url.pos_end,
f"Error sending PUT request: {str(e)}", ctx)
)
return res.failure(RTError(url.pos_start, url.pos_end, f"Error sending PUT request: {str(e)}", ctx))

@args(["url", "headers"], [String, HashMap({})])
@method
Expand All @@ -79,16 +71,12 @@ def delete(ctx):
url = ctx.symbol_table.get("url")
headers = ctx.symbol_table.get("headers")
try:
req = urllib.request.Request(
url.value, headers=deradonify(headers), method='DELETE')
req = urllib.request.Request(url.value, headers=deradonify(headers), method="DELETE")
with urllib.request.urlopen(req) as response:
response_data = response.read().decode('utf-8')
response_data = response.read().decode("utf-8")
return res.success(radonify(response_data, url.pos_start, url.pos_end, url.context))
except Exception as e:
return res.failure(
RTError(url.pos_start, url.pos_end,
f"Error sending DELETE request: {str(e)}", ctx)
)
return res.failure(RTError(url.pos_start, url.pos_end, f"Error sending DELETE request: {str(e)}", ctx))

@args(["url", "data", "headers"], [String, HashMap({}), HashMap({})])
@method
Expand All @@ -98,13 +86,14 @@ def patch(ctx):
data = ctx.symbol_table.get("data")
headers = ctx.symbol_table.get("headers")
try:
req = urllib.request.Request(url.value, data=json.dumps(deradonify(
data)).encode('utf-8'), headers=deradonify(headers), method='PATCH')
req = urllib.request.Request(
url.value,
data=json.dumps(deradonify(data)).encode("utf-8"),
headers=deradonify(headers),
method="PATCH",
)
with urllib.request.urlopen(req) as response:
response_data = response.read().decode('utf-8')
response_data = response.read().decode("utf-8")
return res.success(radonify(response_data, url.pos_start, url.pos_end, url.context))
except Exception as e:
return res.failure(
RTError(url.pos_start, url.pos_end,
f"Error sending PATCH request: {str(e)}", ctx)
)
return res.failure(RTError(url.pos_start, url.pos_end, f"Error sending PATCH request: {str(e)}", ctx))
2 changes: 1 addition & 1 deletion core/builtin_classes/string_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StringObject(BuiltInObject):
@check([String], [String("")])
def constructor(self, string: String):
self.value: str = string.value
return RTResult().success(None)
return RTResult[None]().success(None)

@operator("__add__")
@check([String])
Expand Down
Loading

0 comments on commit e2d0f41

Please sign in to comment.