| 1 |
import collections |
| 2 |
import io |
| 3 |
import sys |
| 4 |
from array import array |
| 5 |
from collections import UserDict, defaultdict, deque |
| 6 |
from dataclasses import dataclass, field |
| 7 |
from typing import Any, List, NamedTuple |
| 8 |
|
| 9 |
import attr |
| 10 |
import pytest |
| 11 |
|
| 12 |
from rich.console import Console |
| 13 |
from rich.measure import Measurement |
| 14 |
from rich.pretty import Node, Pretty, _ipy_display_hook, install, pprint, pretty_repr |
| 15 |
from rich.text import Text |
| 16 |
|
| 17 |
skip_py38 = pytest.mark.skipif( |
| 18 |
sys.version_info.minor == 8 and sys.version_info.major == 3, |
| 19 |
reason="rendered differently on py3.8", |
| 20 |
) |
| 21 |
skip_py39 = pytest.mark.skipif( |
| 22 |
sys.version_info.minor == 9 and sys.version_info.major == 3, |
| 23 |
reason="rendered differently on py3.9", |
| 24 |
) |
| 25 |
skip_py310 = pytest.mark.skipif( |
| 26 |
sys.version_info.minor == 10 and sys.version_info.major == 3, |
| 27 |
reason="rendered differently on py3.10", |
| 28 |
) |
| 29 |
skip_py311 = pytest.mark.skipif( |
| 30 |
sys.version_info.minor == 11 and sys.version_info.major == 3, |
| 31 |
reason="rendered differently on py3.11", |
| 32 |
) |
| 33 |
skip_py312 = pytest.mark.skipif( |
| 34 |
sys.version_info.minor == 12 and sys.version_info.major == 3, |
| 35 |
reason="rendered differently on py3.12", |
| 36 |
) |
| 37 |
skip_py313 = pytest.mark.skipif( |
| 38 |
sys.version_info.minor == 13 and sys.version_info.major == 3, |
| 39 |
reason="rendered differently on py3.13", |
| 40 |
) |
| 41 |
skip_py314 = pytest.mark.skipif( |
| 42 |
sys.version_info.minor == 14 and sys.version_info.major == 3, |
| 43 |
reason="rendered differently on py3.14", |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_install() -> None: |
| 48 |
console = Console(file=io.StringIO()) |
| 49 |
dh = sys.displayhook |
| 50 |
install(console) |
| 51 |
sys.displayhook("foo") |
| 52 |
assert console.file.getvalue() == "'foo'\n" |
| 53 |
assert sys.displayhook is not dh |
| 54 |
|
| 55 |
|
| 56 |
def test_install_max_depth() -> None: |
| 57 |
console = Console(file=io.StringIO()) |
| 58 |
dh = sys.displayhook |
| 59 |
install(console, max_depth=1) |
| 60 |
sys.displayhook({"foo": {"bar": True}}) |
| 61 |
assert console.file.getvalue() == "{'foo': {...}}\n" |
| 62 |
assert sys.displayhook is not dh |
| 63 |
|
| 64 |
|
| 65 |
def test_ipy_display_hook__repr_html() -> None: |
| 66 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 67 |
|
| 68 |
class Thing: |
| 69 |
def _repr_html_(self): |
| 70 |
return "hello" |
| 71 |
|
| 72 |
console.begin_capture() |
| 73 |
_ipy_display_hook(Thing(), console=console) |
| 74 |
|
| 75 |
# Rendering delegated to notebook because _repr_html_ method exists |
| 76 |
assert console.end_capture() == "" |
| 77 |
|
| 78 |
|
| 79 |
def test_ipy_display_hook__multiple_special_reprs() -> None: |
| 80 |
""" |
| 81 |
The case where there are multiple IPython special _repr_*_ |
| 82 |
methods on the object, and one of them returns None but another |
| 83 |
one does not. |
| 84 |
""" |
| 85 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 86 |
|
| 87 |
class Thing: |
| 88 |
def __repr__(self): |
| 89 |
return "A Thing" |
| 90 |
|
| 91 |
def _repr_latex_(self): |
| 92 |
return None |
| 93 |
|
| 94 |
def _repr_html_(self): |
| 95 |
return "hello" |
| 96 |
|
| 97 |
result = _ipy_display_hook(Thing(), console=console) |
| 98 |
assert result == "A Thing" |
| 99 |
|
| 100 |
|
| 101 |
def test_ipy_display_hook__no_special_repr_methods() -> None: |
| 102 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 103 |
|
| 104 |
class Thing: |
| 105 |
def __repr__(self) -> str: |
| 106 |
return "hello" |
| 107 |
|
| 108 |
result = _ipy_display_hook(Thing(), console=console) |
| 109 |
# should be repr as-is |
| 110 |
assert result == "hello" |
| 111 |
|
| 112 |
|
| 113 |
def test_ipy_display_hook__special_repr_raises_exception() -> None: |
| 114 |
""" |
| 115 |
When an IPython special repr method raises an exception, |
| 116 |
we treat it as if it doesn't exist and look for the next. |
| 117 |
""" |
| 118 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 119 |
|
| 120 |
class Thing: |
| 121 |
def _repr_markdown_(self): |
| 122 |
raise Exception() |
| 123 |
|
| 124 |
def _repr_latex_(self): |
| 125 |
return None |
| 126 |
|
| 127 |
def _repr_html_(self): |
| 128 |
return "hello" |
| 129 |
|
| 130 |
def __repr__(self): |
| 131 |
return "therepr" |
| 132 |
|
| 133 |
result = _ipy_display_hook(Thing(), console=console) |
| 134 |
assert result == "therepr" |
| 135 |
|
| 136 |
|
| 137 |
def test_ipy_display_hook__console_renderables_on_newline() -> None: |
| 138 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 139 |
console.begin_capture() |
| 140 |
result = _ipy_display_hook(Text("hello"), console=console) |
| 141 |
assert result == "\nhello" |
| 142 |
|
| 143 |
|
| 144 |
def test_pretty() -> None: |
| 145 |
test = { |
| 146 |
"foo": [1, 2, 3, (4, 5, {6}, 7, 8, {9}), {}], |
| 147 |
"bar": {"egg": "baz", "words": ["Hello World"] * 10}, |
| 148 |
False: "foo", |
| 149 |
True: "", |
| 150 |
"text": ("Hello World", "foo bar baz egg"), |
| 151 |
} |
| 152 |
|
| 153 |
result = pretty_repr(test, max_width=80) |
| 154 |
print(result) |
| 155 |
expected = "{\n 'foo': [1, 2, 3, (4, 5, {6}, 7, 8, {9}), {}],\n 'bar': {\n 'egg': 'baz',\n 'words': [\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World'\n ]\n },\n False: 'foo',\n True: '',\n 'text': ('Hello World', 'foo bar baz egg')\n}" |
| 156 |
print(expected) |
| 157 |
assert result == expected |
| 158 |
|
| 159 |
|
| 160 |
@dataclass |
| 161 |
class ExampleDataclass: |
| 162 |
foo: int |
| 163 |
bar: str |
| 164 |
ignore: int = field(repr=False) |
| 165 |
baz: List[str] = field(default_factory=list) |
| 166 |
last: int = field(default=1, repr=False) |
| 167 |
|
| 168 |
|
| 169 |
@dataclass |
| 170 |
class Empty: |
| 171 |
pass |
| 172 |
|
| 173 |
|
| 174 |
def test_pretty_dataclass() -> None: |
| 175 |
dc = ExampleDataclass(1000, "Hello, World", 999, ["foo", "bar", "baz"]) |
| 176 |
result = pretty_repr(dc, max_width=80) |
| 177 |
print(repr(result)) |
| 178 |
assert ( |
| 179 |
result |
| 180 |
== "ExampleDataclass(foo=1000, bar='Hello, World', baz=['foo', 'bar', 'baz'])" |
| 181 |
) |
| 182 |
result = pretty_repr(dc, max_width=16) |
| 183 |
print(repr(result)) |
| 184 |
assert ( |
| 185 |
result |
| 186 |
== "ExampleDataclass(\n foo=1000,\n bar='Hello, World',\n baz=[\n 'foo',\n 'bar',\n 'baz'\n ]\n)" |
| 187 |
) |
| 188 |
dc.bar = dc |
| 189 |
result = pretty_repr(dc, max_width=80) |
| 190 |
print(repr(result)) |
| 191 |
assert result == "ExampleDataclass(foo=1000, bar=..., baz=['foo', 'bar', 'baz'])" |
| 192 |
|
| 193 |
|
| 194 |
def test_empty_dataclass() -> None: |
| 195 |
assert pretty_repr(Empty()) == "Empty()" |
| 196 |
assert pretty_repr([Empty()]) == "[Empty()]" |
| 197 |
|
| 198 |
|
| 199 |
class StockKeepingUnit(NamedTuple): |
| 200 |
name: str |
| 201 |
description: str |
| 202 |
price: float |
| 203 |
category: str |
| 204 |
reviews: List[str] |
| 205 |
|
| 206 |
|
| 207 |
def test_pretty_namedtuple() -> None: |
| 208 |
console = Console(color_system=None) |
| 209 |
console.begin_capture() |
| 210 |
|
| 211 |
example_namedtuple = StockKeepingUnit( |
| 212 |
"Sparkling British Spring Water", |
| 213 |
"Carbonated spring water", |
| 214 |
0.9, |
| 215 |
"water", |
| 216 |
["its amazing!", "its terrible!"], |
| 217 |
) |
| 218 |
|
| 219 |
result = pretty_repr(example_namedtuple) |
| 220 |
|
| 221 |
print(result) |
| 222 |
assert ( |
| 223 |
result |
| 224 |
== """StockKeepingUnit( |
| 225 |
name='Sparkling British Spring Water', |
| 226 |
description='Carbonated spring water', |
| 227 |
price=0.9, |
| 228 |
category='water', |
| 229 |
reviews=['its amazing!', 'its terrible!'] |
| 230 |
)""" |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
def test_pretty_namedtuple_length_one_no_trailing_comma() -> None: |
| 235 |
instance = collections.namedtuple("Thing", ["name"])(name="Bob") |
| 236 |
assert pretty_repr(instance) == "Thing(name='Bob')" |
| 237 |
|
| 238 |
|
| 239 |
def test_pretty_namedtuple_empty() -> None: |
| 240 |
instance = collections.namedtuple("Thing", [])() |
| 241 |
assert pretty_repr(instance) == "Thing()" |
| 242 |
|
| 243 |
|
| 244 |
def test_pretty_namedtuple_custom_repr() -> None: |
| 245 |
class Thing(NamedTuple): |
| 246 |
def __repr__(self): |
| 247 |
return "XX" |
| 248 |
|
| 249 |
assert pretty_repr(Thing()) == "XX" |
| 250 |
|
| 251 |
|
| 252 |
def test_pretty_namedtuple_fields_invalid_type() -> None: |
| 253 |
class LooksLikeANamedTupleButIsnt(tuple): |
| 254 |
_fields = "blah" |
| 255 |
|
| 256 |
instance = LooksLikeANamedTupleButIsnt() |
| 257 |
result = pretty_repr(instance) |
| 258 |
assert result == "()" # Treated as tuple |
| 259 |
|
| 260 |
|
| 261 |
def test_pretty_namedtuple_max_depth() -> None: |
| 262 |
instance = {"unit": StockKeepingUnit("a", "b", 1.0, "c", ["d", "e"])} |
| 263 |
result = pretty_repr(instance, max_depth=1) |
| 264 |
assert result == "{'unit': StockKeepingUnit(...)}" |
| 265 |
|
| 266 |
|
| 267 |
def test_small_width() -> None: |
| 268 |
test = ["Hello world! 12345"] |
| 269 |
result = pretty_repr(test, max_width=10) |
| 270 |
expected = "[\n 'Hello world! 12345'\n]" |
| 271 |
assert result == expected |
| 272 |
|
| 273 |
|
| 274 |
def test_ansi_in_pretty_repr() -> None: |
| 275 |
class Hello: |
| 276 |
def __repr__(self): |
| 277 |
return "Hello \x1b[38;5;239mWorld!" |
| 278 |
|
| 279 |
pretty = Pretty(Hello()) |
| 280 |
|
| 281 |
console = Console(file=io.StringIO(), record=True) |
| 282 |
console.print(pretty) |
| 283 |
result = console.export_text() |
| 284 |
|
| 285 |
assert result == "Hello World!\n" |
| 286 |
|
| 287 |
|
| 288 |
def test_broken_repr() -> None: |
| 289 |
class BrokenRepr: |
| 290 |
def __repr__(self): |
| 291 |
1 / 0 |
| 292 |
|
| 293 |
test = [BrokenRepr()] |
| 294 |
result = pretty_repr(test) |
| 295 |
expected = "[<repr-error 'division by zero'>]" |
| 296 |
assert result == expected |
| 297 |
|
| 298 |
|
| 299 |
def test_broken_getattr() -> None: |
| 300 |
class BrokenAttr: |
| 301 |
def __getattr__(self, name): |
| 302 |
1 / 0 |
| 303 |
|
| 304 |
def __repr__(self): |
| 305 |
return "BrokenAttr()" |
| 306 |
|
| 307 |
test = BrokenAttr() |
| 308 |
result = pretty_repr(test) |
| 309 |
assert result == "BrokenAttr()" |
| 310 |
|
| 311 |
|
| 312 |
def test_reference_cycle_container() -> None: |
| 313 |
test = [] |
| 314 |
test.append(test) |
| 315 |
res = pretty_repr(test) |
| 316 |
assert res == "[...]" |
| 317 |
|
| 318 |
test = [1, []] |
| 319 |
test[1].append(test) |
| 320 |
res = pretty_repr(test) |
| 321 |
assert res == "[1, [...]]" |
| 322 |
|
| 323 |
# Not a cyclic reference, just a repeated reference |
| 324 |
a = [2] |
| 325 |
test = [1, [a, a]] |
| 326 |
res = pretty_repr(test) |
| 327 |
assert res == "[1, [[2], [2]]]" |
| 328 |
|
| 329 |
|
| 330 |
def test_reference_cycle_namedtuple() -> None: |
| 331 |
class Example(NamedTuple): |
| 332 |
x: int |
| 333 |
y: Any |
| 334 |
|
| 335 |
test = Example(1, [Example(2, [])]) |
| 336 |
test.y[0].y.append(test) |
| 337 |
res = pretty_repr(test) |
| 338 |
assert res == "Example(x=1, y=[Example(x=2, y=[...])])" |
| 339 |
|
| 340 |
# Not a cyclic reference, just a repeated reference |
| 341 |
a = Example(2, None) |
| 342 |
test = Example(1, [a, a]) |
| 343 |
res = pretty_repr(test) |
| 344 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 345 |
|
| 346 |
|
| 347 |
def test_reference_cycle_dataclass() -> None: |
| 348 |
@dataclass |
| 349 |
class Example: |
| 350 |
x: int |
| 351 |
y: Any |
| 352 |
|
| 353 |
test = Example(1, None) |
| 354 |
test.y = test |
| 355 |
res = pretty_repr(test) |
| 356 |
assert res == "Example(x=1, y=...)" |
| 357 |
|
| 358 |
test = Example(1, Example(2, None)) |
| 359 |
test.y.y = test |
| 360 |
res = pretty_repr(test) |
| 361 |
assert res == "Example(x=1, y=Example(x=2, y=...))" |
| 362 |
|
| 363 |
# Not a cyclic reference, just a repeated reference |
| 364 |
a = Example(2, None) |
| 365 |
test = Example(1, [a, a]) |
| 366 |
res = pretty_repr(test) |
| 367 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 368 |
|
| 369 |
|
| 370 |
def test_reference_cycle_attrs() -> None: |
| 371 |
@attr.define |
| 372 |
class Example: |
| 373 |
x: int |
| 374 |
y: Any |
| 375 |
|
| 376 |
test = Example(1, None) |
| 377 |
test.y = test |
| 378 |
res = pretty_repr(test) |
| 379 |
assert res == "Example(x=1, y=...)" |
| 380 |
|
| 381 |
test = Example(1, Example(2, None)) |
| 382 |
test.y.y = test |
| 383 |
res = pretty_repr(test) |
| 384 |
assert res == "Example(x=1, y=Example(x=2, y=...))" |
| 385 |
|
| 386 |
# Not a cyclic reference, just a repeated reference |
| 387 |
a = Example(2, None) |
| 388 |
test = Example(1, [a, a]) |
| 389 |
res = pretty_repr(test) |
| 390 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 391 |
|
| 392 |
|
| 393 |
def test_reference_cycle_custom_repr() -> None: |
| 394 |
class Example: |
| 395 |
def __init__(self, x, y): |
| 396 |
self.x = x |
| 397 |
self.y = y |
| 398 |
|
| 399 |
def __rich_repr__(self): |
| 400 |
yield ("x", self.x) |
| 401 |
yield ("y", self.y) |
| 402 |
|
| 403 |
test = Example(1, None) |
| 404 |
test.y = test |
| 405 |
res = pretty_repr(test) |
| 406 |
assert res == "Example(x=1, y=...)" |
| 407 |
|
| 408 |
test = Example(1, Example(2, None)) |
| 409 |
test.y.y = test |
| 410 |
res = pretty_repr(test) |
| 411 |
assert res == "Example(x=1, y=Example(x=2, y=...))" |
| 412 |
|
| 413 |
# Not a cyclic reference, just a repeated reference |
| 414 |
a = Example(2, None) |
| 415 |
test = Example(1, [a, a]) |
| 416 |
res = pretty_repr(test) |
| 417 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 418 |
|
| 419 |
|
| 420 |
def test_max_depth() -> None: |
| 421 |
d = {} |
| 422 |
d["foo"] = {"fob": {"a": [1, 2, 3], "b": {"z": "x", "y": ["a", "b", "c"]}}} |
| 423 |
|
| 424 |
assert pretty_repr(d, max_depth=0) == "{...}" |
| 425 |
assert pretty_repr(d, max_depth=1) == "{'foo': {...}}" |
| 426 |
assert pretty_repr(d, max_depth=2) == "{'foo': {'fob': {...}}}" |
| 427 |
assert pretty_repr(d, max_depth=3) == "{'foo': {'fob': {'a': [...], 'b': {...}}}}" |
| 428 |
assert ( |
| 429 |
pretty_repr(d, max_width=100, max_depth=4) |
| 430 |
== "{'foo': {'fob': {'a': [1, 2, 3], 'b': {'z': 'x', 'y': [...]}}}}" |
| 431 |
) |
| 432 |
assert ( |
| 433 |
pretty_repr(d, max_width=100, max_depth=5) |
| 434 |
== "{'foo': {'fob': {'a': [1, 2, 3], 'b': {'z': 'x', 'y': ['a', 'b', 'c']}}}}" |
| 435 |
) |
| 436 |
assert ( |
| 437 |
pretty_repr(d, max_width=100, max_depth=None) |
| 438 |
== "{'foo': {'fob': {'a': [1, 2, 3], 'b': {'z': 'x', 'y': ['a', 'b', 'c']}}}}" |
| 439 |
) |
| 440 |
|
| 441 |
|
| 442 |
def test_max_depth_rich_repr() -> None: |
| 443 |
class Foo: |
| 444 |
def __init__(self, foo): |
| 445 |
self.foo = foo |
| 446 |
|
| 447 |
def __rich_repr__(self): |
| 448 |
yield "foo", self.foo |
| 449 |
|
| 450 |
class Bar: |
| 451 |
def __init__(self, bar): |
| 452 |
self.bar = bar |
| 453 |
|
| 454 |
def __rich_repr__(self): |
| 455 |
yield "bar", self.bar |
| 456 |
|
| 457 |
assert ( |
| 458 |
pretty_repr(Foo(foo=Bar(bar=Foo(foo=[]))), max_depth=2) |
| 459 |
== "Foo(foo=Bar(bar=Foo(...)))" |
| 460 |
) |
| 461 |
|
| 462 |
|
| 463 |
def test_max_depth_attrs() -> None: |
| 464 |
@attr.define |
| 465 |
class Foo: |
| 466 |
foo = attr.field() |
| 467 |
|
| 468 |
@attr.define |
| 469 |
class Bar: |
| 470 |
bar = attr.field() |
| 471 |
|
| 472 |
assert ( |
| 473 |
pretty_repr(Foo(foo=Bar(bar=Foo(foo=[]))), max_depth=2) |
| 474 |
== "Foo(foo=Bar(bar=Foo(...)))" |
| 475 |
) |
| 476 |
|
| 477 |
|
| 478 |
def test_max_depth_dataclass() -> None: |
| 479 |
@dataclass |
| 480 |
class Foo: |
| 481 |
foo: object |
| 482 |
|
| 483 |
@dataclass |
| 484 |
class Bar: |
| 485 |
bar: object |
| 486 |
|
| 487 |
assert ( |
| 488 |
pretty_repr(Foo(foo=Bar(bar=Foo(foo=[]))), max_depth=2) |
| 489 |
== "Foo(foo=Bar(bar=Foo(...)))" |
| 490 |
) |
| 491 |
|
| 492 |
|
| 493 |
def test_defaultdict() -> None: |
| 494 |
test_dict = defaultdict(int, {"foo": 2}) |
| 495 |
result = pretty_repr(test_dict) |
| 496 |
assert result == "defaultdict(<class 'int'>, {'foo': 2})" |
| 497 |
|
| 498 |
|
| 499 |
def test_deque() -> None: |
| 500 |
test_deque = deque([1, 2, 3]) |
| 501 |
result = pretty_repr(test_deque) |
| 502 |
assert result == "deque([1, 2, 3])" |
| 503 |
test_deque = deque([1, 2, 3], maxlen=None) |
| 504 |
result = pretty_repr(test_deque) |
| 505 |
assert result == "deque([1, 2, 3])" |
| 506 |
test_deque = deque([1, 2, 3], maxlen=5) |
| 507 |
result = pretty_repr(test_deque) |
| 508 |
assert result == "deque([1, 2, 3], maxlen=5)" |
| 509 |
test_deque = deque([1, 2, 3], maxlen=0) |
| 510 |
result = pretty_repr(test_deque) |
| 511 |
assert result == "deque(maxlen=0)" |
| 512 |
test_deque = deque([]) |
| 513 |
result = pretty_repr(test_deque) |
| 514 |
assert result == "deque()" |
| 515 |
test_deque = deque([], maxlen=None) |
| 516 |
result = pretty_repr(test_deque) |
| 517 |
assert result == "deque()" |
| 518 |
test_deque = deque([], maxlen=5) |
| 519 |
result = pretty_repr(test_deque) |
| 520 |
assert result == "deque(maxlen=5)" |
| 521 |
test_deque = deque([], maxlen=0) |
| 522 |
result = pretty_repr(test_deque) |
| 523 |
assert result == "deque(maxlen=0)" |
| 524 |
|
| 525 |
|
| 526 |
def test_array() -> None: |
| 527 |
test_array = array("I", [1, 2, 3]) |
| 528 |
result = pretty_repr(test_array) |
| 529 |
assert result == "array('I', [1, 2, 3])" |
| 530 |
|
| 531 |
|
| 532 |
def test_tuple_of_one() -> None: |
| 533 |
assert pretty_repr((1,)) == "(1,)" |
| 534 |
|
| 535 |
|
| 536 |
def test_node() -> None: |
| 537 |
node = Node("abc") |
| 538 |
assert pretty_repr(node) == "abc: " |
| 539 |
|
| 540 |
|
| 541 |
def test_indent_lines() -> None: |
| 542 |
console = Console(width=100, color_system=None) |
| 543 |
console.begin_capture() |
| 544 |
console.print(Pretty([100, 200], indent_guides=True), width=8) |
| 545 |
expected = """\ |
| 546 |
[ |
| 547 |
│ 100, |
| 548 |
│ 200 |
| 549 |
] |
| 550 |
""" |
| 551 |
result = console.end_capture() |
| 552 |
print(repr(result)) |
| 553 |
print(result) |
| 554 |
assert result == expected |
| 555 |
|
| 556 |
|
| 557 |
def test_pprint() -> None: |
| 558 |
console = Console(color_system=None) |
| 559 |
console.begin_capture() |
| 560 |
pprint(1, console=console) |
| 561 |
assert console.end_capture() == "1\n" |
| 562 |
|
| 563 |
|
| 564 |
def test_pprint_max_values() -> None: |
| 565 |
console = Console(color_system=None) |
| 566 |
console.begin_capture() |
| 567 |
pprint([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], console=console, max_length=2) |
| 568 |
assert console.end_capture() == "[1, 2, ... +8]\n" |
| 569 |
|
| 570 |
|
| 571 |
def test_pprint_max_items() -> None: |
| 572 |
console = Console(color_system=None) |
| 573 |
console.begin_capture() |
| 574 |
pprint({"foo": 1, "bar": 2, "egg": 3}, console=console, max_length=2) |
| 575 |
assert console.end_capture() == """{'foo': 1, 'bar': 2, ... +1}\n""" |
| 576 |
|
| 577 |
|
| 578 |
def test_pprint_max_string() -> None: |
| 579 |
console = Console(color_system=None) |
| 580 |
console.begin_capture() |
| 581 |
pprint(["Hello" * 20], console=console, max_string=8) |
| 582 |
assert console.end_capture() == """['HelloHel'+92]\n""" |
| 583 |
|
| 584 |
|
| 585 |
def test_tuples() -> None: |
| 586 |
console = Console(color_system=None) |
| 587 |
console.begin_capture() |
| 588 |
pprint((1,), console=console) |
| 589 |
pprint((1,), expand_all=True, console=console) |
| 590 |
pprint(((1,),), expand_all=True, console=console) |
| 591 |
result = console.end_capture() |
| 592 |
print(repr(result)) |
| 593 |
expected = "(1,)\n(\n│ 1,\n)\n(\n│ (\n│ │ 1,\n│ ),\n)\n" |
| 594 |
print(result) |
| 595 |
print("--") |
| 596 |
print(expected) |
| 597 |
assert result == expected |
| 598 |
|
| 599 |
|
| 600 |
def test_newline() -> None: |
| 601 |
console = Console(color_system=None) |
| 602 |
console.begin_capture() |
| 603 |
console.print(Pretty((1,), insert_line=True, expand_all=True)) |
| 604 |
result = console.end_capture() |
| 605 |
expected = "\n(\n 1,\n)\n" |
| 606 |
assert result == expected |
| 607 |
|
| 608 |
|
| 609 |
def test_empty_repr() -> None: |
| 610 |
class Foo: |
| 611 |
def __repr__(self): |
| 612 |
return "" |
| 613 |
|
| 614 |
assert pretty_repr(Foo()) == "" |
| 615 |
|
| 616 |
|
| 617 |
def test_attrs() -> None: |
| 618 |
@attr.define |
| 619 |
class Point: |
| 620 |
x: int |
| 621 |
y: int |
| 622 |
foo: str = attr.field(repr=str.upper) |
| 623 |
z: int = 0 |
| 624 |
|
| 625 |
result = pretty_repr(Point(1, 2, foo="bar")) |
| 626 |
print(repr(result)) |
| 627 |
expected = "Point(x=1, y=2, foo=BAR, z=0)" |
| 628 |
assert result == expected |
| 629 |
|
| 630 |
|
| 631 |
def test_attrs_empty() -> None: |
| 632 |
@attr.define |
| 633 |
class Nada: |
| 634 |
pass |
| 635 |
|
| 636 |
result = pretty_repr(Nada()) |
| 637 |
print(repr(result)) |
| 638 |
expected = "Nada()" |
| 639 |
assert result == expected |
| 640 |
|
| 641 |
|
| 642 |
@skip_py310 |
| 643 |
@skip_py311 |
| 644 |
@skip_py312 |
| 645 |
@skip_py313 |
| 646 |
@skip_py314 |
| 647 |
def test_attrs_broken() -> None: |
| 648 |
@attr.define |
| 649 |
class Foo: |
| 650 |
bar: int |
| 651 |
|
| 652 |
foo = Foo(1) |
| 653 |
del foo.bar |
| 654 |
result = pretty_repr(foo) |
| 655 |
print(repr(result)) |
| 656 |
expected = "Foo(bar=AttributeError('bar'))" |
| 657 |
assert result == expected |
| 658 |
|
| 659 |
|
| 660 |
@skip_py38 |
| 661 |
@skip_py39 |
| 662 |
def test_attrs_broken_310() -> None: |
| 663 |
@attr.define |
| 664 |
class Foo: |
| 665 |
bar: int |
| 666 |
|
| 667 |
foo = Foo(1) |
| 668 |
del foo.bar |
| 669 |
result = pretty_repr(foo) |
| 670 |
print(repr(result)) |
| 671 |
if sys.version_info >= (3, 13): |
| 672 |
expected = "Foo(\n bar=AttributeError(\"'tests.test_pretty.test_attrs_broken_310.<locals>.Foo' object has no attribute 'bar'\")\n)" |
| 673 |
else: |
| 674 |
expected = "Foo(bar=AttributeError(\"'Foo' object has no attribute 'bar'\"))" |
| 675 |
assert result == expected |
| 676 |
|
| 677 |
|
| 678 |
def test_user_dict() -> None: |
| 679 |
class D1(UserDict): |
| 680 |
pass |
| 681 |
|
| 682 |
class D2(UserDict): |
| 683 |
def __repr__(self): |
| 684 |
return "FOO" |
| 685 |
|
| 686 |
d1 = D1({"foo": "bar"}) |
| 687 |
d2 = D2({"foo": "bar"}) |
| 688 |
result = pretty_repr(d1, expand_all=True) |
| 689 |
print(repr(result)) |
| 690 |
assert result == "{\n 'foo': 'bar'\n}" |
| 691 |
result = pretty_repr(d2, expand_all=True) |
| 692 |
print(repr(result)) |
| 693 |
assert result == "FOO" |
| 694 |
|
| 695 |
|
| 696 |
def test_lying_attribute() -> None: |
| 697 |
"""Test getattr doesn't break rich repr protocol""" |
| 698 |
|
| 699 |
class Foo: |
| 700 |
def __getattr__(self, attr): |
| 701 |
return "foo" |
| 702 |
|
| 703 |
foo = Foo() |
| 704 |
result = pretty_repr(foo) |
| 705 |
assert "Foo" in result |
| 706 |
|
| 707 |
|
| 708 |
def test_measure_pretty() -> None: |
| 709 |
"""Test measure respects expand_all""" |
| 710 |
# https://github.com/Textualize/rich/issues/1998 |
| 711 |
console = Console() |
| 712 |
pretty = Pretty(["alpha", "beta", "delta", "gamma"], expand_all=True) |
| 713 |
|
| 714 |
measurement = console.measure(pretty) |
| 715 |
assert measurement == Measurement(12, 12) |
| 716 |
|
| 717 |
|
| 718 |
def test_tuple_rich_repr() -> None: |
| 719 |
""" |
| 720 |
Test that can use None as key to have tuple positional values. |
| 721 |
""" |
| 722 |
|
| 723 |
class Foo: |
| 724 |
def __rich_repr__(self): |
| 725 |
yield None, (1,) |
| 726 |
|
| 727 |
assert pretty_repr(Foo()) == "Foo((1,))" |
| 728 |
|
| 729 |
|
| 730 |
def test_tuple_rich_repr_default() -> None: |
| 731 |
""" |
| 732 |
Test that can use None as key to have tuple positional values and with a default. |
| 733 |
""" |
| 734 |
|
| 735 |
class Foo: |
| 736 |
def __rich_repr__(self): |
| 737 |
yield None, (1,), (1,) |
| 738 |
|
| 739 |
assert pretty_repr(Foo()) == "Foo()" |
| 740 |
|
| 741 |
|
| 742 |
def test_dataclass_no_attribute() -> None: |
| 743 |
"""Regression test for https://github.com/Textualize/rich/issues/3417""" |
| 744 |
from dataclasses import dataclass, field |
| 745 |
|
| 746 |
@dataclass(eq=False) |
| 747 |
class BadDataclass: |
| 748 |
item: int = field(init=False) |
| 749 |
|
| 750 |
# item is not provided |
| 751 |
bad_data_class = BadDataclass() |
| 752 |
|
| 753 |
console = Console() |
| 754 |
with console.capture() as capture: |
| 755 |
console.print(bad_data_class) |
| 756 |
|
| 757 |
expected = "BadDataclass()\n" |
| 758 |
result = capture.get() |
| 759 |
assert result == expected |