From 87e114224240f7f6026a19551588b568fc656d7f Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 2 Oct 2024 17:31:43 +0100 Subject: [PATCH 1/3] raise AttributeError --- src/textual/widgets/__init__.py | 2 +- tests/test_widget.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/textual/widgets/__init__.py b/src/textual/widgets/__init__.py index 7036ca44f8..bcaac621c0 100644 --- a/src/textual/widgets/__init__.py +++ b/src/textual/widgets/__init__.py @@ -104,7 +104,7 @@ def __getattr__(widget_class: str) -> type[Widget]: pass if widget_class not in __all__: - raise ImportError(f"Package 'textual.widgets' has no class '{widget_class}'") + raise AttributeError(f"Package 'textual.widgets' has no class '{widget_class}'") widget_module_path = f"._{camel_to_snake(widget_class)}" module = import_module(widget_module_path, package="textual.widgets") diff --git a/tests/test_widget.py b/tests/test_widget.py index d5e79c0c11..4d5133aaee 100644 --- a/tests/test_widget.py +++ b/tests/test_widget.py @@ -576,3 +576,11 @@ def test_bad_widget_name_raised() -> None: class lowercaseWidget(Widget): pass + + +def test_lazy_loading() -> None: + from textual import widgets + + assert not hasattr(widgets, "foo") + assert not hasattr(widgets, "bar") + assert hasattr(widgets, "Label") From 91747dea562b052caa4dc257ef0452410431f6c5 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 2 Oct 2024 17:37:23 +0100 Subject: [PATCH 2/3] tests --- tests/test_widget.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_widget.py b/tests/test_widget.py index 4d5133aaee..a12a90befe 100644 --- a/tests/test_widget.py +++ b/tests/test_widget.py @@ -579,7 +579,17 @@ class lowercaseWidget(Widget): def test_lazy_loading() -> None: + """Regression test for https://github.com/Textualize/textual/issues/5077 + + Check that the lazy loading magic doesn't break attribute access. + + """ + + with pytest.raises(ImportError): + pass + from textual import widgets + from textual.widgets import Label assert not hasattr(widgets, "foo") assert not hasattr(widgets, "bar") From bf16ded909aaeba01fc80868a8e1795bf9f79528 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 2 Oct 2024 17:39:26 +0100 Subject: [PATCH 3/3] pycln comment --- tests/test_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_widget.py b/tests/test_widget.py index a12a90befe..22e784b3b2 100644 --- a/tests/test_widget.py +++ b/tests/test_widget.py @@ -586,7 +586,7 @@ def test_lazy_loading() -> None: """ with pytest.raises(ImportError): - pass + from textual.widgets import Foo # nopycln: import from textual import widgets from textual.widgets import Label