Skip to content

Commit

Permalink
np.unicode_ removed in NumPy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed Aug 28, 2023
1 parent 029a509 commit b06ebd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
10 changes: 6 additions & 4 deletions astropy/io/votable/converters.py
Expand Up @@ -4,7 +4,6 @@
to/from TABLEDATA_ and BINARY_ formats.
"""


# STDLIB
import re
import sys
Expand All @@ -16,6 +15,7 @@
from numpy import ma

# ASTROPY
from astropy.utils.compat import NUMPY_LT_2_0
from astropy.utils.xml.writer import xml_escape_cdata

# LOCAL
Expand Down Expand Up @@ -1362,11 +1362,13 @@ def get_converter(field, config=None, pos=None):
np.int64().dtype.num: "long",
np.complex64().dtype.num: "floatComplex",
np.complex128().dtype.num: "doubleComplex",
np.unicode_().dtype.num: "unicodeChar",
np.bytes_().dtype.num: "char",
}


numpy_dtype_to_field_mapping[np.bytes_().dtype.num] = "char"
if NUMPY_LT_2_0:
numpy_dtype_to_field_mapping[np.unicode_().dtype.num] = "unicodeChar"
else:
numpy_dtype_to_field_mapping[np.str_().dtype.num] = "unicodeChar"

Check warning on line 1371 in astropy/io/votable/converters.py

View check run for this annotation

Codecov / codecov/patch

astropy/io/votable/converters.py#L1371

Added line #L1371 was not covered by tests


def _all_matching_dtype(column):
Expand Down
19 changes: 13 additions & 6 deletions astropy/io/votable/tests/test_vo.py
Expand Up @@ -11,18 +11,17 @@
import sys
from unittest import mock

import numpy as np

# THIRD-PARTY
import numpy as np
import pytest
from numpy.testing import assert_array_equal

# LOCAL
from astropy.io.votable import tree
from astropy.io.votable.exceptions import W39, VOTableSpecError, VOWarning

# LOCAL
from astropy.io.votable.table import parse, parse_single_table, validate
from astropy.io.votable.xmlutil import validate_schema
from astropy.utils.compat import NUMPY_LT_2_0
from astropy.utils.data import get_pkg_data_filename, get_pkg_data_filenames

# Determine the kind of float formatting in this build of Python
Expand Down Expand Up @@ -298,7 +297,11 @@ def test_string_test(self):
)

def test_fixed_string_test(self):
assert issubclass(self.array["string_test_2"].dtype.type, np.unicode_)
if NUMPY_LT_2_0:
klass = np.unicode_
else:
klass = np.str_
assert issubclass(self.array["string_test_2"].dtype.type, klass)
assert_array_equal(
self.array["string_test_2"], ["Fixed stri", "0123456789", "XXXX", "", ""]
)
Expand All @@ -311,7 +314,11 @@ def test_unicode_test(self):
)

def test_fixed_unicode_test(self):
assert issubclass(self.array["fixed_unicode_test"].dtype.type, np.unicode_)
if NUMPY_LT_2_0:
klass = np.unicode_
else:
klass = np.str_
assert issubclass(self.array["fixed_unicode_test"].dtype.type, klass)
assert_array_equal(
self.array["fixed_unicode_test"],
["Ceçi n'est", "வணக்கம்", "0123456789", "", ""],
Expand Down
7 changes: 6 additions & 1 deletion astropy/table/meta.py
Expand Up @@ -6,6 +6,8 @@
import numpy as np
import yaml

from astropy.utils.compat import NUMPY_LT_2_0

__all__ = ["get_header_from_yaml", "get_yaml_from_header", "get_yaml_from_table"]


Expand Down Expand Up @@ -193,7 +195,10 @@ class ConvertError(ValueError):
"""Local conversion error used below."""

# Numpy types supported as variable-length arrays
np_classes = (np.floating, np.integer, np.bool_, np.unicode_)
if NUMPY_LT_2_0:
np_classes = (np.floating, np.integer, np.bool_, np.unicode_)
else:
np_classes = (np.floating, np.integer, np.bool_)

Check warning on line 201 in astropy/table/meta.py

View check run for this annotation

Codecov / codecov/patch

astropy/table/meta.py#L201

Added line #L201 was not covered by tests

try:
if len(col) == 0 or not all(isinstance(val, np.ndarray) for val in col):
Expand Down

0 comments on commit b06ebd4

Please sign in to comment.