Skip to content

fix: resolve mypy type errors in cli/testflinger_cli/auth.py#1030

Open
Ahmedaltu wants to merge 1 commit intocanonical:mainfrom
Ahmedaltu:fix/auth-type-errors
Open

fix: resolve mypy type errors in cli/testflinger_cli/auth.py#1030
Ahmedaltu wants to merge 1 commit intocanonical:mainfrom
Ahmedaltu:fix/auth-type-errors

Conversation

@Ahmedaltu
Copy link
Copy Markdown

Summary

Fixes type errors in cli/testflinger_cli/auth.py detected by mypy.

Changes

  • _authenticate_with_credentials and _authenticate_with_refresh_token: added return None after _handle_auth_error() calls and updated return types from str to str | None to reflect actual behaviour
  • get_stored_refresh_token: changed fallback=None to fallback="" in ConfigParser.get() call — ConfigParser expects str, not None
  • decode_jwt_token: changed self.jwt_token to self.jwt_token or "" in jwt.decode() call — jwt_token is str | None but jwt.decode() expects str | bytes

Verification

Before:
cli/testflinger_cli/auth.py:79: error: Missing return statement [return]
cli/testflinger_cli/auth.py:91: error: Missing return statement [return]
cli/testflinger_cli/auth.py:126: error: Argument "fallback" to "get" of "ConfigParser" has incompatible type "None"; expected "str" [arg-type]
cli/testflinger_cli/auth.py:180: error: Argument 1 has incompatible type "str | None"; expected "str | bytes" [arg-type]

After:
(no errors for auth.py)

All 216 unit tests pass. tox checks pass (lock, format, lint, unit).

Closes #1028

Copy link
Copy Markdown
Collaborator

@ajzobro ajzobro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your offer to contribute to this project!

Please showcase how your code changes are non-breaking by including tests which ensure that the new code is still handling these conditions the same way. Especially cases where None has been replaced with "". See my comments below.

Comment thread cli/testflinger_cli/auth.py Outdated

# Set client_id from config file to keep token owner
self.client_id = config_file.get("AUTH", "client_id", fallback=None)
self.client_id = config_file.get("AUTH", "client_id", fallback="")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should client_id be set to an empty string instead of None here? I think that None is appropriate.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to fallback=None. Added # type: ignore[arg-type] to satisfy mypy while keeping None as the intended value. No behaviour change.

Comment thread cli/testflinger_cli/auth.py Outdated
try:
decoded_jwt = jwt.decode(
self.jwt_token, options={"verify_signature": False}
self.jwt_token or "", options={"verify_signature": False}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like above, rather than an empty string I feel like None is appropriate here as it ought to land us in the exception handler.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to passing self.jwt_token directly. Added # type: ignore[arg-type] to satisfy mypy. None will still land in the exception handler as intended. No behaviour change.

@Ahmedaltu Ahmedaltu force-pushed the fix/auth-type-errors branch 3 times, most recently from 98632c2 to 675471e Compare April 25, 2026 13:28
@Ahmedaltu
Copy link
Copy Markdown
Author

Reverted both runtime changes. Added # type: ignore[arg-type] on lines 130 and 184 to satisfy mypy while keeping None as the intended value in both cases. No behaviour change.

- Add return None after _handle_auth_error() calls in
  _authenticate_with_credentials and _authenticate_with_refresh_token
- Add type: ignore[arg-type] on ConfigParser.get() fallback=None call
- Add type: ignore[arg-type] on jwt.decode() call for str | None token

Fixes canonical#1028

Signed-off-by: Ahmed Al-Tuwaijari <altuwaijari.ahmed@gmail.com>
@Ahmedaltu Ahmedaltu force-pushed the fix/auth-type-errors branch from bd4ff00 to e6d0eb3 Compare April 26, 2026 18:18
@Ahmedaltu Ahmedaltu requested a review from ajzobro April 26, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: type errors in cli/testflinger_cli/auth.py detected by mypy

2 participants