-
Notifications
You must be signed in to change notification settings - Fork 775
Improve type hints #1666
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
Improve type hints #1666
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,8 @@ | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Union | ||
from datetime import timedelta | ||
from typing import Union, Optional | ||
|
||
from selenium.common.exceptions import TimeoutException, WebDriverException | ||
from selenium.webdriver.support import expected_conditions as EC | ||
|
@@ -31,7 +32,7 @@ class AlertKeywords(LibraryComponent): | |
|
||
@keyword | ||
def input_text_into_alert( | ||
self, text: str, action: str = ACCEPT, timeout: Union[float, str, None] = None | ||
self, text: str, action: str = ACCEPT, timeout: Optional[timedelta] = None | ||
): | ||
"""Types the given ``text`` into an input field in an alert. | ||
|
||
|
@@ -52,7 +53,7 @@ def alert_should_be_present( | |
self, | ||
text: str = "", | ||
action: str = ACCEPT, | ||
timeout: Union[float, str, None] = None, | ||
timeout: Optional[timedelta] = None, | ||
): | ||
"""Verifies that an alert is present and by default, accepts it. | ||
|
||
|
@@ -68,6 +69,7 @@ def alert_should_be_present( | |
In earlier versions, the alert was always accepted and a timeout was | ||
hardcoded to one second. | ||
""" | ||
self.info(f"{type(timeout)}::{timeout}") | ||
message = self.handle_alert(action, timeout) | ||
if text and text != message: | ||
raise AssertionError( | ||
|
@@ -76,7 +78,7 @@ def alert_should_be_present( | |
|
||
@keyword | ||
def alert_should_not_be_present( | ||
self, action: str = ACCEPT, timeout: Union[float, str, None] = 0 | ||
self, action: str = ACCEPT, timeout: Union[timedelta] = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong because an argument with type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, what would be suitable default? The easiest would be to change it to
be better? |
||
): | ||
"""Verifies that no alert is present. | ||
|
||
|
@@ -101,7 +103,7 @@ def alert_should_not_be_present( | |
|
||
@keyword | ||
def handle_alert( | ||
self, action: str = ACCEPT, timeout: Union[float, str, None] = None | ||
self, action: str = ACCEPT, timeout: Optional[timedelta] = None | ||
): | ||
"""Handles the current alert and returns its message. | ||
|
||
|
@@ -127,6 +129,7 @@ def handle_alert( | |
|
||
New in SeleniumLibrary 3.0. | ||
""" | ||
self.info(f"HANDLE::{type(timeout)}::{timeout}") | ||
alert = self._wait_alert(timeout) | ||
return self._handle_alert(alert, action) | ||
|
||
|
@@ -143,6 +146,7 @@ def _handle_alert(self, alert, action): | |
|
||
def _wait_alert(self, timeout=None): | ||
timeout = self.get_timeout(timeout) | ||
self.info(f"WAITING::{type(timeout)}::{timeout}") | ||
wait = WebDriverWait(self.driver, timeout) | ||
try: | ||
return wait.until(EC.alert_is_present()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from datetime import timedelta | ||
|
||
import pytest | ||
from mockito import mock, unstub | ||
|
||
from SeleniumLibrary.base import LibraryComponent | ||
|
||
|
||
@pytest.fixture | ||
def lib(): | ||
ctx = mock() | ||
ctx.timeout = 5.0 | ||
return LibraryComponent(ctx) | ||
|
||
|
||
def teardown_function(): | ||
unstub() | ||
|
||
|
||
def test_timeout_as_none(lib: LibraryComponent): | ||
assert lib.get_timeout(None) == 5.0 | ||
|
||
|
||
def test_timeout_as_float(lib: LibraryComponent): | ||
assert lib.get_timeout(1.0) == 1.0 | ||
|
||
|
||
def test_timeout_as_timedelta(lib: LibraryComponent): | ||
assert lib.get_timeout(timedelta(seconds=0.1)) == 0.1 |
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.
Optional[]
is unnecessary when there'sNone
as a default value. Even MyPy doesn't need it unless you run it with--no-implicit-optional
:https://mypy.readthedocs.io/en/stable/command_line.html?highlight=optional#cmdoption-mypy-no-implicit-optional
Uh oh!
There was an error while loading. Please reload this page.
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 think Optional is best practice if None is used. Types should not contain None as part of the type and if they do, then that should explicitly be stated.
See for example https://en.wikipedia.org/wiki/Void_safety for more details on this subject.
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.
And the default is also changing in mypy python/mypy#9091