def test_Not_enough_values_to_unpack(): d = (1, ) try: a, b, *c = d except ValueError: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert ( "ValueError: not enough values to unpack (expected at least 2, got 1)" in result) if friendly.get_lang() == "en": assert "a `tuple` of length 1" in result try: for x, y, z in enumerate(range(3)): pass except ValueError: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ValueError: not enough values to unpack (expected 3, got 2)" in result d = "ab" try: a, b, c = d except ValueError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ValueError: not enough values to unpack (expected 3, got 2)" in result if friendly.get_lang() == "en": assert "a string (`str`) of length 2" in result return result, message
def test_Similar_names(): first = {"alpha": 1, "beta": 2, "gamma": 3} try: a = first["alpha1"] except KeyError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "KeyError: 'alpha1'" in result if friendly.get_lang() == "en": expected = "Did you mean `'alpha'`?" ok, diff = expected_in_result(expected, result) assert ok, diff second = {"alpha0": 1, "alpha11": 2, "alpha12": 3} try: a = second["alpha"] except KeyError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "KeyError: 'alpha'" in result if friendly.get_lang() == "en": expected = "Did you mean `'alpha0'`?" ok, diff = expected_in_result(expected, result) assert ok, diff expected = "'alpha0', 'alpha12', 'alpha11'" ok, diff = expected_in_result(expected, result) assert ok, diff return result, message
def test_Typo_in_local(): def test1(): alpha1 = 1 alpha2 += 1 try: test1() except UnboundLocalError: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "local variable 'alpha2' referenced before assignment" in result if friendly.get_lang() == "en": assert "similar name `alpha1` was found" in result def test2(): alpha1 = 1 alpha2 = 1 alpha3 += 1 try: test2() except UnboundLocalError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "local variable 'alpha3' referenced before assignment" in result if friendly.get_lang() == "en": assert "perhaps you meant one of the following" in result return result, message
def test_Too_many_positional_argument(): def fn(*, b=1): pass try: fn(1) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError" in result assert "fn() takes 0 positional arguments but 1 was given" in result if friendly.get_lang() == "en": assert "1 positional argument(s) while it requires 0" in result class A: def f(x): pass try: A().f(1) except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError" in result assert "f() takes 1 positional argument but 2 were given" in result if friendly.get_lang() == "en": assert "2 positional argument(s) while it requires 1" in result # assert "Perhaps you forgot `self`" in result return result, message
def test_Module_attribute_typo(): import string try: string.ascii_lowecase except AttributeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "AttributeError: module 'string' has no attribute 'ascii_lowecase'" in result if friendly.get_lang() == "en": assert "Did you mean `ascii_lowercase`" in result import math try: math.cost except AttributeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "AttributeError: module 'math' has no attribute 'cost'" in result if friendly.get_lang() == "en": assert ( "Instead of writing `math.cost`, perhaps you meant to write one of" in result) assert "cos, cosh" in result assert not "acosh" in result return result, message
def test_Float_modulo(): zero = 0. a = 10**1000 # for coverage: tests format_var_info for long variables try: a % 1 % zero except ZeroDivisionError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ZeroDivisionError: float modulo" in result if friendly.get_lang() == "en": assert "The following mathematical expression includes a division by zero" in result assert "done using the modulo operator" in result try: 1 % zero except ZeroDivisionError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ZeroDivisionError: float modulo" in result if friendly.get_lang() == "en": assert "Using the modulo operator" in result return result, message
def test_Not_a_package(): try: import os.xxx except ModuleNotFoundError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ModuleNotFoundError: No module named 'os.xxx'" in result if friendly.get_lang() == "en": assert "`xxx` cannot be imported" in result try: import os.open except ModuleNotFoundError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ModuleNotFoundError: No module named 'os.open'" in result if friendly.get_lang() == "en": assert "`from os import open`" in result try: import os.pathh except ModuleNotFoundError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ModuleNotFoundError: No module named 'os.pathh'" in result if friendly.get_lang() == "en": assert "Did you mean `import os.path`" in result return result, message
def test_Indices_must_be_integers_or_slices(): a = [1, 2, 3] try: a[1, 2] except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: list indices must be integers or slices" in result if friendly.get_lang() == "en": assert "Did you mean `a[1:2]`" in result a = (1, 2, 3) try: a[2.0] except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: tuple indices must be integers or slices" in result if friendly.get_lang() == "en": assert "Perhaps you forgot to convert `2.0` into an integer." in result try: [1, 2, 3]["2"] except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: list indices must be integers or slices" in result if friendly.get_lang() == "en": assert 'Perhaps you forgot to convert `"2"` into an integer.' in result return result, message
def test_Object_is_not_subscriptable(): try: a = 2 [1] except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'int' object is not subscriptable" in result if friendly.get_lang() == "en": assert "from `2`, an object of type `int`" in result def f(): pass try: a = f[1] except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'function' object is not subscriptable" in result if friendly.get_lang() == "en": assert "Did you mean `f(1)`" in result return result, message
def test_Generic(): # Generic - no additional explanation class A: pass try: A.x # testing type except AttributeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "AttributeError: type object 'A' has no attribute 'x'" in result if friendly.get_lang() == "en": assert "The object `A` has no attribute" in result try: a = A() a.x # Testing instance except AttributeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "AttributeError: 'A' object has no attribute 'x'" in result if friendly.get_lang() == "en": assert "The object `a` has no attribute" in result return result, message
def test_Attribute_from_other_module(): import math import keyword try: keyword.pi except AttributeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "module 'keyword' has no attribute 'pi'" in result if friendly.get_lang() == "en": assert "Did you mean `math`?" in result import cmath try: keyword.pi except AttributeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() if friendly.get_lang() == "en": assert "Did you mean one of the following modules:" in result return result, message
def test_Not_callable(): try: _ = (1, 2)(3, 4) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'tuple' object is not callable" in result if friendly.get_lang() == "en": assert "you have a missing comma between the object" in result try: _ = 3 (4 + 4) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'int' object is not callable" in result if friendly.get_lang() == "en": assert "Perhaps you forgot a multiplication operator" in result try: _ = [1, 2](3, 4) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'list' object is not callable" in result if friendly.get_lang() == "en": assert "you have a missing comma between the object" in result # Test with dotted name class A: a_list = [1, 2, 3] try: b = A() b.a_list(3) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'list' object is not callable" in result if friendly.get_lang() == "en": assert "b.a_list[3]" in result try: a, b = 3, 7 _ = [1, 2](a + b) except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'list' object is not callable" in result if friendly.get_lang() == "en": assert "Perhaps you meant to use `[]` instead of `()`" in result return result, message
def test_Cannot_multiply_by_non_int(): try: "b" * "a" except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: can't multiply sequence by non-int of type 'str'" in result if friendly.get_lang() == "en": assert "You can only multiply sequences, such as" in result try: "3" * "a" except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: can't multiply sequence by non-int of type 'str'" in result if friendly.get_lang() == "en": assert 'Did you forget to convert `"3"` into an integer?' in result a = b = c = "2" try: d = a * b * c except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: can't multiply sequence by non-int of type 'str'" in result if friendly.get_lang() == "en": assert "Did you forget to convert `a` and `b` into integers?" in result a = "abc" try: a *= c except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: can't multiply sequence by non-int of type 'str'" in result if friendly.get_lang() == "en": assert "Did you forget to convert `c` into an integer?" in result try: "a" * "2" except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: can't multiply sequence by non-int of type 'str'" in result if friendly.get_lang() == "en": assert 'Did you forget to convert `"2"` into an integer?' in result return result, message
def test_flush(): try: b = c except Exception: friendly.explain_traceback(redirect="capture") result = friendly.get_output(flush=False) assert "NameError: name 'c' is not defined" in result result1 = friendly.get_output() # flushes assert "NameError: name 'c' is not defined" in result1 result2 = friendly.get_output() # returns empty string assert not result2 return result, result2
def test_history(): while not empty_history(): helpers.back() try: a except NameError: friendly.explain_traceback(redirect="capture") friendly.get_output() helpers.history() assert "NameError" in friendly.get_output() helpers.back() helpers.history() assert empty_history()
def test_Bad_type_for_unary_operator(): try: a = + "abc" print(a) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: bad operand type for unary +: 'str'" in result assert not "+=" in result if friendly.get_lang() == "en": assert "You tried to use the unary operator '+'" in result try: a = - [1, 2, 3] print(a) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: bad operand type for unary -: 'list'" in result if friendly.get_lang() == "en": assert "You tried to use the unary operator '-'" in result try: a = ~(1, 2, 3) print(a) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: bad operand type for unary ~: 'tuple'" in result if friendly.get_lang() == "en": assert "You tried to use the unary operator '~'" in result try: # fmt: off a = "abc" a = + "def" # fmt: on print(a) except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: bad operand type for unary +: 'str'" in result if friendly.get_lang() == "en": assert "Perhaps you meant to write `+=`" in result assert "You tried to use the unary operator '+'" in result return result, message
def test_python_tb(): while not empty_history(): helpers.back() try: math.Pi except AttributeError: friendly.explain_traceback(redirect="capture") friendly.get_output() helpers.python_tb() result = friendly.get_output() assert "Did you mean `pi`" not in result assert "AttributeError" in result assert "File" in result helpers.back() assert empty_history()
def test_hint(): while not empty_history(): helpers.back() try: math.Pi except AttributeError: friendly.explain_traceback(redirect="capture") friendly.get_output() helpers.hint() result = friendly.get_output() assert _hint in result assert _message not in result assert "File" not in result helpers.back() assert empty_history()
def test_no_why(): while not empty_history(): helpers.back() try: raise ArithmeticError except ArithmeticError: friendly.explain_traceback(redirect="capture") friendly.get_output() helpers.why() result = friendly.get_output() assert "I have no suggestion to offer." in result helpers.hint() new_result = friendly.get_output() assert "I have no suggestion to offer." in new_result helpers.back() assert empty_history()
def test_back(): while not empty_history(): helpers.back() nothing_back = "Nothing to go back to: no exception recorded." helpers.back() assert nothing_back in friendly.get_output() try: a except NameError: friendly.explain_traceback(redirect="capture") friendly.get_output() helpers.back() assert nothing_back not in friendly.get_output() helpers.back() assert nothing_back in friendly.get_output() assert empty_history()
def test_syntax_errors(filename): cause = descriptions[filename]["in cause"] try: exec("from . import %s" % filename) except Exception: friendly.explain_traceback(redirect="capture") result = friendly.get_output() if "tab_error" in filename: assert "TabError" in result, "TabError identified incorrectly; %s" % filename elif "indentation" in filename or "indented" in cause: assert "IndentationError" in result, ( "IndentationError identified incorrectly; %s" % filename) else: assert "SyntaxError" in result, ( "SyntaxError identified incorrectly; %s" % filename) unwrapped_result = " ".join(result.split()) assert cause in unwrapped_result, "\nExpected to see: %s\nIn: %s" % ( cause, result) if "also in cause" in descriptions[filename]: other_causes = descriptions[filename]["also in cause"] for cause in other_causes: assert cause in unwrapped_result, "\nExpected to see: %s\nIn: %s" % ( cause, result, ) if "not in cause" in descriptions[filename]: not_in_cause = descriptions[filename]["not in cause"] for cause in not_in_cause: assert cause not in unwrapped_result, "\nDid not expect to see: %s\nIn: %s" % ( cause, result, )
def test_why_no_hint(): while not empty_history(): helpers.back() try: math.PiPiPi except AttributeError: friendly.explain_traceback(redirect="capture") friendly.get_output() helpers.why() result = friendly.get_output() assert "Python tells us" in result helpers.hint() result = friendly.get_output() assert "I have no suggestion to offer; try `why()`." in result helpers.back() assert empty_history()
def multiple_import_on_same_line(): try: import circular_a, circular_b except ImportError: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "cannot import name 'a'" in result if friendly.get_lang() == "en": assert "likely caused by what is known as a 'circular import'." in result
def multiple_choices(): try: from math import bsin except ImportError: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ImportError: cannot import name 'bsin'" in result if friendly.get_lang() == "en": assert "Did you mean one of the following: `sin" in result
def no_suggestion(): try: from math import alphabet_alphabet except ImportError: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ImportError: cannot import name 'alphabet_alphabet'" in result if friendly.get_lang() == "en": assert "could not be imported is `alphabet_alphabet`" in result
def test_run_error_en(): friendly.run( "../name_error.py", include="explain", # comprehensive console=False, redirect="capture", ) result = friendly.get_output() friendly.uninstall() assert "The similar name `pi` was found in the local scope." in result
def test_Can_only_concatenate(): try: a = "2" one = 1 result = a + one except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() py37 = "TypeError: can only concatenate" in result py36 = "must be str, not int" in result assert py37 or py36 if friendly.get_lang() == "en": assert "a string (`str`) and an integer (`int`)" in result assert "Perhaps you forgot to convert the string" in result try: a = "a" a_list = [1, 2, 3] result = a + a_list except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() py37 = "TypeError: can only concatenate" in result py36 = "must be str, not list" in result assert py37 or py36 if friendly.get_lang() == "en": assert "a string (`str`) and a `list`" in result try: a_tuple = (1, 2, 3) a_list = [1, 2, 3] result = a_tuple + a_list except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: can only concatenate" in result if friendly.get_lang() == "en": assert "a `tuple` and a `list`" in result return result, message
def test_Not_an_integer(): try: range([1, 2]) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'list' object cannot be interpreted as an integer" in result if friendly.get_lang() == "en": assert "Perhaps you forgot to convert " not in result try: range("2") except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'str' object cannot be interpreted as an integer" in result if friendly.get_lang() == "en": assert 'Perhaps you forgot to convert `"2"` into an integer.' in result try: range(1.0) except TypeError as e: friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'float' object cannot be interpreted as an integer" in result if friendly.get_lang() == "en": assert "Perhaps you forgot to convert `1.0" in result c, d = "2", "3" try: range(c, d) except TypeError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "TypeError: 'str' object cannot be interpreted as an integer" in result if friendly.get_lang() == "en": assert "Perhaps you forgot to convert `c, d` into integers." in result return result, message
def test_Mixed_operations(): try: a = divmod(8, 1 // 2) except ZeroDivisionError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "ZeroDivisionError: integer division or modulo by zero" in result if friendly.get_lang() == "en": assert "The following mathematical expression includes a division by zero" in result return result, message
def test_no_curses(): try: import curses except ModuleNotFoundError as e: message = str(e) friendly.explain_traceback(redirect="capture") result = friendly.get_output() assert "No module named '_curses'" in result if friendly.get_lang() == "en": assert "The curses module is rarely installed with Python on Windows." in result return result, message