(exc_value, tb, wrong_name)
| 1590 | |
| 1591 | |
| 1592 | def _compute_suggestion_error(exc_value, tb, wrong_name): |
| 1593 | if wrong_name is None or not isinstance(wrong_name, str): |
| 1594 | return None |
| 1595 | if isinstance(exc_value, AttributeError): |
| 1596 | obj = exc_value.obj |
| 1597 | try: |
| 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)]) |
| 1603 | hide_underscored = (wrong_name[:1] != '_') |
| 1604 | if hide_underscored and tb is not None: |
| 1605 | while tb.tb_next is not None: |
| 1606 | tb = tb.tb_next |
| 1607 | frame = tb.tb_frame |
| 1608 | if 'self' in frame.f_locals and frame.f_locals['self'] is obj: |
| 1609 | hide_underscored = False |
| 1610 | if hide_underscored: |
| 1611 | d = [x for x in d if x[:1] != '_'] |
| 1612 | except Exception: |
| 1613 | return None |
| 1614 | elif isinstance(exc_value, ImportError): |
| 1615 | try: |
| 1616 | mod = __import__(exc_value.name) |
| 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)]) |
| 1622 | if wrong_name[:1] != '_': |
| 1623 | d = [x for x in d if x[:1] != '_'] |
| 1624 | except Exception: |
| 1625 | return None |
| 1626 | else: |
| 1627 | assert isinstance(exc_value, NameError) |
| 1628 | # find most recent frame |
| 1629 | if tb is None: |
| 1630 | return None |
| 1631 | while tb.tb_next is not None: |
| 1632 | tb = tb.tb_next |
| 1633 | frame = tb.tb_frame |
| 1634 | d = ( |
| 1635 | list(frame.f_locals) |
| 1636 | + list(frame.f_globals) |
| 1637 | + list(frame.f_builtins) |
| 1638 | ) |
| 1639 | d = [x for x in d if isinstance(x, str)] |
| 1640 | |
| 1641 | # Check first if we are in a method and the instance |
| 1642 | # has the wrong name as attribute |
| 1643 | if 'self' in frame.f_locals: |
| 1644 | self = frame.f_locals['self'] |
| 1645 | try: |
| 1646 | has_wrong_name = hasattr(self, wrong_name) |
| 1647 | except Exception: |
| 1648 | has_wrong_name = False |
| 1649 | if has_wrong_name: |
no test coverage detected