From 8ceeb83fd499a9580daca440a868024b1d2da689 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 27 Jan 2022 12:36:07 +0000 Subject: [PATCH] Update changelog, add test for inspecting modules containing classes --- CHANGELOG.md | 2 +- tests/test_inspect.py | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb80f1da..613fca7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Workaround for edge case of object from Faiss with no `__class__` https://github.com/Textualize/rich/issues/1838 - Add `Syntax.guess_lexer`, add support for more lexers (e.g. Django templates etc.) https://github.com/Textualize/rich/pull/1869 - Ensure `Syntax` always justifies left https://github.com/Textualize/rich/pull/1872 - +- Handle classes in inspect when methods=True https://github.com/Textualize/rich/pull/1874 ### Added diff --git a/tests/test_inspect.py b/tests/test_inspect.py index 05a78b87..63c5f062 100644 --- a/tests/test_inspect.py +++ b/tests/test_inspect.py @@ -1,5 +1,6 @@ import io import sys +from types import ModuleType import pytest @@ -81,7 +82,6 @@ def test_render(): def test_inspect_text(): - expected = ( "╭──────────────── ─────────────────╮\n" "│ str(object='') -> str │\n" @@ -99,7 +99,6 @@ def test_inspect_text(): @skip_py36 @skip_py37 def test_inspect_empty_dict(): - expected = ( "╭──────────────── ────────────────╮\n" "│ dict() -> new empty dictionary │\n" @@ -121,7 +120,6 @@ def test_inspect_empty_dict(): def test_inspect_builtin_function(): - expected = ( "╭────────── ───────────╮\n" "│ def print(...) │\n" @@ -138,7 +136,6 @@ def test_inspect_builtin_function(): @skip_py36 def test_inspect_integer(): - expected = ( "╭────── ───────╮\n" "│ int([x]) -> integer │\n" @@ -155,7 +152,6 @@ def test_inspect_integer(): @skip_py36 def test_inspect_integer_with_value(): - expected = "╭────── ───────╮\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)) @@ -166,7 +162,6 @@ def test_inspect_integer_with_value(): @skip_py37 @skip_py310 def test_inspect_integer_with_methods(): - expected = ( "╭──────────────── ─────────────────╮\n" "│ int([x]) -> integer │\n" @@ -204,7 +199,6 @@ def test_inspect_integer_with_methods(): @skip_py38 @skip_py39 def test_inspect_integer_with_methods(): - expected = ( "╭──────────────── ─────────────────╮\n" "│ int([x]) -> integer │\n" @@ -274,3 +268,25 @@ def test_inspect_swig_edge_case(): 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 = ( + "╭────────── ──────────╮\n" + "│ function = def function(): │\n" + "│ SomeClass = class SomeClass(): Docstring │\n" + "╰──────────────────────────────────────────╯\n" + ) + assert render(module, methods=True) == expected