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
JavaScript
x
2
1
"return" can be used only within a functionPylance
2
here is utils.py
JavaScript
1
74
74
1
class CognitoResponse(object):
2
def __init__(self, access_token, refresh_token, cognito_user_id=None):
3
self.access_token = access_token
4
self.refresh_token = refresh_token
5
self.cognito_user_id = cognito_user_id
6
7
def cognito_signup(username: str, password: str):
8
9
return signup_result
10
11
# In order to get the ID and authenticate, use AWS Cognito
12
client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME'))
13
try:
14
response = client.sign_up(
15
ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'),
16
Username=username,
17
Password=password
18
)
19
except Exception as e: # Generally, will trigger upon non-unique email
20
raise HTTPException(status_code=400, detail=f"{e}")
21
22
user_sub = response['UserSub']
23
24
# This will confirm user registration as an admin without a confirmation code
25
client.admin_confirm_sign_up(
26
UserPoolId=os.environ.get('USER_POOL_ID'),
27
Username=username,
28
)
29
30
# Now authenticate the user and return the tokens
31
auth_response = client.initiate_auth(
32
ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'),
33
AuthFlow='USER_PASSWORD_AUTH',
34
AuthParameters={
35
'USERNAME': username,
36
'PASSWORD': password
37
}
38
)
39
access_token = auth_response['AuthenticationResult']['AccessToken']
40
refresh_token = auth_response['AuthenticationResult']['RefreshToken']
41
42
signup_result = utils.CognitoResponse(
43
access_token=access_token,
44
refresh_token=refresh_token,
45
cognito_user_id=user_sub
46
)
47
return signup_result
48
49
def cognito_login(username: str, password: str):
50
51
return login_result
52
53
client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME'))
54
# Authenticate the user and return the tokens
55
try:
56
auth_response = client.initiate_auth(
57
ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'),
58
AuthFlow='USER_PASSWORD_AUTH',
59
AuthParameters={
60
'USERNAME': username,
61
'PASSWORD': password
62
}
63
)
64
except Exception as e: # Generally, will trigger upon wrong email/password
65
raise HTTPException(status_code=400, detail=f"{e}")
66
67
access_token = auth_response['AuthenticationResult']['AccessToken']
68
refresh_token = auth_response['AuthenticationResult']['RefreshToken']
69
login_result = utils.CognitoResponse(
70
access_token=access_token,
71
refresh_token=refresh_token
72
)
73
return login_result
74
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
JavaScript
1
18
18
1
def cognito_login(username: str, password: str):
2
3
return login_result
4
5
client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME'))
6
7
# Authenticate the user and return the tokens
8
try:
9
auth_response = client.initiate_auth(
10
ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'),
11
AuthFlow='USER_PASSWORD_AUTH',
12
AuthParameters={
13
'USERNAME': username,
14
'PASSWORD': password
15
}
16
)
17
# lots more code...
18
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:
JavaScript
1
18
18
1
def cognito_login(username: str, password: str):
2
3
#return login_result remove this as the next lines of code need to run
4
5
client = boto3.client('cognito-idp', region_name=os.environ.get('COGNITO_REGION_NAME'))
6
7
# Authenticate the user and return the tokens
8
try:
9
auth_response = client.initiate_auth(
10
ClientId=os.environ.get('COGNITO_USER_CLIENT_ID'),
11
AuthFlow='USER_PASSWORD_AUTH',
12
AuthParameters={
13
'USERNAME': username,
14
'PASSWORD': password
15
}
16
)
17
# lots more code...
18