Skip to content

Commit

Permalink
Migrate all uses of assertErrorLogIs to assertErrorRegexes.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 303864672
  • Loading branch information
rchen152 committed Apr 1, 2020
1 parent 0210cd0 commit 024e0c9
Show file tree
Hide file tree
Showing 88 changed files with 1,650 additions and 2,146 deletions.
13 changes: 6 additions & 7 deletions pytype/tests/py2/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ class AbstractMethodTests(test_base.TargetPython27FeatureTest):
"""Tests for @abc.abstractmethod."""

def test_name_error(self):
_, errors = self.InferWithErrors("""\
self.InferWithErrors("""\
import abc
class Example(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def foo(self):
name_error
name_error # name-error
""")
self.assertErrorLogIs(errors, [(6, "name-error", r"name_error")])

def test_instantiate_abstract_class(self):
_, errors = self.InferWithErrors("""\
Expand All @@ -25,9 +24,9 @@ class Example(object):
@abc.abstractmethod
def foo(self):
pass
Example() # line 7
Example() # not-instantiable[e]
""")
self.assertErrorLogIs(errors, [(7, "not-instantiable", r"Example.*foo")])
self.assertErrorRegexes(errors, {"e": r"Example.*foo"})

def test_multiple_inheritance_implementation(self):
self.Check("""
Expand Down Expand Up @@ -59,9 +58,9 @@ def foo(self):
pass
class Foo(X, Interface):
pass
Foo().foo() # line 11
Foo().foo() # not-instantiable[e]
""")
self.assertErrorLogIs(errors, [(11, "not-instantiable", r"Foo.*foo")])
self.assertErrorRegexes(errors, {"e": r"Foo.*foo"})

def test_multiple_inheritance_builtins(self):
self.Check("""
Expand Down
8 changes: 4 additions & 4 deletions pytype/tests/py2/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class A(object):
values = 42
args = {A() if __random__ else True: ""}
for x, y in sorted(args.iteritems()):
x.values # line 5
x.values # attribute-error[e]
""")
self.assertErrorLogIs(errors, [(5, "attribute-error", r"'values' on bool")])
self.assertErrorRegexes(errors, {"e": r"'values' on bool"})

def testTypeParameterInstanceSetAttr(self):
ty = self.Infer("""
Expand Down Expand Up @@ -63,9 +63,9 @@ def testIter(self):
errors = self.CheckWithErrors("""\
def f():
x = None
return [y for y in x]
return [y for y in x] # attribute-error[e]
""")
self.assertErrorLogIs(errors, [(3, "attribute-error", r"__iter__.*None")])
self.assertErrorRegexes(errors, {"e": r"__iter__.*None"})

@test_base.skip("Needs vm._get_iter() to iterate over individual bindings.")
def testMetaclassIter(self):
Expand Down
5 changes: 2 additions & 3 deletions pytype/tests/py2/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,9 @@ def testIterItems(self):

def testIntInit(self):
_, errors = self.InferWithErrors("""\
int(0, 1) # line 8: expected str or unicode, got int for first arg
int(0, 1) # wrong-arg-types[e]
""")
self.assertErrorLogIs(errors, [(1, "wrong-arg-types",
r"Union\[str, unicode\].*int")])
self.assertErrorRegexes(errors, {"e": r"Union\[str, unicode\].*int"})

def testAddStrAndBytearray(self):
ty = self.Infer("""
Expand Down
4 changes: 2 additions & 2 deletions pytype/tests/py2/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def decorate(cls):
@decorate
class Foo(object):
def Hello(self):
return self.Goodbye() # line 6
return self.Goodbye() # attribute-error[e]
""")
self.assertErrorLogIs(errors, [(6, "attribute-error", r"Goodbye")])
self.assertErrorRegexes(errors, {"e": r"Goodbye"})


test_base.main(globals(), __name__ == "__main__")
22 changes: 11 additions & 11 deletions pytype/tests/py2/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ class ErrorTest(test_base.TargetPython27FeatureTest):
def testProtocolMismatch(self):
_, errors = self.InferWithErrors("""\
class Foo(object): pass
next(Foo())
next(Foo()) # wrong-arg-types[e]
""")
self.assertErrorLogIs(errors, [
(2, "wrong-arg-types", "__iter__, next")
])
self.assertErrorRegexes(errors, {"e": r"__iter__, next"})

def testProtocolMismatchPartial(self):
_, errors = self.InferWithErrors("""\
class Foo(object):
def __iter__(self):
return self
next(Foo())
next(Foo()) # wrong-arg-types[e]
""")
self.assertErrorLogIs(errors, [(
4, "wrong-arg-types", r"\n\s*next\s*$")]) # `next` on its own line
self.assertErrorRegexes(
errors, {"e": r"\n\s*next\s*$"}) # `next` on its own line

def testGetSlice(self):
errors = self.CheckWithErrors("def f(): v = []; return v[:'foo']")
self.assertErrorLogIs(errors, [
(1, "unsupported-operands",
r"slicing.*List.*str.*__getslice__ on List.*Optional\[int\]")])
errors = self.CheckWithErrors("""\
def f(): v = []; return v[:'foo'] # unsupported-operands[e]
""")
self.assertErrorRegexes(
errors,
{"e": r"slicing.*List.*str.*__getslice__ on List.*Optional\[int\]"})


test_base.main(globals(), __name__ == "__main__")
9 changes: 4 additions & 5 deletions pytype/tests/py2/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def test_getslice_slot(self):
g = a[2:None]
h = a[None:2]
i = a[None:None]
j = a[int:str]
k = a["s":]
j = a[int:str] # unsupported-operands[e1]
k = a["s":] # unsupported-operands[e2]
l = a[1:-1]
m = a[0:0]
n = a[1:1]
Expand All @@ -41,9 +41,8 @@ def test_getslice_slot(self):
m = ... # type: List[nothing]
n = ... # type: List[nothing]
""")
self.assertErrorLogIs(errors, [
(10, "unsupported-operands", r"__getslice__ on List"),
(11, "unsupported-operands", r"__getslice__ on List")])
self.assertErrorRegexes(errors, {
"e1": r"__getslice__ on List", "e2": r"__getslice__ on List"})


test_base.main(globals(), __name__ == "__main__")
16 changes: 6 additions & 10 deletions pytype/tests/py2/test_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ def test_calls(self):
""")

def test_bad_call(self):
_, errorlog = self.InferWithErrors("""\
self.InferWithErrors("""\
import collections
collections.namedtuple()
collections.namedtuple("_")
collections.namedtuple("_", "", True, True, True)
collections.namedtuple("_", "", True, verbose=True)
collections.namedtuple() # missing-parameter
collections.namedtuple("_") # missing-parameter
collections.namedtuple("_", "", True, True, True) # wrong-arg-count
collections.namedtuple(
"_", "", True, verbose=True) # duplicate-keyword-argument
""")
self.assertErrorLogIs(errorlog,
[(2, "missing-parameter"),
(3, "missing-parameter"),
(4, "wrong-arg-count"),
(5, "duplicate-keyword-argument")])


test_base.main(globals(), __name__ == "__main__")
17 changes: 9 additions & 8 deletions pytype/tests/py2/test_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def get_varargs(x: int, *args: T, z: int, **kws: int) -> T: ...
v1 = a.get_varargs(1, *l1)
v2 = a.get_varargs(1, *l2, z=5)
v3 = a.get_varargs(1, True, 2.0, z=5)
v4 = a.get_varargs(1, 2j, "foo", z=5) # bad: conflicting args types
v5 = a.get_varargs(1, *None) # bad: None not iterable
# bad: conflicting args types
v4 = a.get_varargs(1, 2j, "foo", z=5) # wrong-arg-types[e1]
# bad: None not iterable
v5 = a.get_varargs(1, *None) # wrong-arg-types[e2]
""", deep=True, pythonpath=[d.path])
self.assertTypesMatchPytd(ty, """
from typing import Any
Expand All @@ -53,8 +55,7 @@ def get_varargs(x: int, *args: T, z: int, **kws: int) -> T: ...
r"Actually passed: \(x, _, _2: str, \.\.\.\)")
msg2 = (r"Expected: \(x, \*args: Iterable, \.\.\.\).*"
r"Actually passed: \(x, args: None\)")
self.assertErrorLogIs(errors, [(8, "wrong-arg-types", msg1),
(9, "wrong-arg-types", msg2)])
self.assertErrorRegexes(errors, {"e1": msg1, "e2": msg2})

# TODO(sivachandra): Make this a target independent test after
# after b/78785264 is fixed.
Expand All @@ -69,11 +70,12 @@ def get_kwargs(x: int, *args: int, z: int, **kws: T) -> T: ...
import a
d1 = None # type: dict[int, int]
d2 = None # type: Mapping[str, Union[str, complex]]
v1 = a.get_kwargs(1, 2, 3, z=5, **d1) # bad: K must be str
# bad: K must be str
v1 = a.get_kwargs(1, 2, 3, z=5, **d1) # wrong-arg-types[e1]
v2 = a.get_kwargs(1, 2, 3, z=5, **d2)
v3 = a.get_kwargs(1, 2, 3, z=5, v=0, u=3j)
# bad: conflicting kwargs types
v4 = a.get_kwargs(1, 2, 3, z=5, v="", u=3j)
v4 = a.get_kwargs(1, 2, 3, z=5, v="", u=3j) # wrong-arg-types[e2]
""", deep=True, pythonpath=[d.path])
self.assertTypesMatchPytd(ty, """
from typing import Any, Mapping
Expand All @@ -89,8 +91,7 @@ def get_kwargs(x: int, *args: int, z: int, **kws: T) -> T: ...
r"Actually passed: \(x, _, _, z, kws: Dict\[int, int\]\)")
msg2 = (r"Expected: \(x, _, _, u, v: complex, \.\.\.\).*"
r"Actually passed: \(x, _, _, u, v: str, \.\.\.\)")
self.assertErrorLogIs(errors, [(5, "wrong-arg-types", msg1),
(9, "wrong-arg-types", msg2)])
self.assertErrorRegexes(errors, {"e1": msg1, "e2": msg2})


test_base.main(globals(), __name__ == "__main__")
4 changes: 2 additions & 2 deletions pytype/tests/py2/test_reingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def foo(self):
d.create_file("foo.pyi", pytd_utils.Print(foo))
_, errors = self.InferWithErrors("""\
import foo
foo.Foo()
foo.Foo() # not-instantiable[e]
foo.Bar()
""", pythonpath=[d.path])
self.assertErrorLogIs(errors, [(2, "not-instantiable", r"foo\.Foo.*foo")])
self.assertErrorRegexes(errors, {"e": r"foo\.Foo.*foo"})


test_base.main(globals(), __name__ == "__main__")
11 changes: 5 additions & 6 deletions pytype/tests/py2/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ class SlotsTest(test_base.TargetPython27FeatureTest):
"""Tests for __slots__."""

def testBuiltinAttr(self):
_, errors = self.InferWithErrors("""\
buffer("foo").bar = 16
self.InferWithErrors("""\
buffer("foo").bar = 16 # not-writable
""")
self.assertErrorLogIs(errors, [(1, "not-writable")])

def testSlotWithBytes(self):
_ = self.Check("""\
self.Check("""\
class Foo(object):
__slots__ = (b"x",)
""")

def testSlotWithUnicode(self):
errors = self.CheckWithErrors("""\
class Foo(object):
class Foo(object): # bad-slots[e]
__slots__ = (u"fo\\xf6", u"b\\xe4r", "baz")
Foo().baz = 3
""")
self.assertErrorLogIs(errors, [(1, "bad-slots", r"fo\\xc3\\xb6")])
self.assertErrorRegexes(errors, {"e": r"fo\\xc3\\xb6"})


test_base.main(globals(), __name__ == "__main__")
9 changes: 4 additions & 5 deletions pytype/tests/py2/test_super.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ def testSuperMissingArg(self):
_, errors = self.InferWithErrors("""\
class Foo(object):
def __new__(cls):
return super(cls).__new__(cls)
return super(cls).__new__(cls) # wrong-arg-types[e1]
class Bar(object):
def __new__(cls):
return super().__new__(cls)
return super().__new__(cls) # wrong-arg-count[e2]
""")
self.assertErrorLogIs(errors, [
(3, "wrong-arg-types", r"Type\[super\].*Type\[Foo\]"),
(6, "wrong-arg-count", r"2.*0")])
self.assertErrorRegexes(
errors, {"e1": r"Type\[super\].*Type\[Foo\]", "e2": r"2.*0"})


test_base.main(globals(), __name__ == "__main__")
24 changes: 11 additions & 13 deletions pytype/tests/py2/test_typevar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,31 @@ class Test(test_base.TargetPython27FeatureTest):

def testUseConstraintsFromPyi(self):
with file_utils.Tempdir() as d:
d.create_file("foo.pyi", """\
d.create_file("foo.pyi", """
from typing import AnyStr, TypeVar
T = TypeVar("T", int, float)
def f(x: T) -> T: ...
def g(x: AnyStr) -> AnyStr: ...
""")
_, errors = self.InferWithErrors("""\
import foo
foo.f("")
foo.g(0)
foo.f("") # wrong-arg-types[e1]
foo.g(0) # wrong-arg-types[e2]
""", pythonpath=[d.path])
self.assertErrorLogIs(errors, [
(2, "wrong-arg-types", r"Union\[float, int\].*str"),
(3, "wrong-arg-types", r"Union\[str, unicode\].*int")])
self.assertErrorRegexes(errors, {
"e1": r"Union\[float, int\].*str",
"e2": r"Union\[str, unicode\].*int"})

def testExtraArguments(self):
# TODO(b/78905523): Make this a target-independent test.
_, errors = self.InferWithErrors("""\
from typing import TypeVar
T = TypeVar("T", extra_arg=42)
S = TypeVar("S", *__any_object__)
U = TypeVar("U", **__any_object__)
T = TypeVar("T", extra_arg=42) # invalid-typevar[e1]
S = TypeVar("S", *__any_object__) # invalid-typevar[e2]
U = TypeVar("U", **__any_object__) # invalid-typevar[e3]
""")
self.assertErrorLogIs(errors, [
(2, "invalid-typevar", r"extra_arg"),
(3, "invalid-typevar", r"\*args"),
(4, "invalid-typevar", r"\*\*kwargs")])
self.assertErrorRegexes(errors, {
"e1": r"extra_arg", "e2": r"\*args", "e3": r"\*\*kwargs"})

def testSimplifyArgsAndKwargs(self):
# TODO(b/78905523): Make this a target-independent test.
Expand Down
4 changes: 2 additions & 2 deletions pytype/tests/py3/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Bar(Foo):
@property
def foo(self):
return super(Bar, self).foo
v1 = Foo().foo
v1 = Foo().foo # not-instantiable[e]
v2 = Bar().foo
""")
self.assertTypesMatchPytd(ty, """
Expand All @@ -77,7 +77,7 @@ class Bar(Foo):
class Foo(metaclass=abc.ABCMeta):
foo = ... # type: Any
""")
self.assertErrorLogIs(errors, [(10, "not-instantiable", r"Foo.*foo")])
self.assertErrorRegexes(errors, {"e": r"Foo.*foo"})


test_base.main(globals(), __name__ == "__main__")

0 comments on commit 024e0c9

Please sign in to comment.