|
49 | 49 |
|
50 | 50 | import re
|
51 | 51 | import struct
|
| 52 | +import sys |
52 | 53 | import warnings
|
53 | 54 | from collections import Counter, OrderedDict, defaultdict, deque
|
| 55 | +from dataclasses import dataclass, field |
54 | 56 | from enum import Enum, Flag
|
55 | 57 | from functools import partial
|
56 | 58 |
|
| 59 | +import attrs |
57 | 60 | import pytest
|
58 | 61 |
|
59 | 62 | from hypothesis import given, strategies as st
|
@@ -758,3 +761,118 @@ def test_pprint_extremely_large_integers():
|
758 | 761 | got = p.getvalue()
|
759 | 762 | assert got == f"{x:#_x}" # hexadecimal with underscores
|
760 | 763 | assert eval(got) == x
|
| 764 | + |
| 765 | + |
| 766 | +class ReprDetector: |
| 767 | + def _repr_pretty_(self, p, cycle): |
| 768 | + """Exercise the IPython callback interface.""" |
| 769 | + p.text("GOOD") |
| 770 | + |
| 771 | + def __repr__(self): |
| 772 | + return "BAD" |
| 773 | + |
| 774 | + |
| 775 | +@dataclass |
| 776 | +class SomeDataClass: |
| 777 | + x: object |
| 778 | + |
| 779 | + |
| 780 | +def test_pretty_prints_data_classes(): |
| 781 | + assert pretty.pretty(SomeDataClass(ReprDetector())) == "SomeDataClass(x=GOOD)" |
| 782 | + |
| 783 | + |
| 784 | +@attrs.define |
| 785 | +class SomeAttrsClass: |
| 786 | + x: ReprDetector |
| 787 | + |
| 788 | + |
| 789 | +@pytest.mark.skipif(sys.version_info[:2] >= (3, 14), reason="FIXME-py314") |
| 790 | +def test_pretty_prints_attrs_classes(): |
| 791 | + assert pretty.pretty(SomeAttrsClass(ReprDetector())) == "SomeAttrsClass(x=GOOD)" |
| 792 | + |
| 793 | + |
| 794 | +@attrs.define |
| 795 | +class SomeAttrsClassWithCustomPretty: |
| 796 | + def _repr_pretty_(self, p, cycle): |
| 797 | + """Exercise the IPython callback interface.""" |
| 798 | + p.text("I am a banana") |
| 799 | + |
| 800 | + |
| 801 | +def test_custom_pretty_print_method_overrides_field_printing(): |
| 802 | + assert pretty.pretty(SomeAttrsClassWithCustomPretty()) == "I am a banana" |
| 803 | + |
| 804 | + |
| 805 | +@attrs.define |
| 806 | +class SomeAttrsClassWithLotsOfFields: |
| 807 | + a: int |
| 808 | + b: int |
| 809 | + c: int |
| 810 | + d: int |
| 811 | + e: int |
| 812 | + f: int |
| 813 | + g: int |
| 814 | + h: int |
| 815 | + i: int |
| 816 | + j: int |
| 817 | + k: int |
| 818 | + l: int |
| 819 | + m: int |
| 820 | + n: int |
| 821 | + o: int |
| 822 | + p: int |
| 823 | + q: int |
| 824 | + r: int |
| 825 | + s: int |
| 826 | + |
| 827 | + |
| 828 | +@pytest.mark.skipif(sys.version_info[:2] >= (3, 14), reason="FIXME-py314") |
| 829 | +def test_will_line_break_between_fields(): |
| 830 | + obj = SomeAttrsClassWithLotsOfFields( |
| 831 | + **{ |
| 832 | + at.name: 12345678900000000000000001 |
| 833 | + for at in SomeAttrsClassWithLotsOfFields.__attrs_attrs__ |
| 834 | + } |
| 835 | + ) |
| 836 | + assert "\n" in pretty.pretty(obj) |
| 837 | + |
| 838 | + |
| 839 | +@attrs.define |
| 840 | +class SomeDataClassWithNoFields: ... |
| 841 | + |
| 842 | + |
| 843 | +def test_prints_empty_dataclass_correctly(): |
| 844 | + assert pretty.pretty(SomeDataClassWithNoFields()) == "SomeDataClassWithNoFields()" |
| 845 | + |
| 846 | + |
| 847 | +def test_handles_cycles_in_dataclass(): |
| 848 | + x = SomeDataClass(x=1) |
| 849 | + x.x = x |
| 850 | + |
| 851 | + assert pretty.pretty(x) == "SomeDataClass(x=SomeDataClass(...))" |
| 852 | + |
| 853 | + |
| 854 | +@dataclass |
| 855 | +class DataClassWithNoInitField: |
| 856 | + x: int |
| 857 | + y: int = field(init=False) |
| 858 | + |
| 859 | + |
| 860 | +def test_does_not_include_no_init_fields_in_dataclass_printing(): |
| 861 | + record = DataClassWithNoInitField(x=1) |
| 862 | + assert pretty.pretty(record) == "DataClassWithNoInitField(x=1)" |
| 863 | + record.y = 1 |
| 864 | + assert pretty.pretty(record) == "DataClassWithNoInitField(x=1)" |
| 865 | + |
| 866 | + |
| 867 | +@attrs.define |
| 868 | +class AttrsClassWithNoInitField: |
| 869 | + x: int |
| 870 | + y: int = attrs.field(init=False) |
| 871 | + |
| 872 | + |
| 873 | +@pytest.mark.skipif(sys.version_info[:2] >= (3, 14), reason="FIXME-py314") |
| 874 | +def test_does_not_include_no_init_fields_in_attrs_printing(): |
| 875 | + record = AttrsClassWithNoInitField(x=1) |
| 876 | + assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)" |
| 877 | + record.y = 1 |
| 878 | + assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)" |
0 commit comments