[py] Add type annotations to bidi network module - #16875
Conversation
Add comprehensive type hints to NetworkEvent, Network, and Request classes in the BiDi network module to improve code quality and enable better IDE support and static analysis.
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||
cgoldberg
left a comment
There was a problem hiding this comment.
Thanks.
There is a mypy error:
selenium\webdriver\remote\webdriver.py:1126: error: Argument 1 to "Network" has incompatible type "WebSocketConnection | None"; expected "WebSocketConnection" [arg-type]
Can you fix that?
|
I don't think the annotations I mentioned need to be forward references because they are already defined and shouldn't introduce circular imports? Edit: sorry, it's not a circular import you are trying to get around. I'm just looking at them quickly and don't see why they need to be forward references.. but if they do, you can leave them the way you had them. |
- Remove forward reference quotes from NetworkEvent and Request type hints - Add assertion for websocket connection type narrowing in webdriver.py
|
I used I think I saw it already in other places, Question: I could help with type annotation in other places, but I don't want to introduce a big PR. Doing 3-4 files in one PR would be fine? |
yes, that's fine. I don't mind either way, but smaller PR's are easier to review. |
|
weird.. the Docs failed to build with some import errors... I'm not sure why. I'll try to run them again. |
|
It's showing: It's because the annotation is referring to the class it's in. I guess that does need to be a forward reference. |
|
I think I know why I need to add now I removed quotes from a few annotations. On local, I got the same error. I will push a fix shortly |
User description
🔗 Related Issues
Contributes to ongoing Python type annotation improvements (see #16837, #16821)
💥 What does this PR do?
Adds comprehensive type annotations to the BiDi network module (
selenium/webdriver/common/bidi/network.py):__init__andfrom_jsonclassmethod🔧 Implementation Notes
Callablefromcollections.abcfor callback type hintsAnytype for complex JSON structures and flexible parameters (cookies, headers, timings)🔄 Types of changes
PR Type
Enhancement
Description
Adds comprehensive type annotations to BiDi network module classes
Annotates NetworkEvent, Network, and Request classes with type hints
Uses Callable, Any, and union types for flexible parameter handling
Improves IDE support and enables static type analysis
Diagram Walkthrough
File Walkthrough
network.py
Add type hints to network module classespy/selenium/webdriver/common/bidi/network.py
Callablefromcollections.abcandAnyfromtypingNetworkEvent.__init__andfrom_jsonclassmethod
Networkclass methods andinstance variables
Requestclass__init__and all manipulationmethods
|) for optional parameters and flexible typesAnytype for complex JSON structures and callback parameters