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

Move away from legacy importlib.resources API #19091

Merged
merged 2 commits into from
Oct 21, 2021
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
20 changes: 6 additions & 14 deletions airflow/providers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,12 @@
from airflow.utils.log.logging_mixin import LoggingMixin
from airflow.utils.module_loading import import_string

try:
import importlib.resources as importlib_resources
except ImportError:
# Try back-ported to PY<37 `importlib_resources`.
import importlib_resources

log = logging.getLogger(__name__)

if sys.version_info >= (3, 9):
from functools import cache
from importlib.resources import files as resource_files
else:
from functools import lru_cache

cache = lru_cache(maxsize=None)
from importlib_resources import files as resource_files

MIN_PROVIDER_VERSIONS = {
"apache-airflow-providers-celery": "2.1.0",
Expand Down Expand Up @@ -102,17 +94,17 @@ def __contains__(self, key):

def _create_provider_info_schema_validator():
"""Creates JSON schema validator from the provider_info.schema.json"""
schema = json.loads(importlib_resources.read_text('airflow', 'provider_info.schema.json'))
with resource_files("airflow").joinpath("provider_info.schema.json").open("rb") as f:
schema = json.load(f)
cls = jsonschema.validators.validator_for(schema)
validator = cls(schema)
return validator


def _create_customized_form_field_behaviours_schema_validator():
"""Creates JSON schema validator from the customized_form_field_behaviours.schema.json"""
schema = json.loads(
importlib_resources.read_text('airflow', 'customized_form_field_behaviours.schema.json')
)
with resource_files("airflow").joinpath("customized_form_field_behaviours.schema.json").open("rb") as f:
schema = json.load(f)
cls = jsonschema.validators.validator_for(schema)
validator = cls(schema)
return validator
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ install_requires =
gunicorn>=20.1.0
httpx
importlib_metadata>=1.7;python_version<"3.9"
importlib_resources~=5.2;python_version<"3.7"
importlib_resources~=5.2;python_version<"3.9"
# Required by vendored-in connexion
inflection>=0.3.1
iso8601>=0.1.12
Expand Down
19 changes: 9 additions & 10 deletions tests/core/test_providers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ def test_warning_logs_not_generated(self):

@patch('airflow.providers_manager.importlib.import_module')
def test_hooks(self, mock_import_module):
# importlib-resources>=5.2 relies on importing the containing module to
# read its content. The provider manager needs to read the validation
# schema 'provider_info.schema.json'. Not sure why this needs to be
# called twice. Also see comment on the assert at the end of the function.
# importlib.resources relies on importing the containing module to read
# its content. The provider manager needs to read two validation schemas
# 'provider_info.schema.json' and 'customized_form_field_behaviours.schema.json'.
mock_import_module.side_effect = [airflow, airflow]

with pytest.warns(expected_warning=None) as warning_records:
Expand All @@ -132,12 +131,12 @@ def test_hooks(self, mock_import_module):
assert [] == [w.message for w in warning_records.list if "hook-class-names" in str(w.message)]
assert len(self._caplog.records) == 0

# The stdlib importlib.resources implementation in 3.7 through 3.9 does
# not rely on importlib.import_module, so the function should not be
# called. The implementation for 3.10+ and the backport to 3.6 uses it
# to import the top-level 'airflow' for 'provider_info.schema.json'.
# Also see comment on mocking at the beginning of the function.
if (3, 7) <= sys.version_info < (3, 10):
# The stdlib importlib.resources implementation in 3.9 does not rely on
# importlib.import_module, so the function should not be called. The
# implementation for 3.10+ and the backport we use for up to 3.8 uses it
# to import the top-level 'airflow' for the two schema files. Also see
# comment on mocking at the beginning of the function.
if sys.version_info[:2] == (3, 9):
assert mock_import_module.mock_calls == []
else:
assert mock_import_module.mock_calls == [call("airflow"), call("airflow")]
Expand Down