From d9d71039902759e5dd6aced6db90166c80818dc1 Mon Sep 17 00:00:00 2001 From: Jarek Potiuk Date: Mon, 23 Jun 2025 10:51:15 +0200 Subject: [PATCH] Fix compatibility with new numpy and pandas After #52060 some tests started to fail - this PR fixes it: * test_pandas.py is removed, it makes no sense as we seem to be testing if pandas has specific API (which we neither use nor guarantee) * numpy serializers are now compatible with numpy 2 --- airflow-core/tests/unit/always/test_pandas.py | 59 ------------------- .../serializers/test_serializers.py | 10 +++- 2 files changed, 8 insertions(+), 61 deletions(-) delete mode 100644 airflow-core/tests/unit/always/test_pandas.py diff --git a/airflow-core/tests/unit/always/test_pandas.py b/airflow-core/tests/unit/always/test_pandas.py deleted file mode 100644 index 5e474392dd8aa..0000000000000 --- a/airflow-core/tests/unit/always/test_pandas.py +++ /dev/null @@ -1,59 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -from __future__ import annotations - -import pytest -from sqlalchemy import create_engine - -from airflow.configuration import conf - -pd = pytest.importorskip("pandas") -pyarrow = pytest.importorskip("pyarrow") - -pytestmark = pytest.mark.db_test - - -@pytest.mark.backend("postgres", "mysql") -class TestPandasSQLAlchemyCompatibility: - @pytest.fixture(autouse=True) - def _setup_test_cases(self): - self.temp_table = "test_to_pandas" - uri = conf.get_mandatory_value("database", "sql_alchemy_conn") - self.engine = create_engine(uri) - yield - if self.engine: - with self.engine.begin() as conn: - conn.execute(f"DROP TABLE IF EXISTS {self.temp_table};") - self.engine.dispose() - - def test_write_read_to_db(self): - pd.DataFrame({"a": [0, 1, 2, 3]}).to_sql(self.temp_table, self.engine) - pd.read_sql(f"SELECT * FROM {self.temp_table}", self.engine) - - -class TestPandasSQLAlchemyCompatibilitySQLite: - @pytest.fixture(autouse=True) - def _setup_test_cases(self): - self.temp_table = "test_to_pandas" - self.engine = create_engine("sqlite:///:memory:") - yield - if self.engine: - self.engine.dispose() - - def test_write_read_to_db(self): - pd.DataFrame({"a": [0, 1, 2, 3]}).to_sql(self.temp_table, self.engine) - pd.read_sql(f"SELECT * FROM {self.temp_table}", self.engine) diff --git a/airflow-core/tests/unit/serialization/serializers/test_serializers.py b/airflow-core/tests/unit/serialization/serializers/test_serializers.py index 6cc6505d48b0a..8aa6254816944 100644 --- a/airflow-core/tests/unit/serialization/serializers/test_serializers.py +++ b/airflow-core/tests/unit/serialization/serializers/test_serializers.py @@ -222,8 +222,14 @@ def test_numpy(self): def test_numpy_serializers(self): from airflow.serialization.serializers.numpy import serialize - assert serialize(np.bool_(False)) == (True, "numpy.bool_", 1, True) - assert serialize(np.float32(3.14)) == (float(np.float32(3.14)), "numpy.float32", 1, True) + numpy_version = metadata.version("numpy") + is_numpy_2 = version.parse(numpy_version).major == 2 + + assert serialize(np.bool_(False)) == (True, "numpy.bool" if is_numpy_2 else "numpy.bool_", 1, True) + if is_numpy_2: + assert serialize(np.float64(3.14)) == (float(np.float64(3.14)), "numpy.float64", 1, True) + else: + assert serialize(np.float32(3.14)) == (float(np.float32(3.14)), "numpy.float32", 1, True) assert serialize(np.array([1, 2, 3])) == ("", "", 0, False) @pytest.mark.parametrize(