Skip to content

Commit

Permalink
Use set literals
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 3, 2021
1 parent 12c5fcd commit 7ff93a9
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion coverage/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Collector(object):
_collectors = []

# The concurrency settings we support here.
SUPPORTED_CONCURRENCIES = set(["greenlet", "eventlet", "gevent", "thread"])
SUPPORTED_CONCURRENCIES = {"greenlet", "eventlet", "gevent", "thread"}

def __init__(
self, should_trace, check_include, should_start_context, file_mapper,
Expand Down
2 changes: 1 addition & 1 deletion coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, cov):
data = self.coverage.get_data()
self.has_arcs = data.has_arcs()
if self.config.show_contexts:
if data.measured_contexts() == set([""]):
if data.measured_contexts() == {""}:
self.coverage._warn("No contexts were measured")
data.set_query_contexts(self.config.report_contexts)

Expand Down
2 changes: 1 addition & 1 deletion coverage/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def new_contract(*args, **kwargs):
def one_of(argnames):
"""Ensure that only one of the argnames is non-None."""
def _decorator(func):
argnameset = set(name.strip() for name in argnames.split(","))
argnameset = {name.strip() for name in argnames.split(",")}
def _wrapper(*args, **kwargs):
vals = [kwargs.get(name) for name in argnameset]
assert sum(val is not None for val in vals) == 1
Expand Down
18 changes: 9 additions & 9 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def first_lines(self, lines):
Returns a set of the first lines.
"""
return set(self.first_line(l) for l in lines)
return {self.first_line(l) for l in lines}

def translate_lines(self, lines):
"""Implement `FileReporter.translate_lines`."""
Expand Down Expand Up @@ -520,7 +520,7 @@ class AstArcAnalyzer(object):
def __init__(self, text, statements, multiline):
self.root_node = ast.parse(neuter_encoding_declaration(text))
# TODO: I think this is happening in too many places.
self.statements = set(multiline.get(l, l) for l in statements)
self.statements = {multiline.get(l, l) for l in statements}
self.multiline = multiline

if AST_DUMP: # pragma: debugging
Expand Down Expand Up @@ -626,10 +626,10 @@ def _line__Module(self, node):
return 1

# The node types that just flow to the next node with no complications.
OK_TO_DEFAULT = set([
OK_TO_DEFAULT = {
"Assign", "Assert", "AugAssign", "Delete", "Exec", "Expr", "Global",
"Import", "ImportFrom", "Nonlocal", "Pass", "Print",
])
}

@contract(returns='ArcStarts')
def add_arcs(self, node):
Expand Down Expand Up @@ -661,7 +661,7 @@ def add_arcs(self, node):
print("*** Unhandled: {}".format(node))

# Default for simple statements: one exit from this node.
return set([ArcStart(self.line_for_node(node))])
return {ArcStart(self.line_for_node(node))}

@one_of("from_start, prev_starts")
@contract(returns='ArcStarts')
Expand All @@ -677,7 +677,7 @@ def add_body_arcs(self, body, from_start=None, prev_starts=None):
"""
if prev_starts is None:
prev_starts = set([from_start])
prev_starts = {from_start}
for body_node in body:
lineno = self.line_for_node(body_node)
first_line = self.multiline.get(lineno, lineno)
Expand Down Expand Up @@ -890,7 +890,7 @@ def _handle_decorated(self, node):
self.add_arc(last, lineno)
last = lineno
# The body is handled in collect_arcs.
return set([ArcStart(last)])
return {ArcStart(last)}

_handle__ClassDef = _handle_decorated

Expand Down Expand Up @@ -984,7 +984,7 @@ def _handle__Try(self, node):
# If there are `except` clauses, then raises in the try body
# will already jump to them. Start this set over for raises in
# `except` and `else`.
try_block.raise_from = set([])
try_block.raise_from = set()
else:
self.block_stack.pop()

Expand Down Expand Up @@ -1079,7 +1079,7 @@ def _combine_finally_starts(self, starts, exits):
if start.cause is not None:
causes.append(start.cause.format(lineno=start.lineno))
cause = " or ".join(causes)
exits = set(ArcStart(xit.lineno, cause) for xit in exits)
exits = {ArcStart(xit.lineno, cause) for xit in exits}
return exits

@contract(returns='ArcStarts')
Expand Down
2 changes: 1 addition & 1 deletion coverage/phystokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def source_token_lines(source):
"""

ws_tokens = set([token.INDENT, token.DEDENT, token.NEWLINE, tokenize.NL])
ws_tokens = {token.INDENT, token.DEDENT, token.NEWLINE, tokenize.NL}
line = []
col = 0

Expand Down
4 changes: 2 additions & 2 deletions coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ def measured_contexts(self):
"""
self._start_using()
with self._connect() as con:
contexts = set(row[0] for row in con.execute("select distinct(context) from context"))
contexts = {row[0] for row in con.execute("select distinct(context) from context")}
return contexts

def file_tracer(self, filename):
Expand Down Expand Up @@ -857,7 +857,7 @@ def lines(self, filename):
arcs = self.arcs(filename)
if arcs is not None:
all_lines = itertools.chain.from_iterable(arcs)
return list(set(l for l in all_lines if l > 0))
return list({l for l in all_lines if l > 0})

with self._connect() as con:
file_id = self._file_id(filename)
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin1.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def line_number_range(self, frame):
class FileReporter(coverage.FileReporter):
"""Dead-simple FileReporter."""
def lines(self):
return set([105, 106, 107, 205, 206, 207])
return {105, 106, 107, 205, 206, 207}


def coverage_init(reg, options): # pylint: disable=unused-argument
Expand Down
2 changes: 1 addition & 1 deletion tests/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def otherfunc(x):
self.start_import_stop(cov, "f2")

# Double-check that our files were checked.
abs_files = set(os.path.abspath(f) for f in should_trace_hook.filenames)
abs_files = {os.path.abspath(f) for f in should_trace_hook.filenames}
self.assertIn(os.path.abspath("f1.py"), abs_files)
self.assertIn(os.path.abspath("f2.py"), abs_files)
12 changes: 6 additions & 6 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ def meth(self):
def func(x=25):
return 26
""")
raw_statements = set([3, 4, 5, 6, 8, 9, 10, 13, 15, 16, 17, 20, 22, 23, 25, 26])
raw_statements = {3, 4, 5, 6, 8, 9, 10, 13, 15, 16, 17, 20, 22, 23, 25, 26}
if env.PYBEHAVIOR.trace_decorated_def:
raw_statements.update([11, 19])
self.assertEqual(parser.raw_statements, raw_statements)
self.assertEqual(parser.statements, set([8]))
self.assertEqual(parser.statements, {8})

def test_class_decorator_pragmas(self):
parser = self.parse_source("""\
Expand All @@ -188,8 +188,8 @@ class Bar(object):
def __init__(self):
self.x = 8
""")
self.assertEqual(parser.raw_statements, set([1, 2, 3, 5, 6, 7, 8]))
self.assertEqual(parser.statements, set([1, 2, 3]))
self.assertEqual(parser.raw_statements, {1, 2, 3, 5, 6, 7, 8})
self.assertEqual(parser.statements, {1, 2, 3})

def test_empty_decorated_function(self):
parser = self.parse_source("""\
Expand Down Expand Up @@ -463,7 +463,7 @@ def test_missing_line_ending(self):
""")

parser = self.parse_file("normal.py")
self.assertEqual(parser.statements, set([1]))
self.assertEqual(parser.statements, {1})

self.make_file("abrupt.py", """\
out, err = subprocess.Popen(
Expand All @@ -476,4 +476,4 @@ def test_missing_line_ending(self):
self.assertEqual(f.read()[-1], ")")

parser = self.parse_file("abrupt.py")
self.assertEqual(parser.statements, set([1]))
self.assertEqual(parser.statements, {1})
6 changes: 3 additions & 3 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,12 @@ def test_plugin2_with_branch(self):
# have 7 lines in it. If render() was called with line number 4,
# then the plugin will claim that lines 4 and 5 were executed.
analysis = cov._analyze("foo_7.html")
self.assertEqual(analysis.statements, set([1, 2, 3, 4, 5, 6, 7]))
self.assertEqual(analysis.statements, {1, 2, 3, 4, 5, 6, 7})
# Plugins don't do branch coverage yet.
self.assertEqual(analysis.has_arcs(), True)
self.assertEqual(analysis.arc_possibilities(), [])

self.assertEqual(analysis.missing, set([1, 2, 3, 6, 7]))
self.assertEqual(analysis.missing, {1, 2, 3, 6, 7})

def test_plugin2_with_text_report(self):
self.make_render_and_caller()
Expand Down Expand Up @@ -553,7 +553,7 @@ def line_number_range(self, frame):
class MyReporter(coverage.FileReporter):
def lines(self):
return set([99, 999, 9999])
return {99, 999, 9999}
def coverage_init(reg, options):
reg.add_file_tracer(Plugin())
Expand Down
6 changes: 3 additions & 3 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_should_fail_under_invalid_value():


@pytest.mark.parametrize("statements, lines, result", [
(set([1,2,3,4,5,10,11,12,13,14]), set([1,2,5,10,11,13,14]), "1-2, 5-11, 13-14"),
({1,2,3,4,5,10,11,12,13,14}, {1,2,5,10,11,13,14}, "1-2, 5-11, 13-14"),
([1,2,3,4,5,10,11,12,13,14,98,99], [1,2,5,10,11,13,14,99], "1-2, 5-11, 13-14, 99"),
([1,2,3,4,98,99,100,101,102,103,104], [1,2,99,102,103,104], "1-2, 99, 102-104"),
([17], [17], "17"),
Expand All @@ -128,8 +128,8 @@ def test_format_lines(statements, lines, result):

@pytest.mark.parametrize("statements, lines, arcs, result", [
(
set([1,2,3,4,5,10,11,12,13,14]),
set([1,2,5,10,11,13,14]),
{1,2,3,4,5,10,11,12,13,14},
{1,2,5,10,11,13,14},
(),
"1-2, 5-11, 13-14"
),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class TestingTest(TestCase):

def test_assert_count_equal(self):
self.assertCountEqual(set(), set())
self.assertCountEqual(set([1,2,3]), set([3,1,2]))
self.assertCountEqual({1,2,3}, {3,1,2})
with self.assertRaises(AssertionError):
self.assertCountEqual(set([1,2,3]), set())
self.assertCountEqual({1,2,3}, set())
with self.assertRaises(AssertionError):
self.assertCountEqual(set([1,2,3]), set([4,5,6]))
self.assertCountEqual({1,2,3}, {4,5,6})


class CoverageTestTest(CoverageTest):
Expand Down

0 comments on commit 7ff93a9

Please sign in to comment.