Skip to content

BUG: avoid deprecation warnings from numpy.char.chararray when checking types at runtime - #19217

Closed
neutrinoceros wants to merge 1 commit into
astropy:mainfrom
neutrinoceros:compat/is_chararray
Closed

neutrinoceros wants to merge 1 commit into
astropy:mainfrom
neutrinoceros:compat/is_chararray

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

ref #19216
fixes the dominant failure mode, and should be backportable

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

Comment thread astropy/io/fits/column.py
except Exception:
try: # then try to convert it to a strings array
itemsize = int(recformat[1:])
array = chararray.array(array, itemsize=itemsize)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is adjacent and not technically part of the fix, so I can revert this part if prefered. My reasoning is that eliminating chararray as a namespace alias removes noise when grepping for remaining occurences of this class



def is_chararray(obj, /) -> bool:
if not hasattr(np, "char"):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy dev's doc currently says that the entire module may go away in the future, even though it's not deprecated yet, so I'm using this guard clause for future-proofing

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

allowed_failures is expected to pass this time around even though I intentionally didn't fix all of the warnings (see #19216), because the deprecation hasn't landed in nightlies yet.

@neutrinoceros
neutrinoceros marked this pull request as ready for review January 26, 2026 09:35
@neutrinoceros
neutrinoceros requested a review from saimn as a code owner January 26, 2026 09:35
if field.dtype.char in ("S", "U") and not isinstance(field, chararray.chararray):
field = field.view(chararray.chararray)
if field.dtype.char in ("S", "U") and not is_chararray(field):
field = field.view(np.char.chararray)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that this single line is responsible for every remaining failures after this patch. I'm not sure whether it's okay to backport the necessary change, as it could be breaking. TBD (possibly in #19216)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we jail this line in a warnings.filterwarnings("ignore", category=DeprecationWarning) via context manager like you did in numpycompat ? That would get rid of the remaining warnings until we are ready for exorcise it for real?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong opinions from me. @saimn ?

@saimn

saimn commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

So it seems that currently only np.char.chararray is deprecated but np.char.array (which instantiates a np.char.chararray object) is not ? Hmm, we need to see what are Numpy's plans how the rest of np.char, since masking some warnings does not solve the underlying issue (we rely on that class).

@pllim

pllim commented Jan 26, 2026

Copy link
Copy Markdown
Member

Maybe @jorenham could advise?

@jorenham

Copy link
Copy Markdown
Contributor

The plan is to eventually get rid of numpy.char. Importing numpy.chararray directly has been deprecated for a long time now, but numpy.char.chararray (accidentally) wasn't.

So it seems that currently only np.char.chararray is deprecated but np.char.array (which instantiates a np.char.chararray object) is not ?

Good catch; I agree that we should also deprecate the charray constructor methods. I'll try and bring it up at the next community meeting.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@saimn the failures resolved here are now showing up as collection-time errors on allowed-failure jobs, making them blind to anything else. I'll rebase this branch to make it clear that it goes a bit further than main on this front.

Comment thread astropy/utils/compat/numpycompat.py Resolved
@pllim

pllim commented Feb 2, 2026

Copy link
Copy Markdown
Member

tl;dr -- devdeps still red here. What else is needed to make it green again?

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

#3862

@pllim

pllim commented Feb 2, 2026

Copy link
Copy Markdown
Member

Do you want to rebase one more time, and then hopefully @saimn can do a final review, if this PR is ready? Thanks!

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

rebased, and ready

@pllim

pllim commented Feb 20, 2026

Copy link
Copy Markdown
Member

Superseded by #19267

@pllim pllim closed this Feb 20, 2026
@neutrinoceros
neutrinoceros deleted the compat/is_chararray branch February 20, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants