when I try to return but I got an error in 2nd return signup_result & return login_result https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportUndefinedVariable
"return" can be used only within a functionPylance
here is utils.py
class CognitoResponse(object): def __init__(self, access_token, refresh_token, cognito_user_id=None): self.access_token = access_token self.refresh_token = refresh_token self.cognito_user_id = cognito_user_id def cognito_signup(username: str, password: str): return signup_result # In order to get the ID and authenticate, use AWS Cognito client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME')) try: response = client.sign_up( ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'), Username=username, Password=password ) except Exception as e: # Generally, will trigger upon non-unique email raise HTTPException(status_code=400, detail=f"{e}") user_sub = response['UserSub'] # This will confirm user registration as an admin without a confirmation code client.admin_confirm_sign_up( UserPoolId=os.environ.get('USER_POOL_ID'), Username=username, ) # Now authenticate the user and return the tokens auth_response = client.initiate_auth( ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'), AuthFlow='USER_PASSWORD_AUTH', AuthParameters={ 'USERNAME': username, 'PASSWORD': password } ) access_token = auth_response['AuthenticationResult']['AccessToken'] refresh_token = auth_response['AuthenticationResult']['RefreshToken'] signup_result = utils.CognitoResponse( access_token=access_token, refresh_token=refresh_token, cognito_user_id=user_sub ) return signup_result def cognito_login(username: str, password: str): return login_result client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME')) # Authenticate the user and return the tokens try: auth_response = client.initiate_auth( ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'), AuthFlow='USER_PASSWORD_AUTH', AuthParameters={ 'USERNAME': username, 'PASSWORD': password } ) except Exception as e: # Generally, will trigger upon wrong email/password raise HTTPException(status_code=400, detail=f"{e}") access_token = auth_response['AuthenticationResult']['AccessToken'] refresh_token = auth_response['AuthenticationResult']['RefreshToken'] login_result = utils.CognitoResponse( access_token=access_token, refresh_token=refresh_token ) return login_result
I also try to tab 2 times to avoid indentation error in return signup_result & return login_result but still got the same error Unexpected indentationPylance
Advertisement
Answer
def cognito_login(username: str, password: str): return login_result client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME')) # Authenticate the user and return the tokens try: auth_response = client.initiate_auth( ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'), AuthFlow='USER_PASSWORD_AUTH', AuthParameters={ 'USERNAME': username, 'PASSWORD': password } ) # lots more code...
The cognito_login()
function contains only one line of code return login_result
because that is the only code that is indented underneath the function.
The rest of the following code is not indented underneath the function, therefore it is not part of the function.
Indentation is very important in Python.
Your code should likely be formatted as follows:
def cognito_login(username: str, password: str): #return login_result remove this as the next lines of code need to run client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME')) # Authenticate the user and return the tokens try: auth_response = client.initiate_auth( ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'), AuthFlow='USER_PASSWORD_AUTH', AuthParameters={ 'USERNAME': username, 'PASSWORD': password } ) # lots more code...