From 1bd49e59a5e389dcf3615dd599f182e7eb135e9e Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 01:04:30 +0200 Subject: [PATCH 1/2] moved dtype constants to pandas._testing to avoid direct imports from conftest.py --- pandas/_testing.py | 33 ++++++++++++- pandas/conftest.py | 60 +++++------------------ pandas/tests/dtypes/test_common.py | 48 ++++++++---------- pandas/tests/indexing/test_categorical.py | 7 ++- pandas/tests/test_algos.py | 3 +- 5 files changed, 69 insertions(+), 82 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 1f6b645c821c8..96527913ad00f 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -22,7 +22,7 @@ ) import pandas._libs.testing as _testing -from pandas._typing import FilePathOrBuffer, FrameOrSeries +from pandas._typing import Dtype, FilePathOrBuffer, FrameOrSeries from pandas.compat import _get_lzma_file, _import_lzma from pandas.core.dtypes.common import ( @@ -73,6 +73,37 @@ _K = 4 _RAISE_NETWORK_ERROR_DEFAULT = False +UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"] +UNSIGNED_EA_INT_DTYPES = ["UInt8", "UInt16", "UInt32", "UInt64"] +SIGNED_INT_DTYPES: List[Dtype] = [int, "int8", "int16", "int32", "int64"] +SIGNED_EA_INT_DTYPES = ["Int8", "Int16", "Int32", "Int64"] +ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES +ALL_EA_INT_DTYPES = UNSIGNED_EA_INT_DTYPES + SIGNED_EA_INT_DTYPES + +FLOAT_DTYPES: List[Dtype] = [float, "float32", "float64"] +COMPLEX_DTYPES: List[Dtype] = [complex, "complex64", "complex128"] +STRING_DTYPES: List[Dtype] = [str, "str", "U"] + +DATETIME64_DTYPES = ["datetime64[ns]", "M8[ns]"] +TIMEDELTA64_DTYPES = ["timedelta64[ns]", "m8[ns]"] + +BOOL_DTYPES = [bool, "bool"] +BYTES_DTYPES = [bytes, "bytes"] +OBJECT_DTYPES = [object, "object"] + +ALL_REAL_DTYPES = FLOAT_DTYPES + ALL_INT_DTYPES +ALL_NUMPY_DTYPES = ( + ALL_REAL_DTYPES + + COMPLEX_DTYPES + + STRING_DTYPES + + DATETIME64_DTYPES + + TIMEDELTA64_DTYPES + + BOOL_DTYPES + + OBJECT_DTYPES + + BYTES_DTYPES +) + + # set testing_mode _testing_mode_warnings = (DeprecationWarning, ResourceWarning) diff --git a/pandas/conftest.py b/pandas/conftest.py index e1088dae3925a..70be6b5d9fcbc 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -23,7 +23,6 @@ from decimal import Decimal import operator import os -from typing import List from dateutil.tz import tzlocal, tzutc import hypothesis @@ -32,7 +31,6 @@ import pytest from pytz import FixedOffset, utc -from pandas._typing import Dtype import pandas.util._test_decorators as td import pandas as pd @@ -864,39 +862,7 @@ def utc_fixture(request): # ---------------------------------------------------------------- # Dtypes # ---------------------------------------------------------------- - -UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"] -UNSIGNED_EA_INT_DTYPES = ["UInt8", "UInt16", "UInt32", "UInt64"] -SIGNED_INT_DTYPES: List[Dtype] = [int, "int8", "int16", "int32", "int64"] -SIGNED_EA_INT_DTYPES = ["Int8", "Int16", "Int32", "Int64"] -ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES -ALL_EA_INT_DTYPES = UNSIGNED_EA_INT_DTYPES + SIGNED_EA_INT_DTYPES - -FLOAT_DTYPES: List[Dtype] = [float, "float32", "float64"] -COMPLEX_DTYPES: List[Dtype] = [complex, "complex64", "complex128"] -STRING_DTYPES: List[Dtype] = [str, "str", "U"] - -DATETIME64_DTYPES = ["datetime64[ns]", "M8[ns]"] -TIMEDELTA64_DTYPES = ["timedelta64[ns]", "m8[ns]"] - -BOOL_DTYPES = [bool, "bool"] -BYTES_DTYPES = [bytes, "bytes"] -OBJECT_DTYPES = [object, "object"] - -ALL_REAL_DTYPES = FLOAT_DTYPES + ALL_INT_DTYPES -ALL_NUMPY_DTYPES = ( - ALL_REAL_DTYPES - + COMPLEX_DTYPES - + STRING_DTYPES - + DATETIME64_DTYPES - + TIMEDELTA64_DTYPES - + BOOL_DTYPES - + OBJECT_DTYPES - + BYTES_DTYPES -) - - -@pytest.fixture(params=STRING_DTYPES) +@pytest.fixture(params=tm.STRING_DTYPES) def string_dtype(request): """ Parametrized fixture for string dtypes. @@ -908,7 +874,7 @@ def string_dtype(request): return request.param -@pytest.fixture(params=BYTES_DTYPES) +@pytest.fixture(params=tm.BYTES_DTYPES) def bytes_dtype(request): """ Parametrized fixture for bytes dtypes. @@ -919,7 +885,7 @@ def bytes_dtype(request): return request.param -@pytest.fixture(params=OBJECT_DTYPES) +@pytest.fixture(params=tm.OBJECT_DTYPES) def object_dtype(request): """ Parametrized fixture for object dtypes. @@ -930,7 +896,7 @@ def object_dtype(request): return request.param -@pytest.fixture(params=DATETIME64_DTYPES) +@pytest.fixture(params=tm.DATETIME64_DTYPES) def datetime64_dtype(request): """ Parametrized fixture for datetime64 dtypes. @@ -941,7 +907,7 @@ def datetime64_dtype(request): return request.param -@pytest.fixture(params=TIMEDELTA64_DTYPES) +@pytest.fixture(params=tm.TIMEDELTA64_DTYPES) def timedelta64_dtype(request): """ Parametrized fixture for timedelta64 dtypes. @@ -952,7 +918,7 @@ def timedelta64_dtype(request): return request.param -@pytest.fixture(params=FLOAT_DTYPES) +@pytest.fixture(params=tm.FLOAT_DTYPES) def float_dtype(request): """ Parameterized fixture for float dtypes. @@ -964,7 +930,7 @@ def float_dtype(request): return request.param -@pytest.fixture(params=COMPLEX_DTYPES) +@pytest.fixture(params=tm.COMPLEX_DTYPES) def complex_dtype(request): """ Parameterized fixture for complex dtypes. @@ -976,7 +942,7 @@ def complex_dtype(request): return request.param -@pytest.fixture(params=SIGNED_INT_DTYPES) +@pytest.fixture(params=tm.SIGNED_INT_DTYPES) def sint_dtype(request): """ Parameterized fixture for signed integer dtypes. @@ -990,7 +956,7 @@ def sint_dtype(request): return request.param -@pytest.fixture(params=UNSIGNED_INT_DTYPES) +@pytest.fixture(params=tm.UNSIGNED_INT_DTYPES) def uint_dtype(request): """ Parameterized fixture for unsigned integer dtypes. @@ -1003,7 +969,7 @@ def uint_dtype(request): return request.param -@pytest.fixture(params=ALL_INT_DTYPES) +@pytest.fixture(params=tm.ALL_INT_DTYPES) def any_int_dtype(request): """ Parameterized fixture for any integer dtype. @@ -1021,7 +987,7 @@ def any_int_dtype(request): return request.param -@pytest.fixture(params=ALL_EA_INT_DTYPES) +@pytest.fixture(params=tm.ALL_EA_INT_DTYPES) def any_nullable_int_dtype(request): """ Parameterized fixture for any nullable integer dtype. @@ -1038,7 +1004,7 @@ def any_nullable_int_dtype(request): return request.param -@pytest.fixture(params=ALL_REAL_DTYPES) +@pytest.fixture(params=tm.ALL_REAL_DTYPES) def any_real_dtype(request): """ Parameterized fixture for any (purely) real numeric dtype. @@ -1059,7 +1025,7 @@ def any_real_dtype(request): return request.param -@pytest.fixture(params=ALL_NUMPY_DTYPES) +@pytest.fixture(params=tm.ALL_NUMPY_DTYPES) def any_numpy_dtype(request): """ Parameterized fixture for all numpy dtypes. diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 6e73e1542bb80..b9c8f3a8dd494 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -20,14 +20,6 @@ import pandas as pd import pandas._testing as tm from pandas.arrays import SparseArray -from pandas.conftest import ( - ALL_EA_INT_DTYPES, - ALL_INT_DTYPES, - SIGNED_EA_INT_DTYPES, - SIGNED_INT_DTYPES, - UNSIGNED_EA_INT_DTYPES, - UNSIGNED_INT_DTYPES, -) # EA & Actual Dtypes @@ -295,10 +287,10 @@ def test_is_string_dtype(): "dtype", integer_dtypes + [pd.Series([1, 2])] - + ALL_INT_DTYPES - + to_numpy_dtypes(ALL_INT_DTYPES) - + ALL_EA_INT_DTYPES - + to_ea_dtypes(ALL_EA_INT_DTYPES), + + tm.ALL_INT_DTYPES + + to_numpy_dtypes(tm.ALL_INT_DTYPES) + + tm.ALL_EA_INT_DTYPES + + to_ea_dtypes(tm.ALL_EA_INT_DTYPES), ) def test_is_integer_dtype(dtype): assert com.is_integer_dtype(dtype) @@ -327,10 +319,10 @@ def test_is_not_integer_dtype(dtype): "dtype", signed_integer_dtypes + [pd.Series([1, 2])] - + SIGNED_INT_DTYPES - + to_numpy_dtypes(SIGNED_INT_DTYPES) - + SIGNED_EA_INT_DTYPES - + to_ea_dtypes(SIGNED_EA_INT_DTYPES), + + tm.SIGNED_INT_DTYPES + + to_numpy_dtypes(tm.SIGNED_INT_DTYPES) + + tm.SIGNED_EA_INT_DTYPES + + to_ea_dtypes(tm.SIGNED_EA_INT_DTYPES), ) def test_is_signed_integer_dtype(dtype): assert com.is_integer_dtype(dtype) @@ -347,10 +339,10 @@ def test_is_signed_integer_dtype(dtype): np.array(["a", "b"]), np.array([], dtype=np.timedelta64), ] - + UNSIGNED_INT_DTYPES - + to_numpy_dtypes(UNSIGNED_INT_DTYPES) - + UNSIGNED_EA_INT_DTYPES - + to_ea_dtypes(UNSIGNED_EA_INT_DTYPES), + + tm.UNSIGNED_INT_DTYPES + + to_numpy_dtypes(tm.UNSIGNED_INT_DTYPES) + + tm.UNSIGNED_EA_INT_DTYPES + + to_ea_dtypes(tm.UNSIGNED_EA_INT_DTYPES), ) def test_is_not_signed_integer_dtype(dtype): assert not com.is_signed_integer_dtype(dtype) @@ -363,10 +355,10 @@ def test_is_not_signed_integer_dtype(dtype): "dtype", unsigned_integer_dtypes + [pd.Series([1, 2], dtype=np.uint32)] - + UNSIGNED_INT_DTYPES - + to_numpy_dtypes(UNSIGNED_INT_DTYPES) - + UNSIGNED_EA_INT_DTYPES - + to_ea_dtypes(UNSIGNED_EA_INT_DTYPES), + + tm.UNSIGNED_INT_DTYPES + + to_numpy_dtypes(tm.UNSIGNED_INT_DTYPES) + + tm.UNSIGNED_EA_INT_DTYPES + + to_ea_dtypes(tm.UNSIGNED_EA_INT_DTYPES), ) def test_is_unsigned_integer_dtype(dtype): assert com.is_unsigned_integer_dtype(dtype) @@ -383,10 +375,10 @@ def test_is_unsigned_integer_dtype(dtype): np.array(["a", "b"]), np.array([], dtype=np.timedelta64), ] - + SIGNED_INT_DTYPES - + to_numpy_dtypes(SIGNED_INT_DTYPES) - + SIGNED_EA_INT_DTYPES - + to_ea_dtypes(SIGNED_EA_INT_DTYPES), + + tm.SIGNED_INT_DTYPES + + to_numpy_dtypes(tm.SIGNED_INT_DTYPES) + + tm.SIGNED_EA_INT_DTYPES + + to_ea_dtypes(tm.SIGNED_EA_INT_DTYPES), ) def test_is_not_unsigned_integer_dtype(dtype): assert not com.is_unsigned_integer_dtype(dtype) diff --git a/pandas/tests/indexing/test_categorical.py b/pandas/tests/indexing/test_categorical.py index 829ee61197ff2..c9634c4c90809 100644 --- a/pandas/tests/indexing/test_categorical.py +++ b/pandas/tests/indexing/test_categorical.py @@ -14,7 +14,6 @@ Series, Timedelta, Timestamp, - conftest, ) import pandas._testing as tm from pandas.api.types import CategoricalDtype as CDT @@ -752,9 +751,9 @@ def test_map_with_dict_or_series(self): [1.5, 2.5, 3.5], [-1.5, -2.5, -3.5], # numpy int/uint - *[np.array([1, 2, 3], dtype=dtype) for dtype in conftest.ALL_INT_DTYPES], + *[np.array([1, 2, 3], dtype=dtype) for dtype in tm.ALL_INT_DTYPES], # numpy floats - *[np.array([1.5, 2.5, 3.5], dtype=dtyp) for dtyp in conftest.FLOAT_DTYPES], + *[np.array([1.5, 2.5, 3.5], dtype=dtyp) for dtyp in tm.FLOAT_DTYPES], # numpy object np.array([1, "b", 3.5], dtype=object), # pandas scalars @@ -762,7 +761,7 @@ def test_map_with_dict_or_series(self): [Timestamp(2019, 1, 1), Timestamp(2019, 2, 1), Timestamp(2019, 3, 1)], [Timedelta(1, "d"), Timedelta(2, "d"), Timedelta(3, "D")], # pandas Integer arrays - *[pd.array([1, 2, 3], dtype=dtype) for dtype in conftest.ALL_EA_INT_DTYPES], + *[pd.array([1, 2, 3], dtype=dtype) for dtype in tm.ALL_EA_INT_DTYPES], # other pandas arrays pd.IntervalIndex.from_breaks([1, 4, 6, 9]).array, pd.date_range("2019-01-01", periods=3).array, diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index ad7028702ec8c..5f904241da485 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -31,7 +31,6 @@ compat, ) import pandas._testing as tm -from pandas.conftest import BYTES_DTYPES, STRING_DTYPES import pandas.core.algorithms as algos from pandas.core.arrays import DatetimeArray import pandas.core.common as com @@ -362,7 +361,7 @@ def test_on_index_object(self): def test_dtype_preservation(self, any_numpy_dtype): # GH 15442 - if any_numpy_dtype in (BYTES_DTYPES + STRING_DTYPES): + if any_numpy_dtype in (tm.BYTES_DTYPES + tm.STRING_DTYPES): pytest.skip("skip string dtype") elif is_integer_dtype(any_numpy_dtype): data = [1, 2, 2] From fc8f84ec8d5accb69340b60cd4a31200a7c28782 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 01:35:26 +0200 Subject: [PATCH 2/2] resolved typing conflicts --- pandas/_testing.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 96527913ad00f..4f957b7a55e3a 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -73,10 +73,10 @@ _K = 4 _RAISE_NETWORK_ERROR_DEFAULT = False -UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"] -UNSIGNED_EA_INT_DTYPES = ["UInt8", "UInt16", "UInt32", "UInt64"] +UNSIGNED_INT_DTYPES: List[Dtype] = ["uint8", "uint16", "uint32", "uint64"] +UNSIGNED_EA_INT_DTYPES: List[Dtype] = ["UInt8", "UInt16", "UInt32", "UInt64"] SIGNED_INT_DTYPES: List[Dtype] = [int, "int8", "int16", "int32", "int64"] -SIGNED_EA_INT_DTYPES = ["Int8", "Int16", "Int32", "Int64"] +SIGNED_EA_INT_DTYPES: List[Dtype] = ["Int8", "Int16", "Int32", "Int64"] ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES ALL_EA_INT_DTYPES = UNSIGNED_EA_INT_DTYPES + SIGNED_EA_INT_DTYPES @@ -84,8 +84,8 @@ COMPLEX_DTYPES: List[Dtype] = [complex, "complex64", "complex128"] STRING_DTYPES: List[Dtype] = [str, "str", "U"] -DATETIME64_DTYPES = ["datetime64[ns]", "M8[ns]"] -TIMEDELTA64_DTYPES = ["timedelta64[ns]", "m8[ns]"] +DATETIME64_DTYPES: List[Dtype] = ["datetime64[ns]", "M8[ns]"] +TIMEDELTA64_DTYPES: List[Dtype] = ["timedelta64[ns]", "m8[ns]"] BOOL_DTYPES = [bool, "bool"] BYTES_DTYPES = [bytes, "bytes"]