-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[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
base: trunk
Are you sure you want to change the base?
[py] Fix type annotation error and raise clearer error message #16174
Conversation
…er.py (partially addresses SeleniumHQ#15697)
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
@olleolleolle hello sir |
will try to do more better ! |
if "rpId" not in data: | ||
raise KeyError("Missing required field 'rpId' in credential data.") | ||
rp_id = data["rpId"] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
st_value = None | ||
if isinstance(value, dict): | ||
st_value = value.get("stackTrace") or value.get("stacktrace") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 isinstance(message, dict): | ||
message = message.get("message") | ||
else: | ||
message = None |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
User description
🔗 Related Issues
Partially addresses #15697
💥 What does this PR do?
Fixes specific mypy type annotation errors in:
selenium/webdriver/common/virtual_authenticator.py
rpId
is missing inCredential.from_dict
, ensuringrp_id
is always a string.selenium/webdriver/remote/errorhandler.py
.get
on possibly-None values, so.get
is only used on dictionaries.🔧 Implementation Notes
isinstance(value, dict)
before using.get
to resolve mypy union-attr errors.from_dict
to requirerpId
and raise a clear error if missing, as discussed in project guidelines.💡 Additional Considerations
🔄 Types of changes
PR Type
Other
Description
Fix mypy type annotation errors in virtual authenticator
Add type checks before dictionary operations in error handler
Ensure required
rpId
field validation with clear error messageDiagram Walkthrough
File Walkthrough
virtual_authenticator.py
Validate required rpId field in Credential.from_dict
py/selenium/webdriver/common/virtual_authenticator.py
rpId
retrieval with required field validationrpId
is missing from credential datarp_id
is always a string typeerrorhandler.py
Add type checks before dictionary operations
py/selenium/webdriver/remote/errorhandler.py