Skip to content

Commit

Permalink
All is None and is not None removal (#8963)
Browse files Browse the repository at this point in the history
Co-authored-by: machinexa2 <machinexa>
Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
  • Loading branch information
Machinexa2 and AutomatedTester committed Dec 15, 2020
1 parent 6ac3db6 commit 0e0194b
Show file tree
Hide file tree
Showing 32 changed files with 96 additions and 98 deletions.
4 changes: 2 additions & 2 deletions py/selenium/common/exceptions.py
Expand Up @@ -32,9 +32,9 @@ def __init__(self, msg=None, screen=None, stacktrace=None):

def __str__(self):
exception_msg = "Message: %s\n" % self.msg
if self.screen is not None:
if self.screen:
exception_msg += "Screenshot: available via screen\n"
if self.stacktrace is not None:
if self.stacktrace:
stacktrace = "\n".join(self.stacktrace)
exception_msg += "Stacktrace:\n%s" % stacktrace
return exception_msg
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/chrome/webdriver.py
Expand Up @@ -58,7 +58,7 @@ def __init__(self, executable_path="chromedriver", port=DEFAULT_PORT,
DeprecationWarning, stacklevel=2)
options = chrome_options

if service is None:
if not service:
service = Service(executable_path, port, service_args, service_log_path)

super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/chromium/service.py
Expand Up @@ -38,7 +38,7 @@ def __init__(self, executable_path, port=0, service_args=None,
if log_path:
self.service_args.append('--log-path=%s' % log_path)

if start_error_message is None:
if not start_error_message:
raise AttributeError("start_error_message should not be empty")

service.Service.__init__(self, executable_path, port=port, env=env, start_error_message=start_error_message)
Expand Down
10 changes: 5 additions & 5 deletions py/selenium/webdriver/chromium/webdriver.py
Expand Up @@ -48,7 +48,7 @@ def __init__(self, browser_name, vendor_prefix,
- service_log_path - Deprecated: Where to log information from the driver.
- keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
"""
if desired_capabilities is not None:
if desired_capabilities:
warnings.warn('desired_capabilities has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if port != DEFAULT_PORT:
Expand All @@ -60,12 +60,12 @@ def __init__(self, browser_name, vendor_prefix,
DeprecationWarning, stacklevel=2)

_ignore_proxy = None
if options is None:
if not options:
# desired_capabilities stays as passed in
if desired_capabilities is None:
if not desired_capabilities:
desired_capabilities = self.create_options().to_capabilities()
else:
if desired_capabilities is None:
if not desired_capabilities:
desired_capabilities = options.to_capabilities()
else:
desired_capabilities.update(options.to_capabilities())
Expand All @@ -74,7 +74,7 @@ def __init__(self, browser_name, vendor_prefix,

self.vendor_prefix = vendor_prefix

if service is None:
if not service:
raise AttributeError('service cannot be None')

self.service = service
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/common/actions/action_builder.py
Expand Up @@ -25,9 +25,9 @@

class ActionBuilder(object):
def __init__(self, driver, mouse=None, keyboard=None):
if mouse is None:
if not mouse:
mouse = PointerInput(interaction.POINTER_MOUSE, "mouse")
if keyboard is None:
if not keyboard:
keyboard = KeyInput(interaction.KEY)
self.devices = [mouse, keyboard]
self._key_action = KeyActions(keyboard)
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/common/actions/input_device.py
Expand Up @@ -23,7 +23,7 @@ class InputDevice(object):
Describes the input device being used for the action.
"""
def __init__(self, name=None):
if name is None:
if not name:
self.name = uuid.uuid4()
else:
self.name = name
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/common/actions/key_actions.py
Expand Up @@ -22,7 +22,7 @@
class KeyActions(Interaction):

def __init__(self, source=None):
if source is None:
if not source:
source = KeyInput(KEY)
self.source = source
super(KeyActions, self).__init__(source)
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/common/actions/pointer_actions.py
Expand Up @@ -26,7 +26,7 @@
class PointerActions(Interaction):

def __init__(self, source=None):
if source is None:
if not source:
source = PointerInput(interaction.POINTER_MOUSE, "mouse")
self.source = source
super(PointerActions, self).__init__(source)
Expand All @@ -40,7 +40,7 @@ def pointer_up(self, button=MouseButton.LEFT):
def move_to(self, element, x=None, y=None):
if not isinstance(element, WebElement):
raise AttributeError("move_to requires a WebElement")
if x is not None or y is not None:
if x or y:
el_rect = element.rect
left_offset = el_rect['width'] / 2
top_offset = el_rect['height'] / 2
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/common/actions/pointer_input.py
Expand Up @@ -39,7 +39,7 @@ def create_pointer_move(self, duration=DEFAULT_MOVE_DURATION, x=None, y=None, or
action["y"] = y
if isinstance(origin, WebElement):
action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
elif origin is not None:
elif origin:
action["origin"] = origin

self.add_action(action)
Expand Down
29 changes: 13 additions & 16 deletions py/selenium/webdriver/common/proxy.py
Expand Up @@ -54,10 +54,7 @@ def load(cls, value):
value = str(value).upper()
for attr in dir(cls):
attr_value = getattr(cls, attr)
if isinstance(attr_value, dict) and \
'string' in attr_value and \
attr_value['string'] is not None and \
attr_value['string'] == value:
if isinstance(attr_value, dict) and 'string' in attr_value and attr_value['string'] == value:
return attr_value
raise Exception(f"No proxy type is found for {value}")

Expand Down Expand Up @@ -86,28 +83,28 @@ def __init__(self, raw=None):
:Args:
- raw: raw proxy data. If None, default class values are used.
"""
if raw is not None:
if 'proxyType' in raw and raw['proxyType'] is not None:
if raw:
if 'proxyType' in raw and raw['proxyType']:
self.proxy_type = ProxyType.load(raw['proxyType'])
if 'ftpProxy' in raw and raw['ftpProxy'] is not None:
if 'ftpProxy' in raw and raw['ftpProxy']:
self.ftp_proxy = raw['ftpProxy']
if 'httpProxy' in raw and raw['httpProxy'] is not None:
if 'httpProxy' in raw and raw['httpProxy']:
self.http_proxy = raw['httpProxy']
if 'noProxy' in raw and raw['noProxy'] is not None:
if 'noProxy' in raw and raw['noProxy']:
self.no_proxy = raw['noProxy']
if 'proxyAutoconfigUrl' in raw and raw['proxyAutoconfigUrl'] is not None:
if 'proxyAutoconfigUrl' in raw and raw['proxyAutoconfigUrl']:
self.proxy_autoconfig_url = raw['proxyAutoconfigUrl']
if 'sslProxy' in raw and raw['sslProxy'] is not None:
if 'sslProxy' in raw and raw['sslProxy']:
self.sslProxy = raw['sslProxy']
if 'autodetect' in raw and raw['autodetect'] is not None:
if 'autodetect' in raw and raw['autodetect']:
self.auto_detect = raw['autodetect']
if 'socksProxy' in raw and raw['socksProxy'] is not None:
if 'socksProxy' in raw and raw['socksProxy']:
self.socks_proxy = raw['socksProxy']
if 'socksUsername' in raw and raw['socksUsername'] is not None:
if 'socksUsername' in raw and raw['socksUsername']:
self.socks_username = raw['socksUsername']
if 'socksPassword' in raw and raw['socksPassword'] is not None:
if 'socksPassword' in raw and raw['socksPassword']:
self.socks_password = raw['socksPassword']
if 'socksVersion' in raw and raw['socksVersion'] is not None:
if 'socksVersion' in raw and raw['socksVersion']:
self.socks_version = raw['socksVersion']

@property
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/common/service.py
Expand Up @@ -109,7 +109,7 @@ def start(self):

def assert_process_still_running(self):
return_code = self.process.poll()
if return_code is not None:
if return_code:
raise WebDriverException(
'Service %s unexpectedly exited. Status code was: %s'
% (self.path, return_code)
Expand Down Expand Up @@ -148,7 +148,7 @@ def stop(self):
except Exception:
pass

if self.process is None:
if not self.process:
return

try:
Expand Down
8 changes: 4 additions & 4 deletions py/selenium/webdriver/common/timeouts.py
Expand Up @@ -77,19 +77,19 @@ def script(self, _script):
self._script = self._convert(_script)

def _convert(self, timeout):
if timeout is not None:
if timeout:
if isinstance(timeout, (int, float)):
return int(float(timeout) * 1000)
else:
raise TypeError("Timeouts can only be an int or a float")

def _to_json(self):
timeouts = {}
if self._implicit_wait is not None:
if self._implicit_wait:
timeouts["implicit"] = self._implicit_wait
if self._page_load is not None:
if self._page_load:
timeouts["pageLoad"] = self._page_load
if self._script is not None:
if self._script:
timeouts["script"] = self._script

return timeouts
4 changes: 2 additions & 2 deletions py/selenium/webdriver/firefox/extension_connection.py
Expand Up @@ -37,10 +37,10 @@ def __init__(self, host, firefox_profile, firefox_binary=None, timeout=30):
HOST = host
timeout = int(timeout)

if self.binary is None:
if not self.binary:
self.binary = FirefoxBinary()

if HOST is None:
if not HOST:
HOST = "127.0.0.1"

PORT = utils.free_port()
Expand Down
8 changes: 4 additions & 4 deletions py/selenium/webdriver/firefox/firefox_profile.py
Expand Up @@ -66,7 +66,7 @@ def __init__(self, profile_directory=None):
FirefoxProfile.DEFAULT_PREFERENCES['mutable'])
self.profile_dir = profile_directory
self.tempfolder = None
if self.profile_dir is None:
if not self.profile_dir:
self.profile_dir = self._create_tempfolder()
else:
self.tempfolder = tempfile.mkdtemp()
Expand Down Expand Up @@ -340,14 +340,14 @@ def parse_manifest_json(content):
rdf = get_namespace_id(doc, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')

description = doc.getElementsByTagName(rdf + 'Description').item(0)
if description is None:
if not description:
description = doc.getElementsByTagName('Description').item(0)
for node in description.childNodes:
# Remove the namespace prefix from the tag for comparison
entry = node.nodeName.replace(em, "")
if entry in details.keys():
details.update({entry: get_text(node)})
if details.get('id') is None:
if not details.get('id'):
for i in range(description.attributes.length):
attribute = description.attributes.item(i)
if attribute.name == em + 'id':
Expand All @@ -360,7 +360,7 @@ def parse_manifest_json(content):
details['unpack'] = details['unpack'].lower() == 'true'

# If no ID is set, the add-on is invalid
if details.get('id') is None:
if not details.get('id'):
raise AddonFormatError('Add-on id could not be found.')

return details
3 changes: 2 additions & 1 deletion py/selenium/webdriver/firefox/service.py
Expand Up @@ -41,7 +41,8 @@ def __init__(self, executable_path, port=0, service_args=None,
in the services' environment.
"""
log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
#the condition will fail on both "" and on None, so no need of `log_path is not None and log_path != ""`
log_file = open(log_path, "a+") if log_path else None

service.Service.__init__(
self, executable_path, port=port, log_file=log_file, env=env)
Expand Down
31 changes: 15 additions & 16 deletions py/selenium/webdriver/firefox/webdriver.py
Expand Up @@ -20,7 +20,6 @@
basestring = str

from base64 import b64decode
import shutil
from shutil import rmtree
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -104,14 +103,14 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
if executable_path != DEFAULT_EXECUTABLE_PATH:
warnings.warn('executable_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if capabilities is not None or desired_capabilities is not None:
if capabilities or desired_capabilities:
warnings.warn('capabilities and desired_capabilities have been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if firefox_binary is not None:
if firefox_binary:
warnings.warn('firefox_binary has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
self.binary = None
if firefox_profile is not None:
if firefox_profile:
warnings.warn('firefox_profile has been deprecated, please pass in an Options object',
DeprecationWarning, stacklevel=2)
self.profile = None
Expand All @@ -124,20 +123,20 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
if service_log_path != DEFAULT_SERVICE_LOG_PATH:
warnings.warn('service_log_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if service_args is not None:
if service_args:
warnings.warn('service_args has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)

self.service = service

# If desired capabilities is set, alias it to capabilities.
# If both are set ignore desired capabilities.
if capabilities is None and desired_capabilities:
if not capabilities and desired_capabilities:
capabilities = desired_capabilities

if capabilities is None:
if not capabilities:
capabilities = DesiredCapabilities.FIREFOX.copy()
if options is None:
if not options:
options = Options()

capabilities = dict(capabilities)
Expand All @@ -146,20 +145,20 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
self.binary = capabilities["binary"]

# options overrides capabilities
if options is not None:
if options.binary is not None:
if options:
if options.binary:
self.binary = options.binary
if options.profile is not None:
if options.profile:
self.profile = options.profile

# firefox_binary and firefox_profile
# override options
if firefox_binary is not None:
if firefox_binary:
if isinstance(firefox_binary, basestring):
firefox_binary = FirefoxBinary(firefox_binary)
self.binary = firefox_binary
options.binary = firefox_binary
if firefox_profile is not None:
if firefox_profile:
if isinstance(firefox_profile, basestring):
firefox_profile = FirefoxProfile(firefox_profile)
self.profile = firefox_profile
Expand All @@ -171,7 +170,7 @@ def __init__(self, firefox_profile=None, firefox_binary=None,
if capabilities.get("acceptInsecureCerts"):
options.accept_insecure_certs = capabilities.get("acceptInsecureCerts")

if self.service is None:
if not self.service:
self.service = Service(
executable_path,
service_args=service_args,
Expand Down Expand Up @@ -204,10 +203,10 @@ def quit(self):
else:
self.binary.kill()

if self.profile is not None:
if self.profile:
try:
rmtree(self.profile.path)
if self.profile.tempfolder is not None:
if self.profile.tempfolder:
rmtree(self.profile.tempfolder)
except Exception as e:
print(str(e))
Expand Down
6 changes: 3 additions & 3 deletions py/selenium/webdriver/ie/service.py
Expand Up @@ -36,11 +36,11 @@ def __init__(self, executable_path, port=0, host=None, log_level=None, log_file=
- log_file : Target of logging of service, may be "stdout", "stderr" or file path.
Default is "stdout"."""
self.service_args = []
if host is not None:
if host:
self.service_args.append("--host=%s" % host)
if log_level is not None:
if log_level:
self.service_args.append("--log-level=%s" % log_level)
if log_file is not None:
if log_file:
self.service_args.append("--log-file=%s" % log_file)

service.Service.__init__(self, executable_path, port=port,
Expand Down

0 comments on commit 0e0194b

Please sign in to comment.