Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

eerovaher
Copy link
Member

Description

The repr of Python strings uses ', not ", so to be consistent with that the repr of astropy units should also use '. The reason why " is currently used is a change in bd684a0. From what I can tell the change was arbitrary and I couldn't find any discussion about it.

There is no change in the repr of Quantity and in any case APE 2 allows changing the output of __repr__() even in bugfix releases.

  • 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.

Copy link
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 on lines 105 to +108
>>> su.field_names
(['f0', ('f0', 'f1')], 'f1')
>>> su['f1']
Unit("yr")
Unit('yr')
Copy link
Member Author

Choose a reason for hiding this comment

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

Notice that the quotes are now consistent with the quotes in field_names.

Comment on lines 734 to +735
"^Object with NFKC normalized name 'h' already exists in given namespace "
r'\(Unit\("h"\)\)\.$'
r"\(Unit\('h'\)\)\.$"
Copy link
Member Author

Choose a reason for hiding this comment

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

If this error message is ever shortened so that it would fit on one line then without this patch it would be necessary to explicitly escape the quotation marks within the string.

{'l': Unit("deg"), 'b': Unit("deg")}
{'l': Unit('deg'), 'b': Unit('deg')}
Copy link
Member Author

Choose a reason for hiding this comment

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

No good reason for using both ' and " here.

The repr of Python strings uses `'`, not `"`, so for consistency the
repr of `astropy` units should also use `'`. This change does not affect
the representation of `Quantity` instances.
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Hmm, I think that original change is indeed a mistake, so from that perspective good to correct it. It is also is just that little bit cleaner repr, making it easier to parse (though inconsistent with ruff -- IMO, it would've been better if ruff preferred single quotes too).

It is more than a little churn, though, likely to break at least some doctests downstream as well, so I'd like agreement from the other reviewers that this is worth it.

Thankfully, in most cases (Quantity, Table, SkyCoord, etc.), the repr is not used; the corrections here are almost all just places where the docs explicitly look at a unit.

Overall, +0.5 myself.

@eerovaher
Copy link
Member Author

Nobody has objected, so we can move forward with this.

It is also is just that little bit cleaner repr, making it easier to parse (though inconsistent with ruff -- IMO, it would've been better if ruff preferred single quotes too).

I don't understand what you mean by that.

@mhvk
Copy link
Contributor

mhvk commented Aug 25, 2025

It is also is just that little bit cleaner repr, making it easier to parse (though inconsistent with ruff -- IMO, it would've been better if ruff preferred single quotes too).

I don't understand what you mean by that.

Just grumpy me that ruff chose to use the quote for strings that is not the default in python repr. Nothing one can do about it for this PR, though.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

OK, happy to sign off on this for the units part.

Would like sign off for the other packages, though, so ping again @modelling, @cosmology, @taldcroft, @mwcraig, @bmorris3.

@github-project-automation github-project-automation bot moved this from Triage to Reviewer approved in Cosmology, the Expansion Aug 25, 2025
@taldcroft
Copy link
Member

I would suggest to bump this up to an email to astropy-dev for consideration. This is a pretty good example of a PR that makes no functional improvement to astropy but has the potential to cause non-productive work downstream.

@taldcroft
Copy link
Member

So please DO NOT merge just yet.

@taldcroft
Copy link
Member

And my feedback is that we've lived this for 11 years and given the ruff precedent and significant potential for needless docstring breakage downstream, I'm πŸ‘Ž on this PR.

Copy link
Member

@nstarman nstarman left a comment

Choose a reason for hiding this comment

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

Cosmology and units LGTM.
It is a move towards the PEP8 standards and general consistency with most other libraries.
While APE 2 does allow for __repr__ changes I do agree that Quantity and Unit are some of the most likely to show up in downstream doctests, so for that reason we should.

  1. Do this for a Major.0 release.
  2. Add to the what'snew a link to pytest-doctestplus's instructions on how to restrict doctests to specific versions of Astropy.

@taldcroft
Copy link
Member

Do this for a Major.0 release.

Agreed. One quick-ish test of impact would be to see how many breakages this causes in the coordinated packages.

It is a move towards the PEP8 standards

The only mention I could find of quoting in PEP8 is this: In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it.

Add to the what'snew a link to pytest-doctestplus's instructions on how to restrict doctests to specific versions of Astropy.

Would you be recommending that downstream packages pin astropy at 7.2 for doctest forever?

@mhvk
Copy link
Contributor

mhvk commented Aug 25, 2025

Would you be recommending that downstream packages pin astropy at 7.2 for doctest forever?

It would be the other way around, change to 8.0 and exclude 7.2 from tests.

Note that, as I wrote above, I think the impact will actually be quite minor - it only appears when one explicitly displays a unit, in all container classes (Quantity, SkyCoord, Time, etc.), only unit.__str__() is used, which is unchanged. This is perhaps why the previous, accidental change from single to double quote did not cause any problems (though of course there were far fewer packages depending on astropy at that time!).

Anyway, the above feeling that risks are not that big is why I had a +0.5. But I think you are right it behooves us to test this on the coordinated packages at least (actually, it might not be bad to have a special CI entry + label that does exactly that).

@taldcroft
Copy link
Member

Would you be recommending that downstream packages pin astropy at 7.2 for doctest forever?

It would be the other way around, change to 8.0 and exclude 7.2 from tests.

I was assuming this PR goes into 8.0 and that 7.2 is the last one using double quotes. Then either downstream pins at 7.2 forever or at some point they do the work of fixing doctests to test against the current release. Am I still not understanding?

Anyway, the above feeling that risks are not that big is why I had a +0.5. But I think you are right it behooves us to test this on the coordinated packages at least (actually, it might not be bad to have a special CI entry + label that does exactly that).

Another question is if the astropy notebook tutorials will be impacted. Are they regression tested?

@mhvk
Copy link
Contributor

mhvk commented Aug 25, 2025

Would you be recommending that downstream packages pin astropy at 7.2 for doctest forever?

It would be the other way around, change to 8.0 and exclude 7.2 from tests.

I was assuming this PR goes into 8.0 and that 7.2 is the last one using double quotes. Then either downstream pins at 7.2 forever or at some point they do the work of fixing doctests to test against the current release. Am I still not understanding?

That's correct. And when they do the work, they can pin >=8.0 (if they still support older astropy versions).

@pllim
Copy link
Member

pllim commented Aug 25, 2025

Given the discussions about v8.0 milestone and potentially breaking downstream, I am updating the milestone and turning this into draft for now. Thanks for your understanding.

@pllim pllim modified the milestones: v7.2.0, v8.0.0 Aug 25, 2025
@pllim pllim marked this pull request as draft August 25, 2025 22:11
@eerovaher
Copy link
Member Author

Answering #18509 (comment)

Another question is if the astropy notebook tutorials will be impacted. Are they regression tested?

A notebook would prefer using _repr_latex_() over __repr__(), so I expect the impact on notebooks to be minimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Reviewer approved
Development

Successfully merging this pull request may close these issues.

5 participants