Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise AttributeError #5080

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/textual/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,21 @@ def test_bad_widget_name_raised() -> None:

class lowercaseWidget(Widget):
pass


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):
from textual.widgets import Foo # nopycln: import

from textual import widgets
from textual.widgets import Label

assert not hasattr(widgets, "foo")
assert not hasattr(widgets, "bar")
assert hasattr(widgets, "Label")
Loading