|
18 | 18 |
|
19 | 19 | import datetime
|
20 | 20 | import enum
|
| 21 | +import textwrap |
21 | 22 | from collections import namedtuple
|
22 | 23 | from dataclasses import dataclass
|
23 |
| -from importlib import import_module |
| 24 | +from importlib import import_module, metadata |
24 | 25 | from typing import ClassVar
|
25 | 26 |
|
26 | 27 | import attr
|
27 | 28 | import pytest
|
| 29 | +from packaging import version |
28 | 30 | from pydantic import BaseModel
|
29 | 31 |
|
30 | 32 | from airflow.sdk.definitions.asset import Asset
|
@@ -61,6 +63,67 @@ def recalculate_patterns():
|
61 | 63 | _match_regexp.cache_clear()
|
62 | 64 |
|
63 | 65 |
|
| 66 | +def generate_serializers_importable_tests(): |
| 67 | + """ |
| 68 | + Generate test cases for `test_serializers_importable_and_str`. |
| 69 | +
|
| 70 | + The function iterates through all the modules defined under `airflow.serialization.serializers`. It loads |
| 71 | + the import strings defined in the `serializers` from each module, and create a test case to verify that the |
| 72 | + serializer is importable. |
| 73 | + """ |
| 74 | + import airflow.serialization.serializers |
| 75 | + |
| 76 | + NUMPY_VERSION = version.parse(metadata.version("numpy")) |
| 77 | + |
| 78 | + serializer_tests = [] |
| 79 | + |
| 80 | + for _, name, _ in iter_namespace(airflow.serialization.serializers): |
| 81 | + ############################################################ |
| 82 | + # Handle compatibility / optional dependency at module level |
| 83 | + ############################################################ |
| 84 | + # https://github.com/apache/airflow/pull/37320 |
| 85 | + if name == "airflow.serialization.serializers.iceberg": |
| 86 | + try: |
| 87 | + import pyiceberg # noqa: F401 |
| 88 | + except ImportError: |
| 89 | + continue |
| 90 | + # https://github.com/apache/airflow/pull/38074 |
| 91 | + if name == "airflow.serialization.serializers.deltalake": |
| 92 | + try: |
| 93 | + import deltalake # noqa: F401 |
| 94 | + except ImportError: |
| 95 | + continue |
| 96 | + mod = import_module(name) |
| 97 | + for s in getattr(mod, "serializers", list()): |
| 98 | + ############################################################ |
| 99 | + # Handle compatibility issue at serializer level |
| 100 | + ############################################################ |
| 101 | + if s == "numpy.bool" and NUMPY_VERSION.major < 2: |
| 102 | + reason = textwrap.dedent(f"""\ |
| 103 | + Current NumPy version: {NUMPY_VERSION} |
| 104 | +
|
| 105 | + In NumPy 1.20, `numpy.bool` was deprecated as an alias for the built-in `bool`. |
| 106 | + For NumPy versions <= 1.26, attempting to import `numpy.bool` raises an ImportError. |
| 107 | + Starting with NumPy 2.0, `numpy.bool` is reintroduced as the NumPy scalar type, |
| 108 | + and `numpy.bool_` becomes an alias for `numpy.bool`. |
| 109 | +
|
| 110 | + The serializers are loaded lazily at runtime. As a result: |
| 111 | + - With NumPy <= 1.26, only `numpy.bool_` is loaded. |
| 112 | + - With NumPy >= 2.0, only `numpy.bool` is loaded. |
| 113 | +
|
| 114 | + This test case deliberately attempts to import both `numpy.bool` and `numpy.bool_`, |
| 115 | + regardless of the installed NumPy version. Therefore, when NumPy <= 1.26 is installed, |
| 116 | + importing `numpy.bool` will raise an ImportError. |
| 117 | + """) |
| 118 | + serializer_tests.append(pytest.param(name, s, marks=pytest.mark.skip(reason=reason))) |
| 119 | + else: |
| 120 | + serializer_tests.append(pytest.param(name, s)) |
| 121 | + return serializer_tests |
| 122 | + |
| 123 | + |
| 124 | +SERIALIZER_TESTS = generate_serializers_importable_tests() |
| 125 | + |
| 126 | + |
64 | 127 | class Z:
|
65 | 128 | __version__: ClassVar[int] = 1
|
66 | 129 |
|
@@ -386,29 +449,15 @@ def test_encode_asset(self):
|
386 | 449 | obj = deserialize(serialize(asset))
|
387 | 450 | assert asset.uri == obj.uri
|
388 | 451 |
|
389 |
| - def test_serializers_importable_and_str(self): |
| 452 | + @pytest.mark.parametrize("name, s", SERIALIZER_TESTS) |
| 453 | + def test_serializers_importable_and_str(self, name, s): |
390 | 454 | """Test if all distributed serializers are lazy loading and can be imported"""
|
391 |
| - import airflow.serialization.serializers |
392 |
| - |
393 |
| - for _, name, _ in iter_namespace(airflow.serialization.serializers): |
394 |
| - if name == "airflow.serialization.serializers.iceberg": |
395 |
| - try: |
396 |
| - import pyiceberg # noqa: F401 |
397 |
| - except ImportError: |
398 |
| - continue |
399 |
| - if name == "airflow.serialization.serializers.deltalake": |
400 |
| - try: |
401 |
| - import deltalake # noqa: F401 |
402 |
| - except ImportError: |
403 |
| - continue |
404 |
| - mod = import_module(name) |
405 |
| - for s in getattr(mod, "serializers", list()): |
406 |
| - if not isinstance(s, str): |
407 |
| - raise TypeError(f"{s} is not of type str. This is required for lazy loading") |
408 |
| - try: |
409 |
| - import_string(s) |
410 |
| - except ImportError: |
411 |
| - raise AttributeError(f"{s} cannot be imported (located in {name})") |
| 455 | + if not isinstance(s, str): |
| 456 | + raise TypeError(f"{s} is not of type str. This is required for lazy loading") |
| 457 | + try: |
| 458 | + import_string(s) |
| 459 | + except ImportError: |
| 460 | + raise AttributeError(f"{s} cannot be imported (located in {name})") |
412 | 461 |
|
413 | 462 | def test_stringify(self):
|
414 | 463 | i = V(W(10), ["l1", "l2"], (1, 2), 10)
|
|
0 commit comments