Skip to content

BUG: reject inconsistent converters to StringDType - #20762

Open
neutrinoceros wants to merge 3 commits into
astropy:mainfrom
neutrinoceros:io.ascii/tst/check-StringDType
Open

neutrinoceros wants to merge 3 commits into
astropy:mainfrom
neutrinoceros:io.ascii/tst/check-StringDType

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

Salvaged from gh-20744
The test already passes on main. However, I'm still including a companion refactor to avoid brittle substring matching
(the existing code would have broken when StringDType was introduced in numpy 2 if the class name had been stringdtype, for instance)

AI Disclosure

N/A

  • I certify that I am human and that I take full responsibility for this pull request including all interactions with reviewers.

Merge method

  • 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

github-actions Bot commented Nov 2, 2026

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/ascii/core.py Outdated
Comment on lines 1007 to 1008
# Infer converter type from an instance of numpy_type.
type_name = np.array([], dtype=numpy_type).dtype.name

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.

This line is dead now and the comment also doesn't match the code now.

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.

indeed. Fixed

Comment thread astropy/io/ascii/core.py Outdated
converter_type = FloatType
case "b":
converter_type = BoolType
case "U":

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.

Suggested change
case "U":
case "U" | "T":

If we are doing this then it probably makes sense to treat "T" as string-like. For instance:

lines = ["a\tb", "S\tN", "x\t1", "y\t2"]
ascii.read(lines, format="rdb", converters={"b": [ascii.convert_numpy(str)]})  # StrType: rejected -> guess failure
ascii.read(lines, format="rdb", converters={"b": [ascii.convert_numpy("T")]})  # AllType: accepted -> StringDType column

@neutrinoceros neutrinoceros Nov 3, 2026 •

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.

I'm not sure I understand your suggestion fully.
Here's what I'm grasping:

  • the str converter rejects the data
  • converting to StringDType however returns a result that to me looks valid

your suggestion would make both converters fail. I guess that's one way to resolve API inconsistencies, but I don't understand why that would be the behavior we expect. From where I'm standing, it actually looks like the str converter should be able to convert without raising here. Clearly we are not reaching the same conclusion, so am I missing anything ?

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.

In this case the RDB format specifies that column b is numeric via the N (the "S\tN") line. That type requirement from the file is inconsistent with the user request to treat the column as string and gets rejected with an exception.

This behavior is consistent with the design philosophy / intent of io.ascii that the file format can specify the allowed type(s) and the user can only narrow those types, not go outside.

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.

ah, I see. That makes a lot of sense, I just wasn't aware of the meaning caried by the second line.

@neutrinoceros
neutrinoceros force-pushed the io.ascii/tst/check-StringDType branch from 832fcfc to bebc190 Compare November 3, 2026 11:31
@neutrinoceros
neutrinoceros force-pushed the io.ascii/tst/check-StringDType branch from bebc190 to 14b9cf3 Compare November 3, 2026 13:31
@neutrinoceros neutrinoceros changed the title TST: add test case for reading a StringDType column from an ascii table BUG: reject inconsistent converters to StringDType Nov 3, 2026
@neutrinoceros neutrinoceros added Bug backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x and removed no-changelog-entry-needed labels Nov 3, 2026
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@taldcroft with your suggestion taken into account, the scope changed a bit and this now is a bugfix. I've applied backport labels but if you think that's too disruptive for a backport I think it's also fine not to. Up to you.

@taldcroft taldcroft removed backport-v7.2.x on-merge: backport to v7.2.x backport-v8.0.x on-merge: backport to v8.0.x labels Nov 3, 2026
@taldcroft

Copy link
Copy Markdown
Member

@taldcroft with your suggestion taken into account, the scope changed a bit and this now is a bugfix. I've applied backport labels but if you think that's too disruptive for a backport I think it's also fine not to. Up to you.

I don't honestly think this would affect any code, but you can probably guess that this falls in the category of a behavior change that I'd rather not backport. I left the milestone at 8.1 and took off the backport labels. Thanks

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

CI jobs that failed on network errors were rerun and passed. Is there anything else to do before this can be merged ?

@taldcroft taldcroft left a comment

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.

This is basically good but just a couple of non-code fixes.

Exact example from #9701: When using ascii.read with both the names and
converters arguments, the converters dictionary ignores the user-supplied
Adapted from #9701: When using ascii.read with both the names and
converters arguments, the converters dictionary ignores the user-supplied converters arguments, the converters dictionary ignores the user-supplied

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.

This line got garbled.

Comment thread astropy/io/ascii/core.py
@@ -1005,17 +1005,18 @@ def convert_numpy(numpy_type):
the required type.
"""
# Infer converter type from an instance of numpy_type.

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.

This comment is no longer correct, please update.

This branch has not been deployed

No deployments
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.

2 participants