Skip to content

Commit

Permalink
fix: python linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan M Salamanca committed Sep 19, 2024
1 parent a8758ca commit 697080b
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 60 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release workflow
# - When there is a push to 'main', it detects if the local version has changed and is higher than the remote (PyPI) version.
# - Generates a new nada-dsl release, signes it and pushes it to PyPI when a PR is merged that updates the version number in pyproject.toml.
# - Generates a new nada-dsl Github release

name: Create a new release

Expand Down
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ignore=CVS
# ignore-list. The regex matches against paths and can be in Posix or Windows
# format. Because '\\' represents the directory delimiter on Windows systems,
# it can't be used as an escape character.
ignore-paths=
ignore-paths=nada_dsl/version.py

# Files or directories matching the regular expression patterns are skipped.
# The regex matches against base names, not paths. The default value ignores
Expand Down Expand Up @@ -430,7 +430,7 @@ disable=raw-checker-failed,
deprecated-pragma,
use-symbolic-message-instead,
use-implicit-booleaness-not-comparison-to-string,
use-implicit-booleaness-not-comparison-to-zero
use-implicit-booleaness-not-comparison-to-zero,W0223,W0718,R0903

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
from nada_dsl.nada_types.generics import *
from nada_dsl.nada_types.function import *
from nada_dsl.nada_types.collections import *
from nada_dsl.circuit_io import *
from nada_dsl.program_io import *
from nada_dsl.compiler_frontend import nada_compile
7 changes: 4 additions & 3 deletions nada_dsl/ast_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
""" AST utilities."""

from abc import ABC, abstractmethod
from abc import ABC
from dataclasses import dataclass
import hashlib
from typing import Callable, Dict, List
from typing import Dict, List
from sortedcontainers import SortedDict
from nada_dsl.nada_types import NadaTypeRepr, Party
from nada_dsl.source_ref import SourceRef
Expand Down Expand Up @@ -179,6 +179,7 @@ def __init__(
self.literal_name = hashlib.md5(
(str(self.value) + str(self.ty)).encode("UTF-8")
).hexdigest()
super().__init__(id=self.id, source_ref=self.source_ref, ty=self.ty)

def to_mir(self):
return {
Expand Down Expand Up @@ -337,7 +338,7 @@ def __hash__(self) -> int:
return self.id


# TODO This is partially implemented
# Partially implemented
@dataclass
class CastASTOperation(ASTOperation):
"""AST Representation of a Cast operation."""
Expand Down
22 changes: 13 additions & 9 deletions nada_dsl/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import traceback
import importlib.util
from nada_dsl.compiler_frontend import nada_compile
from nada_dsl.errors import MissingEntryPointError, MissingProgramArgumentError
from nada_dsl.timer import add_timer, timer


Expand All @@ -21,7 +22,7 @@ class CompilerOutput:


@add_timer(timer_name="nada_dsl.compile.compile")
def compile(script_path: str) -> CompilerOutput:
def compile_script(script_path: str) -> CompilerOutput:
"""Compiles a NADA program
Args:
Expand All @@ -41,10 +42,10 @@ def compile(script_path: str) -> CompilerOutput:

try:
main = getattr(script, "nada_main")
except:
raise Exception(
except Exception as exc:
raise MissingEntryPointError(
"'nada_dsl' entrypoint function is missing in program " + script_name
)
) from exc
outputs = main()
compile_output = nada_compile(outputs)
return CompilerOutput(compile_output)
Expand All @@ -64,7 +65,7 @@ def compile_string(script: str) -> CompilerOutput:
temp_name = "temp_program"
spec = importlib.util.spec_from_loader(temp_name, loader=None)
module = importlib.util.module_from_spec(spec)
exec(decoded_program, module.__dict__)
exec(decoded_program, module.__dict__) # pylint:disable=W0122
sys.modules[temp_name] = module
globals()[temp_name] = module

Expand Down Expand Up @@ -92,17 +93,20 @@ def print_output(out: CompilerOutput):
timer.enable()
args_length = len(sys.argv)
if args_length < 2:
raise Exception("expected program as argument")
raise MissingProgramArgumentError("expected program as argument")
if args_length == 2:
output = compile(sys.argv[1])
output = compile_script(sys.argv[1])
print_output(output)
if args_length == 3 and sys.argv[1] == "-s":
output = compile_string(sys.argv[2])
print_output(output)

except Exception as ex:
tb = traceback.format_exc()
output = {"result": "Failure", "reason": str(ex), "traceback": str(tb)}
output = {
"result": "Failure",
"reason": str(ex),
"traceback": str(traceback.format_exc()),
}
print(json.dumps(output))

finally:
Expand Down
6 changes: 4 additions & 2 deletions nada_dsl/compile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import json
import pytest
from nada_dsl.ast_util import AST_OPERATIONS
from nada_dsl.compile import compile, compile_string
from nada_dsl.compile import compile_script, compile_string
from nada_dsl.compiler_frontend import FUNCTIONS, INPUTS, PARTIES


@pytest.fixture(autouse=True)
def clean_inputs():
PARTIES.clear()
Expand All @@ -18,14 +19,15 @@ def clean_inputs():
AST_OPERATIONS.clear()
yield


def get_test_programs_folder():
file_path = os.path.realpath(__file__)
this_directory = os.path.dirname(file_path)
return this_directory + "../test-programs/"


def test_compile_nada_fn_simple():
mir_str = compile(f"{get_test_programs_folder()}/nada_fn_simple.py").mir
mir_str = compile_script(f"{get_test_programs_folder()}/nada_fn_simple.py").mir
assert mir_str != ""
mir = json.loads(mir_str)
mir_functions = mir["functions"]
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/compiler_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)
from nada_dsl.timer import timer
from nada_dsl.source_ref import SourceRef
from nada_dsl.circuit_io import Output
from nada_dsl.program_io import Output

INPUTS = SortedDict()
PARTIES = SortedDict()
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/compiler_frontend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# pylint: disable=wildcard-import,unused-wildcard-import
from nada_dsl.nada_types.types import *
from nada_dsl.circuit_io import Input, Output
from nada_dsl.program_io import Input, Output
from nada_dsl.compiler_frontend import (
nada_dsl_to_nada_mir,
to_input_list,
Expand Down
18 changes: 16 additions & 2 deletions nada_dsl/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
"""
Custom exceptions used in this package.
Nada DSL Exceptions.
"""


class NadaNotAllowedException(Exception):
pass
"""Exception for not allowed use cases."""


class NadaInvalidTypeError(Exception):
"""Invalid type error"""


class MissingProgramArgumentError(Exception):
"""Missing program argument"""


class MissingEntryPointError(Exception):
"""Missing nada_main entry point for the program."""
8 changes: 6 additions & 2 deletions nada_dsl/future/operations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Operations that will be supported in the future."""

from dataclasses import dataclass

from nada_dsl import SourceRef
Expand All @@ -7,6 +9,8 @@

@dataclass
class Cast:
"""Cast operation."""

target: AllTypes
to: AllTypesType
source_ref: SourceRef
Expand All @@ -18,7 +22,7 @@ def __init__(self, target: AllTypes, to: AllTypes, source_ref: SourceRef):
self.source_ref = source_ref

def store_in_ast(self, ty: object):
# We don't need to use ty because the output type is part of the operation.
"""Store object in AST"""
AST_OPERATIONS[self.id] = CastASTOperation(
id=self.id, target=self.target, to=self.to, source_ref=self.source_ref
id=self.id, target=self.target, ty=ty, source_ref=self.source_ref
)
2 changes: 1 addition & 1 deletion nada_dsl/nada_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def to_type(self):

@classmethod
def class_to_type(cls):
"""Converts a class into a type"""
"""Converts a class into a MIR type"""
name = cls.__name__
# Rename public variables so they are considered as the same as literals.
if name.startswith("Public"):
Expand Down
2 changes: 1 addition & 1 deletion nada_dsl/nada_types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from . import NadaType
from dataclasses import dataclass
from nada_dsl.circuit_io import Literal
from nada_dsl.program_io import Literal
from nada_dsl.operations import Addition, BooleanAnd, BooleanOr, BooleanXor, Division, Equals, GreaterOrEqualThan, GreaterThan, IfElse, LeftShift, LessOrEqualThan, LessThan, Modulo, Multiplication, Not, NotEquals, Power, PublicOutputEquality, Random, Reveal, RightShift, Subtraction, TruncPr
from nada_dsl.source_ref import SourceRef
from typing import Union
Expand Down
Loading

0 comments on commit 697080b

Please sign in to comment.