File size: 21,375 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import io
import sys
from types import ModuleType
from typing import Sequence, Type

import pytest

from rich import inspect
from rich._inspect import (
    get_object_types_mro,
    get_object_types_mro_as_strings,
    is_object_one_of_types,
)
from rich.console import Console

skip_py37 = pytest.mark.skipif(
    sys.version_info.minor == 7 and sys.version_info.major == 3,
    reason="rendered differently on py3.7",
)

skip_py38 = pytest.mark.skipif(
    sys.version_info.minor == 8 and sys.version_info.major == 3,
    reason="rendered differently on py3.8",
)

skip_py39 = pytest.mark.skipif(
    sys.version_info.minor == 9 and sys.version_info.major == 3,
    reason="rendered differently on py3.9",
)

skip_py310 = pytest.mark.skipif(
    sys.version_info.minor == 10 and sys.version_info.major == 3,
    reason="rendered differently on py3.10",
)

skip_py311 = pytest.mark.skipif(
    sys.version_info.minor == 11 and sys.version_info.major == 3,
    reason="rendered differently on py3.11",
)

skip_py312 = pytest.mark.skipif(
    sys.version_info.minor == 12 and sys.version_info.major == 3,
    reason="rendered differently on py3.12",
)

skip_py313 = pytest.mark.skipif(
    sys.version_info.minor == 13 and sys.version_info.major == 3,
    reason="rendered differently on py3.13",
)

skip_pypy3 = pytest.mark.skipif(
    hasattr(sys, "pypy_version_info"),
    reason="rendered differently on pypy3",
)


def render(obj, methods=False, value=False, width=50) -> str:
    console = Console(file=io.StringIO(), width=width, legacy_windows=False)
    inspect(obj, console=console, methods=methods, value=value)
    return console.file.getvalue()


class InspectError(Exception):
    def __str__(self) -> str:
        return "INSPECT ERROR"


class Foo:
    """Foo test

    Second line
    """

    def __init__(self, foo: int) -> None:
        """constructor docs."""
        self.foo = foo

    @property
    def broken(self):
        raise InspectError()

    def method(self, a, b) -> str:
        """Multi line

        docs.
        """
        return "test"

    def __dir__(self):
        return ["__init__", "broken", "method"]


class FooSubclass(Foo):
    pass


def test_render():
    console = Console(width=100, file=io.StringIO(), legacy_windows=False)

    foo = Foo("hello")
    inspect(foo, console=console, all=True, value=False)
    result = console.file.getvalue()
    print(repr(result))
    expected = "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'tests.test_inspect.Foo'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\nโ”‚ Foo test                                                     โ”‚\nโ”‚                                                              โ”‚\nโ”‚   broken = InspectError()                                    โ”‚\nโ”‚ __init__ = def __init__(foo: int) -> None: constructor docs. โ”‚\nโ”‚   method = def method(a, b) -> str: Multi line               โ”‚\nโ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    assert result == expected


@skip_pypy3
def test_inspect_text():
    num_attributes = 34 if sys.version_info >= (3, 11) else 33
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'str'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ str(object='') -> str                          โ”‚\n"
        "โ”‚ str(bytes_or_buffer[, encoding[, errors]]) ->  โ”‚\n"
        "โ”‚ str                                            โ”‚\n"
        "โ”‚                                                โ”‚\n"
        f"โ”‚ {num_attributes} attribute(s) not shown. Run                 โ”‚\n"
        "โ”‚ inspect(inspect) for options.                  โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    print(repr(expected))
    assert render("Hello") == expected


@skip_py37
@skip_pypy3
def test_inspect_empty_dict():
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'dict'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ dict() -> new empty dictionary                 โ”‚\n"
        "โ”‚ dict(mapping) -> new dictionary initialized    โ”‚\n"
        "โ”‚ from a mapping object's                        โ”‚\n"
        "โ”‚     (key, value) pairs                         โ”‚\n"
        "โ”‚ dict(iterable) -> new dictionary initialized   โ”‚\n"
        "โ”‚ as if via:                                     โ”‚\n"
        "โ”‚     d = {}                                     โ”‚\n"
        "โ”‚     for k, v in iterable:                      โ”‚\n"
        "โ”‚         d[k] = v                               โ”‚\n"
        "โ”‚ dict(**kwargs) -> new dictionary initialized   โ”‚\n"
        "โ”‚ with the name=value pairs                      โ”‚\n"
        "โ”‚     in the keyword argument list.  For         โ”‚\n"
        "โ”‚ example:  dict(one=1, two=2)                   โ”‚\n"
        "โ”‚                                                โ”‚\n"
    )
    assert render({}).startswith(expected)


@skip_py313
@skip_py312
@skip_py311
@skip_pypy3
def test_inspect_builtin_function_except_python311():
    # Pre-3.11 Python versions - print builtin has no signature available
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <built-in function print> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ def print(...)                                 โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚ print(value, ..., sep=' ', end='\\n',           โ”‚\n"
        "โ”‚ file=sys.stdout, flush=False)                  โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚ 29 attribute(s) not shown. Run                 โ”‚\n"
        "โ”‚ inspect(inspect) for options.                  โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert render(print) == expected


@pytest.mark.skipif(
    sys.version_info < (3, 11), reason="print builtin signature only available on 3.11+"
)
@skip_pypy3
def test_inspect_builtin_function_only_python311():
    # On 3.11, the print builtin *does* have a signature, unlike in prior versions
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <built-in function print> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ def print(*args, sep=' ', end='\\n', file=None, โ”‚\n"
        "โ”‚ flush=False):                                  โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚ Prints the values to a stream, or to           โ”‚\n"
        "โ”‚ sys.stdout by default.                         โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚ 30 attribute(s) not shown. Run                 โ”‚\n"
        "โ”‚ inspect(inspect) for options.                  โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert render(print) == expected


@skip_pypy3
def test_inspect_coroutine():
    async def coroutine():
        pass

    expected = (
        "โ•ญโ”€ <function test_inspect_coroutine.<locals>.corโ”€โ•ฎ\n"
        "โ”‚ async def                                      โ”‚\n"
        "โ”‚ test_inspect_coroutine.<locals>.coroutine():   โ”‚\n"
    )
    assert render(coroutine).startswith(expected)


def test_inspect_integer():
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€ <class 'int'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ int([x]) -> integer        โ”‚\n"
        "โ”‚ int(x, base=10) -> integer โ”‚\n"
        "โ”‚                            โ”‚\n"
        "โ”‚ denominator = 1            โ”‚\n"
        "โ”‚        imag = 0            โ”‚\n"
        "โ”‚   numerator = 1            โ”‚\n"
        "โ”‚        real = 1            โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert expected == render(1)


def test_inspect_integer_with_value():
    expected = "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€ <class 'int'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\nโ”‚ int([x]) -> integer        โ”‚\nโ”‚ int(x, base=10) -> integer โ”‚\nโ”‚                            โ”‚\nโ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚\nโ”‚ โ”‚ 1                      โ”‚ โ”‚\nโ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚\nโ”‚                            โ”‚\nโ”‚ denominator = 1            โ”‚\nโ”‚        imag = 0            โ”‚\nโ”‚   numerator = 1            โ”‚\nโ”‚        real = 1            โ”‚\nโ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    value = render(1, value=True)
    print(repr(value))
    assert value == expected


@skip_py37
@skip_py310
@skip_py311
@skip_py312
@skip_py313
def test_inspect_integer_with_methods_python38_and_python39():
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'int'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ int([x]) -> integer                            โ”‚\n"
        "โ”‚ int(x, base=10) -> integer                     โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚      denominator = 1                           โ”‚\n"
        "โ”‚             imag = 0                           โ”‚\n"
        "โ”‚        numerator = 1                           โ”‚\n"
        "โ”‚             real = 1                           โ”‚\n"
        "โ”‚ as_integer_ratio = def as_integer_ratio():     โ”‚\n"
        "โ”‚                    Return integer ratio.       โ”‚\n"
        "โ”‚       bit_length = def bit_length(): Number of โ”‚\n"
        "โ”‚                    bits necessary to represent โ”‚\n"
        "โ”‚                    self in binary.             โ”‚\n"
        "โ”‚        conjugate = def conjugate(...) Returns  โ”‚\n"
        "โ”‚                    self, the complex conjugate โ”‚\n"
        "โ”‚                    of any int.                 โ”‚\n"
        "โ”‚       from_bytes = def from_bytes(bytes,       โ”‚\n"
        "โ”‚                    byteorder, *,               โ”‚\n"
        "โ”‚                    signed=False): Return the   โ”‚\n"
        "โ”‚                    integer represented by the  โ”‚\n"
        "โ”‚                    given array of bytes.       โ”‚\n"
        "โ”‚         to_bytes = def to_bytes(length,        โ”‚\n"
        "โ”‚                    byteorder, *,               โ”‚\n"
        "โ”‚                    signed=False): Return an    โ”‚\n"
        "โ”‚                    array of bytes representing โ”‚\n"
        "โ”‚                    an integer.                 โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert render(1, methods=True) == expected


@skip_py37
@skip_py38
@skip_py39
@skip_py311
@skip_py312
@skip_py313
def test_inspect_integer_with_methods_python310only():
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'int'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ int([x]) -> integer                            โ”‚\n"
        "โ”‚ int(x, base=10) -> integer                     โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚      denominator = 1                           โ”‚\n"
        "โ”‚             imag = 0                           โ”‚\n"
        "โ”‚        numerator = 1                           โ”‚\n"
        "โ”‚             real = 1                           โ”‚\n"
        "โ”‚ as_integer_ratio = def as_integer_ratio():     โ”‚\n"
        "โ”‚                    Return integer ratio.       โ”‚\n"
        "โ”‚        bit_count = def bit_count(): Number of  โ”‚\n"
        "โ”‚                    ones in the binary          โ”‚\n"
        "โ”‚                    representation of the       โ”‚\n"
        "โ”‚                    absolute value of self.     โ”‚\n"
        "โ”‚       bit_length = def bit_length(): Number of โ”‚\n"
        "โ”‚                    bits necessary to represent โ”‚\n"
        "โ”‚                    self in binary.             โ”‚\n"
        "โ”‚        conjugate = def conjugate(...) Returns  โ”‚\n"
        "โ”‚                    self, the complex conjugate โ”‚\n"
        "โ”‚                    of any int.                 โ”‚\n"
        "โ”‚       from_bytes = def from_bytes(bytes,       โ”‚\n"
        "โ”‚                    byteorder, *,               โ”‚\n"
        "โ”‚                    signed=False): Return the   โ”‚\n"
        "โ”‚                    integer represented by the  โ”‚\n"
        "โ”‚                    given array of bytes.       โ”‚\n"
        "โ”‚         to_bytes = def to_bytes(length,        โ”‚\n"
        "โ”‚                    byteorder, *,               โ”‚\n"
        "โ”‚                    signed=False): Return an    โ”‚\n"
        "โ”‚                    array of bytes representing โ”‚\n"
        "โ”‚                    an integer.                 โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert render(1, methods=True) == expected


@skip_py37
@skip_py38
@skip_py39
@skip_py310
@skip_py312
@skip_py313
def test_inspect_integer_with_methods_python311():
    # to_bytes and from_bytes methods on int had minor signature change -
    # they now, as of 3.11, have default values for all of their parameters
    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'int'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚ int([x]) -> integer                            โ”‚\n"
        "โ”‚ int(x, base=10) -> integer                     โ”‚\n"
        "โ”‚                                                โ”‚\n"
        "โ”‚      denominator = 1                           โ”‚\n"
        "โ”‚             imag = 0                           โ”‚\n"
        "โ”‚        numerator = 1                           โ”‚\n"
        "โ”‚             real = 1                           โ”‚\n"
        "โ”‚ as_integer_ratio = def as_integer_ratio():     โ”‚\n"
        "โ”‚                    Return integer ratio.       โ”‚\n"
        "โ”‚        bit_count = def bit_count(): Number of  โ”‚\n"
        "โ”‚                    ones in the binary          โ”‚\n"
        "โ”‚                    representation of the       โ”‚\n"
        "โ”‚                    absolute value of self.     โ”‚\n"
        "โ”‚       bit_length = def bit_length(): Number of โ”‚\n"
        "โ”‚                    bits necessary to represent โ”‚\n"
        "โ”‚                    self in binary.             โ”‚\n"
        "โ”‚        conjugate = def conjugate(...) Returns  โ”‚\n"
        "โ”‚                    self, the complex conjugate โ”‚\n"
        "โ”‚                    of any int.                 โ”‚\n"
        "โ”‚       from_bytes = def from_bytes(bytes,       โ”‚\n"
        "โ”‚                    byteorder='big', *,         โ”‚\n"
        "โ”‚                    signed=False): Return the   โ”‚\n"
        "โ”‚                    integer represented by the  โ”‚\n"
        "โ”‚                    given array of bytes.       โ”‚\n"
        "โ”‚         to_bytes = def to_bytes(length=1,      โ”‚\n"
        "โ”‚                    byteorder='big', *,         โ”‚\n"
        "โ”‚                    signed=False): Return an    โ”‚\n"
        "โ”‚                    array of bytes representing โ”‚\n"
        "โ”‚                    an integer.                 โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert render(1, methods=True) == expected


@skip_py37
@skip_pypy3
def test_broken_call_attr():
    class NotCallable:
        __call__ = 5  # Passes callable() but isn't really callable

        def __repr__(self):
            return "NotCallable()"

    class Foo:
        foo = NotCallable()

    foo = Foo()
    assert callable(foo.foo)
    expected = "โ•ญโ”€ <class 'tests.test_inspect.test_broken_call_attr.<locals>.Foo'> โ”€โ•ฎ\nโ”‚ foo = NotCallable()                                               โ”‚\nโ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    result = render(foo, methods=True, width=100)
    print(repr(result))
    assert expected == result


def test_inspect_swig_edge_case():
    """Issue #1838 - Edge case with Faiss library - object with empty dir()"""

    class Thing:
        @property
        def __class__(self):
            raise AttributeError

    thing = Thing()
    try:
        inspect(thing)
    except Exception as e:
        assert False, f"Object with no __class__ shouldn't raise {e}"


def test_inspect_module_with_class():
    def function():
        pass

    class Thing:
        """Docstring"""

        pass

    module = ModuleType("my_module")
    module.SomeClass = Thing
    module.function = function

    expected = (
        "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <module 'my_module'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\n"
        "โ”‚  function = def function():              โ”‚\n"
        "โ”‚ SomeClass = class SomeClass(): Docstring โ”‚\n"
        "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
    )
    assert render(module, methods=True) == expected


@pytest.mark.parametrize(
    "special_character,expected_replacement",
    (
        ("\a", "\\a"),
        ("\b", "\\b"),
        ("\f", "\\f"),
        ("\r", "\\r"),
        ("\v", "\\v"),
    ),
)
def test_can_handle_special_characters_in_docstrings(
    special_character: str, expected_replacement: str
) -> None:
    class Something:
        class Thing:
            pass

    Something.Thing.__doc__ = f"""
    Multiline docstring
    with {special_character} should be handled
    """

    expected = """\
โ•ญโ”€ <class 'tests.test_inspect.test_can_handle_spโ”€โ•ฎ
โ”‚ class                                          โ”‚
โ”‚ test_can_handle_special_characters_in_docstrin โ”‚
โ”‚ gs.<locals>.Something():                       โ”‚
โ”‚                                                โ”‚
โ”‚ Thing = class Thing():                         โ”‚
โ”‚         Multiline docstring                    โ”‚
โ”‚         with %s should be handled              โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
""" % (
        expected_replacement
    )
    assert render(Something, methods=True) == expected


@pytest.mark.parametrize(
    "obj,expected_result",
    (
        [object, (object,)],
        [object(), (object,)],
        ["hi", (str, object)],
        [str, (str, object)],
        [Foo(1), (Foo, object)],
        [Foo, (Foo, object)],
        [FooSubclass(1), (FooSubclass, Foo, object)],
        [FooSubclass, (FooSubclass, Foo, object)],
    ),
)
def test_object_types_mro(obj: object, expected_result: Sequence[Type]):
    assert get_object_types_mro(obj) == expected_result


@pytest.mark.parametrize(
    "obj,expected_result",
    (
        # fmt: off
        ["hi", ["builtins.str", "builtins.object"]],
        [str, ["builtins.str", "builtins.object"]],
        [Foo(1), [f"{__name__}.Foo", "builtins.object"]],
        [Foo, [f"{__name__}.Foo", "builtins.object"]],
        [FooSubclass(1),
         [f"{__name__}.FooSubclass", f"{__name__}.Foo", "builtins.object"]],
        [FooSubclass,
         [f"{__name__}.FooSubclass", f"{__name__}.Foo", "builtins.object"]],
        # fmt: on
    ),
)
def test_object_types_mro_as_strings(obj: object, expected_result: Sequence[str]):
    assert get_object_types_mro_as_strings(obj) == expected_result


@pytest.mark.parametrize(
    "obj,types,expected_result",
    (
        # fmt: off
        ["hi", ["builtins.str"], True],
        [str, ["builtins.str"], True],
        ["hi", ["builtins.str", "foo"], True],
        [str, ["builtins.str", "foo"], True],
        [Foo(1), [f"{__name__}.Foo"], True],
        [Foo, [f"{__name__}.Foo"], True],
        [Foo(1), ["builtins.str", f"{__name__}.Foo"], True],
        [Foo, ["builtins.int", f"{__name__}.Foo"], True],
        [Foo(1), [f"{__name__}.FooSubclass"], False],
        [Foo, [f"{__name__}.FooSubclass"], False],
        [Foo(1), [f"{__name__}.FooSubclass", f"{__name__}.Foo"], True],
        [Foo, [f"{__name__}.Foo", f"{__name__}.FooSubclass"], True],
        # fmt: on
    ),
)
def test_object_is_one_of_types(
    obj: object, types: Sequence[str], expected_result: bool
):
    assert is_object_one_of_types(obj, types) is expected_result