Skip to content

Conversation

@nemowang2003
Copy link
Contributor

@nemowang2003 nemowang2003 commented Jan 22, 2026

User description

[py] Fix return type hint for alert_is_present

🔗 Related Issues

💥 What does this PR do?

Updates the return type hint for expected_conditions.alert_is_present.

Previously, alert_is_present was typed as returning Alert | bool.This union type prevented static type checkers from correctly inferring the return type as Alert when the method is successfully used inside WebDriverWait.until().

🔧 Implementation Notes

💡 Additional Considerations

🔄 Types of changes

  • Cleanup (formatting, renaming)

PR Type

Bug fix


Description

  • Fix return type hint for alert_is_present from Alert | bool to Alert | Literal[False]

  • Enables static type checkers to correctly infer Alert return type in WebDriverWait.until()

  • Improves type safety and IDE autocompletion for alert handling


Diagram Walkthrough

flowchart LR
  A["alert_is_present return type"] -->|"was: Alert | bool"| B["Ambiguous for type checkers"]
  A -->|"now: Alert | Literal[False]"| C["Correct type inference in WebDriverWait"]
Loading

File Walkthrough

Relevant files
Bug fix
expected_conditions.py
Update alert_is_present return type annotation                     

py/selenium/webdriver/support/expected_conditions.py

  • Changed return type annotation of alert_is_present() from
    Callable[[WebDriver], Alert | bool] to Callable[[WebDriver], Alert |
    Literal[False]]
  • Improves type checking accuracy by using Literal[False] instead of
    generic bool for the failure case
  • Enables static type checkers to properly narrow the return type to
    Alert when used with WebDriverWait.until()
+1/-1     

Copilot AI review requested due to automatic review settings January 22, 2026 07:05
@CLAassistant
Copy link

CLAassistant commented Jan 22, 2026

CLA assistant check
All committers have signed the CLA.

@selenium-ci selenium-ci added C-py Python Bindings B-support Issue or PR related to support classes labels Jan 22, 2026
@selenium-ci
Copy link
Member

Thank you, @nemowang2003 for this code suggestion.

The support packages contain example code that many users find helpful, but they do not necessarily represent
the best practices for using Selenium, and the Selenium team is not currently merging changes to them.

After reviewing the change, unless it is a critical fix or a feature that is needed for Selenium
to work, we will likely close the PR.

We actively encourage people to add the wrapper and helper code that makes sense for them to their own frameworks.
If you have any questions, please contact us

@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Jan 22, 2026

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🟡
🎫 #1234
🔴 Restore/fix behavior so that clicking a link whose href contains JavaScript triggers the
JavaScript (alert) as it did in Selenium 2.47.1, but fails in 2.48.x on Firefox 42.
Ensure the provided repro/test case produces the expected alert on the affected Firefox
setup.
🟡
🎫 #5678
🔴 Investigate and fix "Error: ConnectFailure (Connection refused)" occurring on subsequent
ChromeDriver instantiations on Ubuntu 16.04 / Chrome 65 / ChromeDriver 2.35 / Selenium
3.9.0.
Ensure multiple consecutive ChromeDriver instantiations do not emit the ConnectFailure
error in console/logs.
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Literal import check: The new return annotation uses Literal[False] and may cause a runtime/import-time failure
if Literal is not imported or if annotations are not postponed in this module.

Referred Code
def alert_is_present() -> Callable[[WebDriver], Alert | Literal[False]]:
    """Check that an alert is present and switch to it.

Learn more about managing compliance generic rules or creating your own custom rules

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Jan 22, 2026

PR Code Suggestions ✨

No code suggestions found for the PR.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a type hint issue in the Python bindings' expected conditions module. The return type of alert_is_present() is changed from Alert | bool to Alert | Literal[False] to enable proper static type inference when used with WebDriverWait.until().

Changes:

  • Updated the return type annotation for alert_is_present() from Alert | bool to Alert | Literal[False]

Copy link
Member

@cgoldberg cgoldberg left a comment

Choose a reason for hiding this comment

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

Thanks... good catch.
I just updated your branch so the docstring matches the return type.

@cgoldberg cgoldberg merged commit b35f2fa into SeleniumHQ:trunk Jan 22, 2026
28 checks passed
titusfortner pushed a commit that referenced this pull request Jan 22, 2026
---------

Co-authored-by: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com>
titusfortner pushed a commit that referenced this pull request Jan 23, 2026
---------

Co-authored-by: Corey Goldberg <1113081+cgoldberg@users.noreply.github.com>
@nemowang2003 nemowang2003 deleted the fix-ec-type-hint branch January 23, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-support Issue or PR related to support classes C-py Python Bindings Review effort 1/5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants