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

Fix behaviour of LazyDictWithCache when import fails #32248

Merged
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
3 changes: 1 addition & 2 deletions airflow/providers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ def __getitem__(self, key):
# callable itself
value = value()
self._resolved.add(key)
if value:
self._raw_dict.__setitem__(key, value)
self._raw_dict.__setitem__(key, value)
return value

def __delitem__(self, key):
Expand Down
31 changes: 30 additions & 1 deletion tests/always/test_providers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from wtforms import BooleanField, Field, StringField

from airflow.exceptions import AirflowOptionalProviderFeatureException
from airflow.providers_manager import HookClassProvider, ProviderInfo, ProvidersManager
from airflow.providers_manager import HookClassProvider, LazyDictWithCache, ProviderInfo, ProvidersManager


class TestProviderManager:
Expand Down Expand Up @@ -373,3 +373,32 @@ def test_optional_feature_debug(self, mock_importlib_import_string):
assert [
"Optional provider feature disabled when importing 'HookClass' from 'test_package' package"
] == self._caplog.messages


@pytest.mark.parametrize(
"value, expected_outputs,",
[
("a", "a"),
(1, 1),
(None, None),
(lambda: 0, 0),
(lambda: None, None),
(lambda: "z", "z"),
],
)
def test_lazy_cache_dict_resolving(value, expected_outputs):
lazy_cache_dict = LazyDictWithCache()
lazy_cache_dict["key"] = value
assert lazy_cache_dict["key"] == expected_outputs
# Retrieve it again to see if it is correctly returned again
assert lazy_cache_dict["key"] == expected_outputs


def test_lazy_cache_dict_raises_error():
def raise_method():
raise Exception("test")

lazy_cache_dict = LazyDictWithCache()
lazy_cache_dict["key"] = raise_method
with pytest.raises(Exception, match="test"):
_ = lazy_cache_dict["key"]