Skip to content

[py] Fix type annotation error and raise clearer error message #16174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion py/selenium/webdriver/common/virtual_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def to_dict(self) -> dict[str, Any]:
def from_dict(cls, data: dict[str, Any]) -> "Credential":
_id = urlsafe_b64decode(f"{data['credentialId']}==")
is_resident_credential = bool(data["isResidentCredential"])
rp_id = data.get("rpId", None)
if "rpId" not in data:
raise KeyError("Missing required field 'rpId' in credential data.")
rp_id = data["rpId"]
Comment on lines +183 to +185
Copy link
Member

Choose a reason for hiding this comment

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

Should we directly use data["rpId"] instead of raising an error since it is a required field and is expected to be always present?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! I added the explicit check to provide a clearer error message if rpId is missing, but I agree that directly accessing data["rpId"] would automatically raise a KeyError if it’s absent. I’m happy to update this to remove the manual check and rely on the built-in exception for missing keys, if that’s preferred for consistency with the rest of the codebase.

Copy link
Member

Choose a reason for hiding this comment

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

Sure

private_key = urlsafe_b64decode(f"{data['privateKey']}==")
sign_count = int(data["signCount"])
user_handle = urlsafe_b64decode(f"{data['userHandle']}==") if data.get("userHandle", None) else None
Expand Down
9 changes: 7 additions & 2 deletions py/selenium/webdriver/remote/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ def check_response(self, response: dict[str, Any]) -> None:
message = value.get("value") or value.get("message")
if not isinstance(message, str):
value = message
message = message.get("message")
if isinstance(message, dict):
message = message.get("message")
else:
message = None
Comment on lines +176 to +179
Copy link
Member

Choose a reason for hiding this comment

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

We can replace with a better and shorter check:

		message = message.get("message") if message is not None else None

Copy link
Author

Choose a reason for hiding this comment

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

Great suggestion! I’ll update this to use the shorter one-liner as you proposed. It’s definitely more concise and readable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Those aren't equivalent. You would want:

message = message.get("message") if isinstance(message, dict) else None

else:
message = value.get("message", None)
except ValueError:
Expand Down Expand Up @@ -202,7 +205,9 @@ def check_response(self, response: dict[str, Any]) -> None:
screen = value["screen"]

stacktrace = None
st_value = value.get("stackTrace") or value.get("stacktrace")
st_value = None
if isinstance(value, dict):
st_value = value.get("stackTrace") or value.get("stacktrace")
Comment on lines +208 to +210
Copy link
Member

Choose a reason for hiding this comment

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

This instance check is not required since we are taking care of it below. It won't raise any mypy type errors.

Copy link
Author

Choose a reason for hiding this comment

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

I’ll remove the redundant isinstance check here since the code already ensures the right type, and it shouldn’t affect mypy or runtime behavior.

if st_value:
if isinstance(st_value, str):
stacktrace = st_value.split("\n")
Expand Down
Loading