summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-02-17 23:19:05 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-02-17 23:51:41 +0100
commitea96fc4bce94673671958e15d17bbcd75914b3f9 (patch)
tree2c0872b4701f60f2469753f9793f7ac95a0b2475 /tests
parent14395e6e5d9d75b6fe1f87a44b77cfe71a538083 (diff)
downloadastroid-git-ea96fc4bce94673671958e15d17bbcd75914b3f9.tar.gz
Move from % syntax to format or f-strings
This is possible with python 3.6
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_brain.py26
-rw-r--r--tests/unittest_brain_numpy_core_fromnumeric.py6
-rw-r--r--tests/unittest_brain_numpy_core_function_base.py2
-rw-r--r--tests/unittest_brain_numpy_core_numerictypes.py8
-rw-r--r--tests/unittest_brain_numpy_core_umath.py26
-rw-r--r--tests/unittest_brain_numpy_ndarray.py20
-rw-r--r--tests/unittest_brain_numpy_random_mtrand.py8
-rw-r--r--tests/unittest_inference.py54
-rw-r--r--tests/unittest_manager.py7
-rw-r--r--tests/unittest_protocols.py4
-rw-r--r--tests/unittest_python3.py6
-rw-r--r--tests/unittest_regrtest.py2
-rw-r--r--tests/unittest_scoped_nodes.py2
13 files changed, 68 insertions, 103 deletions
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py
index f3e5b61e..dc3f25d4 100644
--- a/tests/unittest_brain.py
+++ b/tests/unittest_brain.py
@@ -568,10 +568,10 @@ class MultiprocessingBrainTest(unittest.TestCase):
"""
)
ast_queue = next(module["queue"].infer())
- self.assertEqual(ast_queue.qname(), "{}.Queue".format(queue.__name__))
+ self.assertEqual(ast_queue.qname(), f"{queue.__name__}.Queue")
joinable_queue = next(module["joinable_queue"].infer())
- self.assertEqual(joinable_queue.qname(), "{}.Queue".format(queue.__name__))
+ self.assertEqual(joinable_queue.qname(), f"{queue.__name__}.Queue")
event = next(module["event"].infer())
event_name = "threading.Event"
@@ -591,7 +591,7 @@ class MultiprocessingBrainTest(unittest.TestCase):
for attr in ("list", "dict"):
obj = next(module[attr].infer())
- self.assertEqual(obj.qname(), "{}.{}".format(bases.BUILTINS, attr))
+ self.assertEqual(obj.qname(), f"{bases.BUILTINS}.{attr}")
# pypy's implementation of array.__spec__ return None. This causes problems for this inference.
if not hasattr(sys, "pypy_version_info"):
@@ -632,12 +632,10 @@ class ThreadingBrainTest(unittest.TestCase):
def _test_lock_object(self, object_name):
lock_instance = builder.extract_node(
- """
+ f"""
import threading
- threading.{}()
- """.format(
- object_name
- )
+ threading.{object_name}()
+ """
)
inferred = next(lock_instance.infer())
self.assert_is_valid_lock(inferred)
@@ -675,7 +673,7 @@ class EnumBrainTest(unittest.TestCase):
one = enumeration["one"]
self.assertEqual(one.pytype(), ".MyEnum.one")
- property_type = "{}.property".format(bases.BUILTINS)
+ property_type = f"{bases.BUILTINS}.property"
for propname in ("name", "value"):
prop = next(iter(one.getattr(propname)))
self.assertIn(property_type, prop.decoratornames())
@@ -747,7 +745,7 @@ class EnumBrainTest(unittest.TestCase):
one = enumeration["one"]
clazz = one.getattr("__class__")[0]
- int_type = "{}.{}".format(bases.BUILTINS, "int")
+ int_type = f"{bases.BUILTINS}.int"
self.assertTrue(
clazz.is_subtype_of(int_type),
"IntEnum based enums should be a subtype of int",
@@ -972,12 +970,10 @@ class IOBrainTest(unittest.TestCase):
def test_sys_streams(self):
for name in {"stdout", "stderr", "stdin"}:
node = astroid.extract_node(
- """
+ f"""
import sys
- sys.{}
- """.format(
- name
- )
+ sys.{name}
+ """
)
inferred = next(node.infer())
buffer_attr = next(inferred.igetattr("buffer"))
diff --git a/tests/unittest_brain_numpy_core_fromnumeric.py b/tests/unittest_brain_numpy_core_fromnumeric.py
index cfec2ecb..bd12f0f9 100644
--- a/tests/unittest_brain_numpy_core_fromnumeric.py
+++ b/tests/unittest_brain_numpy_core_fromnumeric.py
@@ -46,13 +46,11 @@ class BrainNumpyCoreFromNumericTest(unittest.TestCase):
inferred_values = list(self._inferred_numpy_func_call(*func_))
self.assertTrue(
len(inferred_values) == 1,
- msg="Too much inferred value for {:s}".format(func_[0]),
+ msg=f"Too much inferred value for {func_[0]:s}",
)
self.assertTrue(
inferred_values[-1].pytype() in licit_array_types,
- msg="Illicit type for {:s} ({})".format(
- func_[0], inferred_values[-1].pytype()
- ),
+ msg=f"Illicit type for {func_[0]:s} ({inferred_values[-1].pytype()})",
)
diff --git a/tests/unittest_brain_numpy_core_function_base.py b/tests/unittest_brain_numpy_core_function_base.py
index bb9dc346..28aa4fc2 100644
--- a/tests/unittest_brain_numpy_core_function_base.py
+++ b/tests/unittest_brain_numpy_core_function_base.py
@@ -50,7 +50,7 @@ class BrainNumpyCoreFunctionBaseTest(unittest.TestCase):
inferred_values = list(self._inferred_numpy_func_call(*func_))
self.assertTrue(
len(inferred_values) == 1,
- msg="Too much inferred value for {:s}".format(func_[0]),
+ msg=f"Too much inferred value for {func_[0]:s}",
)
self.assertTrue(
inferred_values[-1].pytype() in licit_array_types,
diff --git a/tests/unittest_brain_numpy_core_numerictypes.py b/tests/unittest_brain_numpy_core_numerictypes.py
index 286d5774..d9ef6f70 100644
--- a/tests/unittest_brain_numpy_core_numerictypes.py
+++ b/tests/unittest_brain_numpy_core_numerictypes.py
@@ -80,11 +80,9 @@ class NumpyBrainCoreNumericTypesTest(unittest.TestCase):
def _inferred_numpy_attribute(self, attrib):
node = builder.extract_node(
- """
+ f"""
import numpy.core.numerictypes as tested_module
- missing_type = tested_module.{:s}""".format(
- attrib
- )
+ missing_type = tested_module.{attrib:s}"""
)
return next(node.value.infer())
@@ -334,7 +332,7 @@ class NumpyBrainCoreNumericTypesTest(unittest.TestCase):
inferred_values = list(node.infer())
self.assertTrue(
len(inferred_values) == 1,
- msg="Too much inferred value for {:s}".format("datetime64.astype"),
+ msg="Too much inferred value for datetime64.astype",
)
self.assertTrue(
inferred_values[-1].pytype() in licit_array_types,
diff --git a/tests/unittest_brain_numpy_core_umath.py b/tests/unittest_brain_numpy_core_umath.py
index 3d550c97..07a9fbe5 100644
--- a/tests/unittest_brain_numpy_core_umath.py
+++ b/tests/unittest_brain_numpy_core_umath.py
@@ -108,12 +108,10 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
def _inferred_numpy_attribute(self, func_name):
node = builder.extract_node(
- """
+ f"""
import numpy.core.umath as tested_module
- func = tested_module.{:s}
- func""".format(
- func_name
- )
+ func = tested_module.{func_name:s}
+ func"""
)
return next(node.infer())
@@ -204,13 +202,11 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
def _inferred_numpy_func_call(self, func_name, *func_args):
node = builder.extract_node(
- """
+ f"""
import numpy as np
- func = np.{:s}
+ func = np.{func_name:s}
func()
- """.format(
- func_name
- )
+ """
)
return node.infer()
@@ -260,19 +256,15 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
)
self.assertTrue(
len(inferred_values[0].elts) == 2,
- msg="{} should return a pair of values. That's not the case.".format(
- func_
- ),
+ msg=f"{func_} should return a pair of values. That's not the case.",
)
for array in inferred_values[-1].elts:
effective_infer = [m.pytype() for m in array.inferred()]
self.assertTrue(
".ndarray" in effective_infer,
msg=(
- "Each item in the return of {} "
- "should be inferred as a ndarray and not as {}".format(
- func_, effective_infer
- )
+ f"Each item in the return of {func_} should be inferred"
+ f" as a ndarray and not as {effective_infer}"
),
)
diff --git a/tests/unittest_brain_numpy_ndarray.py b/tests/unittest_brain_numpy_ndarray.py
index 3e1365eb..3502a352 100644
--- a/tests/unittest_brain_numpy_ndarray.py
+++ b/tests/unittest_brain_numpy_ndarray.py
@@ -107,25 +107,21 @@ class NumpyBrainNdarrayTest(unittest.TestCase):
def _inferred_ndarray_method_call(self, func_name):
node = builder.extract_node(
- """
+ f"""
import numpy as np
test_array = np.ndarray((2, 2))
- test_array.{:s}()
- """.format(
- func_name
- )
+ test_array.{func_name:s}()
+ """
)
return node.infer()
def _inferred_ndarray_attribute(self, attr_name):
node = builder.extract_node(
- """
+ f"""
import numpy as np
test_array = np.ndarray((2, 2))
- test_array.{:s}
- """.format(
- attr_name
- )
+ test_array.{attr_name:s}
+ """
)
return node.infer()
@@ -139,7 +135,7 @@ class NumpyBrainNdarrayTest(unittest.TestCase):
inferred_values = list(self._inferred_ndarray_method_call(func_))
self.assertTrue(
len(inferred_values) == 1,
- msg="Too much inferred value for {:s}".format(func_),
+ msg=f"Too much inferred value for {func_:s}",
)
self.assertTrue(
inferred_values[-1].pytype() in licit_array_types,
@@ -158,7 +154,7 @@ class NumpyBrainNdarrayTest(unittest.TestCase):
inferred_values = list(self._inferred_ndarray_attribute(attr_))
self.assertTrue(
len(inferred_values) == 1,
- msg="Too much inferred value for {:s}".format(attr_),
+ msg=f"Too much inferred value for {attr_:s}",
)
self.assertTrue(
inferred_values[-1].pytype() in licit_array_types,
diff --git a/tests/unittest_brain_numpy_random_mtrand.py b/tests/unittest_brain_numpy_random_mtrand.py
index 7d1a0ae3..55211fd2 100644
--- a/tests/unittest_brain_numpy_random_mtrand.py
+++ b/tests/unittest_brain_numpy_random_mtrand.py
@@ -77,12 +77,10 @@ class NumpyBrainRandomMtrandTest(unittest.TestCase):
def _inferred_numpy_attribute(self, func_name):
node = builder.extract_node(
- """
+ f"""
import numpy.random.mtrand as tested_module
- func = tested_module.{:s}
- func""".format(
- func_name
- )
+ func = tested_module.{func_name:s}
+ func"""
)
return next(node.infer())
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index bf97b3e3..9dedba65 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -1733,7 +1733,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
for node in ast[8:]:
inferred = next(node.infer())
self.assertIsInstance(inferred, Instance)
- self.assertEqual(inferred.qname(), "{}.tuple".format(BUILTINS))
+ self.assertEqual(inferred.qname(), f"{BUILTINS}.tuple")
def test_starred_in_tuple_literal(self):
code = """
@@ -1886,7 +1886,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
for node in ast[7:]:
inferred = next(node.infer())
self.assertIsInstance(inferred, Instance)
- self.assertEqual(inferred.qname(), "{}.frozenset".format(BUILTINS))
+ self.assertEqual(inferred.qname(), f"{BUILTINS}.frozenset")
def test_set_builtin_inference(self):
code = """
@@ -1917,7 +1917,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
for node in ast[7:]:
inferred = next(node.infer())
self.assertIsInstance(inferred, Instance)
- self.assertEqual(inferred.qname(), "{}.set".format(BUILTINS))
+ self.assertEqual(inferred.qname(), f"{BUILTINS}.set")
def test_list_builtin_inference(self):
code = """
@@ -1947,7 +1947,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
for node in ast[7:]:
inferred = next(node.infer())
self.assertIsInstance(inferred, Instance)
- self.assertEqual(inferred.qname(), "{}.list".format(BUILTINS))
+ self.assertEqual(inferred.qname(), f"{BUILTINS}.list")
def test_conversion_of_dict_methods(self):
ast_nodes = extract_node(
@@ -2018,7 +2018,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
for node in ast[10:]:
inferred = next(node.infer())
self.assertIsInstance(inferred, Instance)
- self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS))
+ self.assertEqual(inferred.qname(), f"{BUILTINS}.dict")
def test_dict_inference_kwargs(self):
ast_node = extract_node("""dict(a=1, b=2, **{'c': 3})""")
@@ -2058,7 +2058,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
ast_node = extract_node(invalid)
inferred = next(ast_node.infer())
self.assertIsInstance(inferred, Instance)
- self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS))
+ self.assertEqual(inferred.qname(), f"{BUILTINS}.dict")
def test_str_methods(self):
code = """
@@ -2665,12 +2665,12 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
def test_bool_value_instances(self):
instances = extract_node(
- """
+ f"""
class FalseBoolInstance(object):
- def {bool}(self):
+ def {BOOL_SPECIAL_METHOD}(self):
return False
class TrueBoolInstance(object):
- def {bool}(self):
+ def {BOOL_SPECIAL_METHOD}(self):
return True
class FalseLenInstance(object):
def __len__(self):
@@ -2694,9 +2694,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
TrueLenInstance() #@
AlwaysTrueInstance() #@
ErrorInstance() #@
- """.format(
- bool=BOOL_SPECIAL_METHOD
- )
+ """
)
expected = (False, True, False, True, True, util.Uninferable, util.Uninferable)
for node, expected_value in zip(instances, expected):
@@ -2705,17 +2703,15 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
def test_bool_value_variable(self):
instance = extract_node(
- """
+ f"""
class VariableBoolInstance(object):
def __init__(self, value):
self.value = value
- def {bool}(self):
+ def {BOOL_SPECIAL_METHOD}(self):
return self.value
not VariableBoolInstance(True)
- """.format(
- bool=BOOL_SPECIAL_METHOD
- )
+ """
)
inferred = next(instance.infer())
self.assertIs(inferred.bool_value(), util.Uninferable)
@@ -4337,20 +4333,20 @@ class TestBool(unittest.TestCase):
def test_bool_bool_special_method(self):
ast_nodes = extract_node(
- """
+ f"""
class FalseClass:
- def {method}(self):
+ def {BOOL_SPECIAL_METHOD}(self):
return False
class TrueClass:
- def {method}(self):
+ def {BOOL_SPECIAL_METHOD}(self):
return True
class C(object):
def __call__(self):
return False
class B(object):
- {method} = C()
+ {BOOL_SPECIAL_METHOD} = C()
class LambdaBoolFalse(object):
- {method} = lambda self: self.foo
+ {BOOL_SPECIAL_METHOD} = lambda self: self.foo
@property
def foo(self): return 0
class FalseBoolLen(object):
@@ -4364,9 +4360,7 @@ class TestBool(unittest.TestCase):
bool(B()) #@
bool(LambdaBoolFalse()) #@
bool(FalseBoolLen()) #@
- """.format(
- method=BOOL_SPECIAL_METHOD
- )
+ """
)
expected = [True, True, False, True, False, False, False]
for node, expected_value in zip(ast_nodes, expected):
@@ -4375,16 +4369,14 @@ class TestBool(unittest.TestCase):
def test_bool_instance_not_callable(self):
ast_nodes = extract_node(
- """
+ f"""
class BoolInvalid(object):
- {method} = 42
+ {BOOL_SPECIAL_METHOD} = 42
class LenInvalid(object):
__len__ = "a"
bool(BoolInvalid()) #@
bool(LenInvalid()) #@
- """.format(
- method=BOOL_SPECIAL_METHOD
- )
+ """
)
for node in ast_nodes:
inferred = next(node.infer())
@@ -4662,7 +4654,7 @@ class SliceTest(unittest.TestCase):
("[1, 2, 3][slice(0, 3, 2)]", [1, 3]),
]
for node, expected_value in ast_nodes:
- ast_node = extract_node("__({})".format(node))
+ ast_node = extract_node(f"__({node})")
inferred = next(ast_node.infer())
self.assertIsInstance(inferred, nodes.List)
self.assertEqual([elt.value for elt in inferred.elts], expected_value)
diff --git a/tests/unittest_manager.py b/tests/unittest_manager.py
index 740a9a1f..ae0941bf 100644
--- a/tests/unittest_manager.py
+++ b/tests/unittest_manager.py
@@ -109,10 +109,7 @@ class AstroidManagerTest(
def _test_ast_from_old_namespace_package_protocol(self, root):
origpath = sys.path[:]
- paths = [
- resources.find("data/path_{}_{}".format(root, index))
- for index in range(1, 4)
- ]
+ paths = [resources.find(f"data/path_{root}_{index}") for index in range(1, 4)]
sys.path.extend(paths)
try:
for name in ("foo", "bar", "baz"):
@@ -195,7 +192,7 @@ class AstroidManagerTest(
self.assertEqual(module.name, "mypypa")
end = os.path.join(archive, "mypypa")
self.assertTrue(
- module.file.endswith(end), "%s doesn't endswith %s" % (module.file, end)
+ module.file.endswith(end), f"{module.file} doesn't endswith {end}"
)
finally:
# remove the module, else after importing egg, we don't get the zip
diff --git a/tests/unittest_protocols.py b/tests/unittest_protocols.py
index 60d9b33c..6321a159 100644
--- a/tests/unittest_protocols.py
+++ b/tests/unittest_protocols.py
@@ -81,7 +81,7 @@ class ProtocolTests(unittest.TestCase):
assert assigned.as_string() == "[1, 2]"
def _get_starred_stmts(self, code):
- assign_stmt = extract_node("{} #@".format(code))
+ assign_stmt = extract_node(f"{code} #@")
starred = next(assign_stmt.nodes_of_class(Starred))
return next(starred.assigned_stmts())
@@ -96,7 +96,7 @@ class ProtocolTests(unittest.TestCase):
self.assertEqual(expected, stmts)
def _helper_starred_inference_error(self, code):
- assign_stmt = extract_node("{} #@".format(code))
+ assign_stmt = extract_node(f"{code} #@")
starred = next(assign_stmt.nodes_of_class(Starred))
self.assertRaises(InferenceError, list, starred.assigned_stmts())
diff --git a/tests/unittest_python3.py b/tests/unittest_python3.py
index 4bcf4819..1cbacfd9 100644
--- a/tests/unittest_python3.py
+++ b/tests/unittest_python3.py
@@ -376,11 +376,9 @@ class Python3TC(unittest.TestCase):
]
for func_body in func_bodies:
code = dedent(
- """
+ f"""
async def f():
- {}""".format(
- func_body
- )
+ {func_body}"""
)
func = extract_node(code)
self.assertEqual(func.as_string().strip(), code.strip())
diff --git a/tests/unittest_regrtest.py b/tests/unittest_regrtest.py
index eeedd353..cec1c602 100644
--- a/tests/unittest_regrtest.py
+++ b/tests/unittest_regrtest.py
@@ -210,7 +210,7 @@ def test():
)
ancestors = list(node.ancestors())
self.assertEqual(len(ancestors), 1)
- self.assertEqual(ancestors[0].qname(), "{}.object".format(BUILTINS))
+ self.assertEqual(ancestors[0].qname(), f"{BUILTINS}.object")
def test_ancestors_missing_from_function(self):
# Test for https://www.logilab.org/ticket/122793
diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py
index 10229349..1ea68a1c 100644
--- a/tests/unittest_scoped_nodes.py
+++ b/tests/unittest_scoped_nodes.py
@@ -222,7 +222,7 @@ class ModuleNodeTest(ModuleLoader, unittest.TestCase):
expected = (
"Relative import with too many levels "
- "({level}) for module {name!r}".format(level=level - 1, name=mod.name)
+ f"({level-1}) for module {mod.name!r}"
)
self.assertEqual(expected, str(cm.exception))