Use built-in suppress from contextlib

In python3, we can use the built-in suppress from contextlib

https://docs.python.org/3/library/contextlib.html#contextlib.suppress
This commit is contained in:
Jake McDermott 2021-04-09 00:22:20 -04:00
parent c72cc6486c
commit 2c61e8f6de
No known key found for this signature in database
GPG Key ID: 0E56ED990CDFCB4F
15 changed files with 33 additions and 65 deletions

View File

@ -1,4 +1,5 @@
from awxkit.utils import suppress
from contextlib import suppress
import awxkit.exceptions as exc

View File

@ -1,4 +1,5 @@
from awxkit.utils import suppress
from contextlib import suppress
import awxkit.exceptions as exc

View File

@ -1,4 +1,6 @@
from awxkit.utils import PseudoNamespace, random_title, suppress, update_payload, set_payload_foreign_key_args
from contextlib import suppress
from awxkit.utils import PseudoNamespace, random_title, update_payload, set_payload_foreign_key_args
from awxkit.api.resources import resources
from awxkit.api.mixins import HasCreate
import awxkit.exceptions as exc

View File

@ -1,9 +1,10 @@
from contextlib import suppress
import logging
import json
import re
from awxkit.api.pages import Credential, Organization, Project, UnifiedJob, UnifiedJobTemplate
from awxkit.utils import filter_by_class, random_title, update_payload, suppress, not_provided, PseudoNamespace, poll_until, random_utf8
from awxkit.utils import filter_by_class, random_title, update_payload, not_provided, PseudoNamespace, poll_until, random_utf8
from awxkit.api.mixins import DSAdapter, HasCreate, HasInstanceGroups, HasNotifications, HasVariables, HasCopy
from awxkit.api.resources import resources
import awxkit.exceptions as exc

View File

@ -1,6 +1,7 @@
from contextlib import suppress
import json
from awxkit.utils import filter_by_class, not_provided, random_title, suppress, update_payload, set_payload_foreign_key_args, PseudoNamespace
from awxkit.utils import filter_by_class, not_provided, random_title, update_payload, set_payload_foreign_key_args, PseudoNamespace
from awxkit.api.pages import Credential, Inventory, Project, UnifiedJobTemplate
from awxkit.api.mixins import HasCreate, HasInstanceGroups, HasNotifications, HasSurvey, HasCopy, DSAdapter
from awxkit.api.resources import resources

View File

@ -1,9 +1,11 @@
from contextlib import suppress
from awxkit.api.mixins import HasCreate, HasCopy, DSAdapter
from awxkit.api.pages import Organization
from awxkit.api.resources import resources
from awxkit.config import config
import awxkit.exceptions as exc
from awxkit.utils import not_provided, random_title, suppress, PseudoNamespace
from awxkit.utils import not_provided, random_title, PseudoNamespace
from . import base
from . import page

View File

@ -1,5 +1,7 @@
from contextlib import suppress
from awxkit.api.mixins import HasCreate, HasInstanceGroups, HasNotifications, DSAdapter
from awxkit.utils import random_title, suppress, set_payload_foreign_key_args, PseudoNamespace
from awxkit.utils import random_title, set_payload_foreign_key_args, PseudoNamespace
from awxkit.api.resources import resources
import awxkit.exceptions as exc
from . import base

View File

@ -1,3 +1,4 @@
from contextlib import suppress
import inspect
import logging
import json
@ -6,7 +7,7 @@ import re
from requests import Response
import http.client as http
from awxkit.utils import PseudoNamespace, is_relative_endpoint, are_same_endpoint, super_dir_set, suppress, is_list_or_tuple, to_str
from awxkit.utils import PseudoNamespace, is_relative_endpoint, are_same_endpoint, super_dir_set, is_list_or_tuple, to_str
from awxkit.api import utils
from awxkit.api.client import Connection
from awxkit.api.registry import URLRegistry

View File

@ -1,7 +1,8 @@
from contextlib import suppress
from awxkit.api.pages import UnifiedJob
from awxkit.api.resources import resources
import awxkit.exceptions as exc
from awxkit.utils import suppress
from . import page
from . import base

View File

@ -1,5 +1,7 @@
from contextlib import suppress
from awxkit.api.mixins import HasCreate, DSAdapter
from awxkit.utils import suppress, random_title, PseudoNamespace
from awxkit.utils import random_title, PseudoNamespace
from awxkit.api.resources import resources
from awxkit.api.pages import Organization
from awxkit.exceptions import NoContent

View File

@ -1,9 +1,10 @@
import awxkit.exceptions as exc
from contextlib import suppress
import awxkit.exceptions as exc
from awxkit.api.pages import base, WorkflowJobTemplate, UnifiedJobTemplate, JobTemplate
from awxkit.api.mixins import HasCreate, DSAdapter
from awxkit.api.resources import resources
from awxkit.utils import update_payload, PseudoNamespace, suppress, random_title
from awxkit.utils import update_payload, PseudoNamespace, random_title
from . import page

View File

@ -1,8 +1,9 @@
from contextlib import suppress
import json
from awxkit.api.mixins import HasCreate, HasNotifications, HasSurvey, HasCopy, DSAdapter
from awxkit.api.pages import Organization, UnifiedJobTemplate
from awxkit.utils import filter_by_class, not_provided, update_payload, random_title, suppress, PseudoNamespace
from awxkit.utils import filter_by_class, not_provided, update_payload, random_title, PseudoNamespace
from awxkit.api.resources import resources
import awxkit.exceptions as exc

View File

@ -1,4 +1,4 @@
import contextlib
from contextlib import contextmanager, suppress
from awxkit import api, utils, exceptions
from awxkit.config import config
@ -56,7 +56,7 @@ def check_related(resource):
if related in examined:
continue
print(related)
with utils.suppress(exceptions.NotFound):
with suppress(exceptions.NotFound):
child_related = related.get()
examined.append(related)
if 'results' in child_related and child_related.results:
@ -66,12 +66,12 @@ def check_related(resource):
if not isinstance(_related, api.page.TentativePage) or _related in examined:
continue
print(_related)
with utils.suppress(exceptions.NotFound):
with suppress(exceptions.NotFound):
_related.get()
examined.append(_related)
@contextlib.contextmanager
@contextmanager
def as_user(v, username, password=None):
"""Context manager to allow running tests as an alternative login user."""
access_token = False

View File

@ -362,28 +362,6 @@ def are_same_endpoint(first, second):
return strip(first) == strip(second)
@contextmanager
def suppress(*exceptions):
"""Context manager that suppresses the provided exceptions
:param exceptions: List of exceptions to suppress
Usage::
>>> with suppress(ZeroDivisionError):
>>> foo = 1/0
>>> # This code will not run
Note: This is an intermediate framework and test refactoring tool.
It's almost never a good idea to plan on using this. Also, note
that after the suppressed exception has been caught, no further
statements in the with block will be executed.
"""
try:
yield
except exceptions:
pass
def utcnow():
"""Provide a wrapped copy of the built-in utcnow that can be easily mocked."""
return datetime.utcnow()

View File

@ -216,32 +216,6 @@ class RecordingCallback(object):
return self.value
def test_suppress():
callback = RecordingCallback()
with utils.suppress(ZeroDivisionError, IndexError):
raise ZeroDivisionError
callback()
raise IndexError
raise KeyError
assert callback.call_count == 0
with utils.suppress(ZeroDivisionError, IndexError):
raise IndexError
callback()
raise ZeroDivisionError
raise KeyError
assert callback.call_count == 0
with pytest.raises(KeyError):
with utils.suppress(ZeroDivisionError, IndexError):
raise KeyError
callback()
raise ZeroDivisionError
raise IndexError
assert callback.call_count == 0
class TestPollUntil(object):
@pytest.mark.parametrize('timeout', [0, 0.0, -0.5, -1, -9999999])
def test_callback_called_once_for_non_positive_timeout(self, timeout):