From 7cdc39dadf97b0a0f8862a5f136a07555eee377e Mon Sep 17 00:00:00 2001 From: Atharva Satpute <55058959+atharva-satpute@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:32:36 +0530 Subject: [PATCH] Change function signature to return new name --- src/autoqasm/api.py | 15 +++++++-------- src/autoqasm/reserved_keywords.py | 15 +++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/autoqasm/api.py b/src/autoqasm/api.py index 26ead82..9de784b 100644 --- a/src/autoqasm/api.py +++ b/src/autoqasm/api.py @@ -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 @@ -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 @@ -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), ) diff --git a/src/autoqasm/reserved_keywords.py b/src/autoqasm/reserved_keywords.py index f3839d9..ecd86a1 100644 --- a/src/autoqasm/reserved_keywords.py +++ b/src/autoqasm/reserved_keywords.py @@ -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", @@ -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