| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1672,6 +1672,17 @@ def test_null_byte(self): | |||
| 1672 | 1672 | self.assertEqual(exit_code, 0) | |
| 1673 | 1673 | self.assertNotIn("TypeError", output) | |
| 1674 | 1674 | ||
| 1675 | + @force_not_colorized | ||
| 1676 | + def test_non_string_suggestion_candidates(self): | ||
| 1677 | + commands = ("import runpy\n" | ||
| 1678 | + "runpy._run_module_code('blech', {0: '', 'bluch': ''}, '')\n" | ||
| 1679 | + "exit()\n") | ||
| 1680 | + | ||
| 1681 | + output, exit_code = self.run_repl(commands) | ||
| 1682 | + self.assertEqual(exit_code, 0) | ||
| 1683 | + self.assertNotIn("all elements in 'candidates' must be strings", output) | ||
| 1684 | + self.assertIn("bluch", output) | ||
| 1685 | + | ||
| 1675 | 1686 | def test_readline_history_file(self): | |
| 1676 | 1687 | # skip, if readline module is not available | |
| 1677 | 1688 | readline = import_module('readline') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4188,6 +4188,15 @@ def __dir__(self): | |||
| 4188 | 4188 | self.assertNotIn("blech", actual) | |
| 4189 | 4189 | self.assertNotIn("oh no!", actual) | |
| 4190 | 4190 | ||
| 4191 | + def test_attribute_error_with_non_string_candidates(self): | ||
| 4192 | + class T: | ||
| 4193 | + bluch = 1 | ||
| 4194 | + | ||
| 4195 | + instance = T() | ||
| 4196 | + instance.__dict__[0] = 1 | ||
| 4197 | + actual = self.get_suggestion(instance, 'blich') | ||
| 4198 | + self.assertIn("bluch", actual) | ||
| 4199 | + | ||
| 4191 | 4200 | def test_attribute_error_with_bad_name(self): | |
| 4192 | 4201 | def raise_attribute_error_with_bad_name(): | |
| 4193 | 4202 | raise AttributeError(name=12, obj=23) | |
@@ -4301,6 +4310,13 @@ def test_import_from_suggestions_underscored(self): | |||
| 4301 | 4310 | self.assertIn("'_bluch'", self.get_import_from_suggestion(code, '_luch')) | |
| 4302 | 4311 | self.assertNotIn("'_bluch'", self.get_import_from_suggestion(code, 'bluch')) | |
| 4303 | 4312 | ||
| 4313 | + def test_import_from_suggestions_non_string(self): | ||
| 4314 | + modWithNonStringAttr = textwrap.dedent("""\ | ||
| 4315 | + globals()[0] = 1 | ||
| 4316 | + bluch = 1 | ||
| 4317 | + """) | ||
| 4318 | + self.assertIn("'bluch'", self.get_import_from_suggestion(modWithNonStringAttr, 'blech')) | ||
| 4319 | + | ||
| 4304 | 4320 | def test_import_from_suggestions_do_not_trigger_for_long_attributes(self): | |
| 4305 | 4321 | code = "blech = None" | |
| 4306 | 4322 | ||
@@ -4397,6 +4413,14 @@ def func(): | |||
| 4397 | 4413 | actual = self.get_suggestion(func) | |
| 4398 | 4414 | self.assertIn("'ZeroDivisionError'?", actual) | |
| 4399 | 4415 | ||
| 4416 | + def test_name_error_suggestions_with_non_string_candidates(self): | ||
| 4417 | + def func(): | ||
| 4418 | + abc = 1 | ||
| 4419 | + globals()[0] = 1 | ||
| 4420 | + abv | ||
| 4421 | + actual = self.get_suggestion(func) | ||
| 4422 | + self.assertIn("abc", actual) | ||
| 4423 | + | ||
| 4400 | 4424 | def test_name_error_suggestions_do_not_trigger_for_long_names(self): | |
| 4401 | 4425 | def func(): | |
| 4402 | 4426 | somethingverywronghehehehehehe = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1595,7 +1595,11 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): | |||
| 1595 | 1595 | if isinstance(exc_value, AttributeError): | |
| 1596 | 1596 | obj = exc_value.obj | |
| 1597 | 1597 | try: | |
| 1598 | - d = dir(obj) | ||
| 1598 | + try: | ||
| 1599 | + d = dir(obj) | ||
| 1600 | + except TypeError: # Attributes are unsortable, e.g. int and str | ||
| 1601 | + d = list(obj.__class__.__dict__.keys()) + list(obj.__dict__.keys()) | ||
| 1602 | + d = sorted([x for x in d if isinstance(x, str)]) | ||
| 1599 | 1603 | hide_underscored = (wrong_name[:1] != '_') | |
| 1600 | 1604 | if hide_underscored and tb is not None: | |
| 1601 | 1605 | while tb.tb_next is not None: | |
@@ -1610,7 +1614,11 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): | |||
| 1610 | 1614 | elif isinstance(exc_value, ImportError): | |
| 1611 | 1615 | try: | |
| 1612 | 1616 | mod = __import__(exc_value.name) | |
| 1613 | - d = dir(mod) | ||
| 1617 | + try: | ||
| 1618 | + d = dir(mod) | ||
| 1619 | + except TypeError: # Attributes are unsortable, e.g. int and str | ||
| 1620 | + d = list(mod.__dict__.keys()) | ||
| 1621 | + d = sorted([x for x in d if isinstance(x, str)]) | ||
| 1614 | 1622 | if wrong_name[:1] != '_': | |
| 1615 | 1623 | d = [x for x in d if x[:1] != '_'] | |
| 1616 | 1624 | except Exception: | |
@@ -1628,6 +1636,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): | |||
| 1628 | 1636 | + list(frame.f_globals) | |
| 1629 | 1637 | + list(frame.f_builtins) | |
| 1630 | 1638 | ) | |
| 1639 | + d = [x for x in d if isinstance(x, str)] | ||
| 1631 | 1640 | ||
| 1632 | 1641 | # Check first if we are in a method and the instance | |
| 1633 | 1642 | # has the wrong name as attribute | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Avoid exiting the new REPL and offer suggestions even if there are non-string | ||
| 2 | + candidates when errors occur. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments