Skip to content
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

[py] Support RelativeBy in type annotations #15050

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

gryznar
Copy link

@gryznar gryznar commented Jan 8, 2025

Fixes: #14881

Description

This change adds support for RelativeBy in type annotations

Motivation and Context

Lack of these annotations causes linting issues

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@CLAassistant
Copy link

CLAassistant commented Jan 8, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

qodo-merge-pro bot commented Jan 8, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Type Safety

The LocatorType definition allows None as the second tuple element only with RelativeBy, but this constraint is not enforced by the type system. This could lead to runtime errors if None is passed with ByType.

LocatorType = Union[Tuple[ByType, str], Tuple[RelativeBy, None]]

Copy link
Contributor

qodo-merge-pro bot commented Jan 8, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible issue
Add support for RelativeBy locators in shadow root element search

The find_element method should handle RelativeBy locators by checking the type and
handling it appropriately, similar to the implementation in webdriver.py.

py/selenium/webdriver/remote/shadowroot.py [48-87]

 def find_element(self, by: Union[ByType, RelativeBy] = By.ID,
                  value: Optional[str] = None) -> WebElement:
+    if isinstance(by, RelativeBy):
+        elements = by.find_elements(self)
+        if not elements:
+            raise NoSuchElementException(f"Cannot locate relative element with: {by.root}")
+        return elements[0]
+
     if by == By.CLASS_NAME:
         by = By.CSS_SELECTOR
         value = f".{value}"
     elif by == By.NAME:
         by = By.CSS_SELECTOR
         value = f'[name="{value}"]'
 
     return self._execute(Command.FIND_ELEMENT_FROM_SHADOW_ROOT, {"using": by, "value": value})["value"]
  • Apply this suggestion
Suggestion importance[1-10]: 9

Why: The suggestion adds critical functionality to handle RelativeBy locators in shadow root searches, ensuring consistent behavior with the main WebDriver implementation and preventing potential failures when using relative locators.

9
Fix type hint to properly handle RelativeBy locators with values

The LocatorType type hint should handle the case where RelativeBy is used with a
non-None value parameter, as RelativeBy locators can have values. Update the type
hint to Union[Tuple[ByType, str], Tuple[RelativeBy, Optional[str]]].

py/selenium/webdriver/support/expected_conditions.py [48]

-LocatorType = Union[Tuple[ByType, str], Tuple[RelativeBy, None]]
+LocatorType = Union[Tuple[ByType, str], Tuple[RelativeBy, Optional[str]]]
  • Apply this suggestion
Suggestion importance[1-10]: 7

Why: The suggestion improves type safety by correctly representing that RelativeBy locators can have string values, making the type hint more accurate and preventing potential type checking issues.

7

Copy link

codecov bot commented Jan 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 58.86%. Comparing base (57f8398) to head (28d4f96).
Report is 1141 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk   #15050      +/-   ##
==========================================
+ Coverage   58.48%   58.86%   +0.38%     
==========================================
  Files          86       94       +8     
  Lines        5270     6012     +742     
  Branches      220      259      +39     
==========================================
+ Hits         3082     3539     +457     
- Misses       1968     2214     +246     
- Partials      220      259      +39     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@shbenzer
Copy link
Contributor

shbenzer commented Jan 9, 2025

@gryznar Thanks for the PR!

Please run scripts/format.sh to pass the formatting tests

@gryznar
Copy link
Author

gryznar commented Jan 9, 2025

Sure. I was not aware of this script and therefore I've formatted manually

@shbenzer
Copy link
Contributor

shbenzer commented Jan 9, 2025

Sure. I was not aware of this script and therefore I've formatted manually

No worries, I didn't know about it either until someone directed me to it

@gryznar gryznar force-pushed the annotate-relative-by branch from 28d4f96 to 474d3b7 Compare January 9, 2025 23:04
@gryznar
Copy link
Author

gryznar commented Jan 9, 2025

Please run scripts/format.sh to pass the formatting tests

@shbenzer Done

@gryznar gryznar force-pushed the annotate-relative-by branch from 474d3b7 to d8c871a Compare January 9, 2025 23:19
@gryznar
Copy link
Author

gryznar commented Jan 9, 2025

I've also reverted changes in shadowroot.py, because they were causing circular imports

@VietND96 VietND96 changed the title Support RelativeBy in type annotations [py] Support RelativeBy in type annotations Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: Missing support for RelativeBy in annotations
4 participants