Skip to content

Commit

Permalink
Merge pull request #1496 from willmcgugan/pretty-fix
Browse files Browse the repository at this point in the history
test fake attributes
  • Loading branch information
willmcgugan authored Sep 18, 2021
2 parents ce51285 + f613b3e commit 23aa717
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [10.10.0] - 2021-09-18

### Added

- Added stdin support to `rich.json`

### Fixed

- Fixed pretty printing of objects with fo magic with **getattr** https://github.com/willmcgugan/rich/issues/1492

## [10.9.0] - 2021-08-29

### Added
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "10.9.0"
version = "10.10.0"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <[email protected]>"]
license = "MIT"
Expand Down
23 changes: 20 additions & 3 deletions rich/pretty.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import builtins
import os
from rich.repr import RichReprResult
import sys
from array import array
from collections import Counter, defaultdict, deque, UserDict, UserList
Expand Down Expand Up @@ -503,9 +504,24 @@ def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
else:
yield arg

if hasattr(obj, "__rich_repr__") and not isclass(obj):
try:
fake_attributes = hasattr(
obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
)
except Exception:
fake_attributes = False

rich_repr_result: Optional[RichReprResult] = None
if not fake_attributes:
try:
if hasattr(obj, "__rich_repr__") and not isclass(obj):
rich_repr_result = obj.__rich_repr__()
except Exception:
pass

if rich_repr_result is not None:
angular = getattr(obj.__rich_repr__, "angular", False)
args = list(iter_rich_args(obj.__rich_repr__()))
args = list(iter_rich_args(rich_repr_result))
class_name = obj.__class__.__name__

if args:
Expand Down Expand Up @@ -544,7 +560,7 @@ def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
children=[],
last=root,
)
elif _is_attr_object(obj):
elif _is_attr_object(obj) and not fake_attributes:
children = []
append = children.append

Expand Down Expand Up @@ -592,6 +608,7 @@ def iter_attrs() -> Iterable[
elif (
is_dataclass(obj)
and not isinstance(obj, type)
and not fake_attributes
and (
"__create_fn__" in obj.__repr__.__qualname__ or py_version == (3, 6)
) # Check if __repr__ wasn't overridden
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,15 @@ def __repr__(self):
result = pretty_repr(d2, expand_all=True)
print(repr(result))
assert result == "FOO"


def test_lying_attribute():
"""Test getattr doesn't break rich repr protocol"""

class Foo:
def __getattr__(self, attr):
return "foo"

foo = Foo()
result = pretty_repr(foo)
assert "Foo" in result

0 comments on commit 23aa717

Please sign in to comment.