Skip to content

Commit

Permalink
Change function signature to return new name
Browse files Browse the repository at this point in the history
  • Loading branch information
atharva-satpute committed Jun 11, 2024
1 parent 19084a9 commit 7cdc39d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/autoqasm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import autoqasm.types as aq_types
from autoqasm import errors
from autoqasm.program.gate_calibrations import GateCalibration
from autoqasm.reserved_keywords import is_reserved_keyword
from autoqasm.reserved_keywords import sanitize_parameter_name
from autoqasm.types import QubitIdentifierType as Qubit


Expand Down Expand Up @@ -326,9 +326,8 @@ def _convert_subroutine(

# Iterate over list of dictionary keys to avoid runtime error
for key in list(kwargs):
is_keyword, new_name = is_reserved_keyword(key)
if is_keyword:
kwargs[new_name] = kwargs.pop(key)
new_name = sanitize_parameter_name(key)
kwargs[new_name] = kwargs.pop(key)

if f not in program_conversion_context.subroutines_processing:
# Mark that we are starting to process this function to short-circuit recursion
Expand Down Expand Up @@ -453,13 +452,13 @@ def _func(*args, **kwargs) -> Any:
f'Parameter "{param.name}" for subroutine "{_func.__name__}" '
"is missing a required type hint."
)

# Check whether 'param.name' is a reserved keyword
is_keyword, _name = is_reserved_keyword(param.name)
if is_keyword:
_func.__annotations__.pop(param.name)
new_name = sanitize_parameter_name(param.name)
_func.__annotations__.pop(param.name)

new_param = inspect.Parameter(
name=_name,
name=new_name,
kind=param.kind,
annotation=aq_types.map_parameter_type(param.annotation),
)
Expand Down
15 changes: 7 additions & 8 deletions src/autoqasm/reserved_keywords.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Permalink: https://github.com/openqasm/openqasm/blob/main/source/grammar/qasm3Lexer.g4
# Copied from: https://github.com/openqasm/openqasm/blob/main/source/grammar/qasm3Lexer.g4
reserved_keywords = {
"angle",
"array",
Expand Down Expand Up @@ -57,18 +57,17 @@
}


def is_reserved_keyword(name: str) -> tuple[bool, str]:
def sanitize_parameter_name(name: str) -> str:
"""
Method to check whether or not 'name' is a reserved keyword
Method to modify the variable name if it is a
reserved keyword
Args:
name (str): Name of the variable to be checked
Returns:
tuple[bool, str]: Returns a tuple containing a boolean indicating
whether the input 'name' is a reserved keyword and the modified name.
If 'name' is a reserved keyword, the modified name has an underscore
('_') appended to it; otherwise, it remains unchanged.
str: Returns a modified 'name' that has an underscore ('_') appended to it;
otherwise, it returns the original 'name' unchanged
"""
is_keyword = name in reserved_keywords
return (is_keyword, f"{name}_" if is_keyword else name)
return f"{name}_" if is_keyword else name

0 comments on commit 7cdc39d

Please sign in to comment.