Skip to content

Commit

Permalink
Replace direct type comparisons with isinstance (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
delatrie authored Aug 8, 2023
1 parent 7b5f698 commit 200b717
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
14 changes: 11 additions & 3 deletions allure-behave/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ def get_hook_name(name, parameters):
def step_status_details(result):
if result.exception:
# workaround for https://github.com/behave/behave/pull/616
trace = "\n".join(result.exc_traceback) if type(result.exc_traceback) == list else format_traceback(
result.exc_traceback)
return StatusDetails(message=format_exception(type(result.exception), result.exception), trace=trace)
trace = "\n".join(result.exc_traceback) if isinstance(
result.exc_traceback,
list
) else format_traceback(result.exc_traceback)
return StatusDetails(
message=format_exception(
type(result.exception),
result.exception
),
trace=trace
)

elif result.status == 'undefined':
message = '\nYou can implement step definitions for undefined steps with these snippets:\n\n'
Expand Down
2 changes: 1 addition & 1 deletion allure-nose2/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _get_attrs(obj, keys):
pairs = set()
for key in keys:
values = getattr(obj, key, ())
for value in (values,) if type(values) == str else values:
for value in (values,) if isinstance(values, str) else values:
pairs.add((key, value))
return pairs

Expand Down
4 changes: 2 additions & 2 deletions allure-python-commons/src/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _last_item_uuid(self, item_type=None):
item = self._items.get(uuid)
if item_type is None:
return uuid
elif type(item) == item_type or isinstance(item, item_type):
elif isinstance(item, item_type):
return uuid

@contextmanager
Expand Down Expand Up @@ -75,7 +75,7 @@ def start_container(self, uuid=None):

def containers(self):
for item in self._items.values():
if type(item) == TestResultContainer:
if isinstance(item, TestResultContainer):
yield item

@contextmanager
Expand Down
11 changes: 3 additions & 8 deletions allure-python-commons/src/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ def __init__(self, report_dir, clean=False):
def _report_item(self, item):
indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None
filename = item.file_pattern.format(prefix=uuid.uuid4())
data = asdict(
item,
filter=lambda attr, value: not (
type(value) != bool and not bool(value)
)
)
data = asdict(item, filter=lambda _, v: v or v is False)
with io.open(self._report_dir / filename, 'w', encoding='utf8') as json_file:
json.dump(data, json_file, indent=indent, ensure_ascii=False)

Expand Down Expand Up @@ -62,12 +57,12 @@ def __init__(self):

@hookimpl
def report_result(self, result):
data = asdict(result, filter=lambda attr, value: not (type(value) != bool and not bool(value)))
data = asdict(result, filter=lambda _, v: v or v is False)
self.test_cases.append(data)

@hookimpl
def report_container(self, container):
data = asdict(container, filter=lambda attr, value: not (type(value) != bool and not bool(value)))
data = asdict(container, filter=lambda _, v: v or v is False)
self.test_containers.append(data)

@hookimpl
Expand Down
2 changes: 1 addition & 1 deletion allure-python-commons/src/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_last_item(self, item_type=None):
for _uuid in reversed(self._items):
if item_type is None:
return self._items.get(_uuid)
if type(self._items[_uuid]) == item_type:
if isinstance(self._items[_uuid], item_type):
return self._items.get(_uuid)

def start_group(self, uuid, group):
Expand Down

0 comments on commit 200b717

Please sign in to comment.