Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: POC header comment API design #332

Merged
merged 18 commits into from
Jul 26, 2024
Merged

feat: POC header comment API design #332

merged 18 commits into from
Jul 26, 2024

Conversation

simonrw
Copy link
Owner

@simonrw simonrw commented Jul 20, 2024

Motivation

Inspired by #307, can we design a nice API to read/write comments of header cards?

Changes

  • write_key takes either a simple value, or a tuple of (value, comment) for writing comments
  • read_key returns a new structured type which contains both the value and comment
  • tests and docs updated
  • restructure the headers code
    • create new headers module with previous code
    • add constants module, mostly with header related lengths
    • add header_value module defining the HeaderValue<T> type along with its std trait implementations

TODO

  • write_key support
  • read_key support
  • perform assertions on header card lengths
  • rewrite the change in a backwards compatible way
    • writing a header value is backwards compatible already
    • make read_key take either a primitive value (backwards compatible) or HeaderValue<_>
  • remove public access to HeaderValue internals? Or remove methods
    • decifded to keep fields as the type should be simple

@simonrw simonrw self-assigned this Jul 20, 2024
@coveralls
Copy link

coveralls commented Jul 20, 2024

Coverage Status

coverage: 76.094% (-0.7%) from 76.797%
when pulling 6c7fc37 on write-header-comments
into 1ebd973 on main.

@simonrw simonrw changed the title POC header comment API design major: POC header comment API design Jul 20, 2024
@sunipkm
Copy link

sunipkm commented Jul 24, 2024

I think this is the best way to have implemented this. The write APIs are unaffected, and read API only needs an additional identifier to differentiate between the value and the comment. Awesome!

As for deprecating the old method and adding a new one, read_key sounds more generic than (e.g.) read_fkey.
On the other hand, we could follow the same rationale as dropping xrange (which was better than range in Python 2.7) and making the behaviour of range in Python 3 the same as Python 2.7 xrange. However, range and xrange did not differ in calling convention, only in behavior. I guess this band-aid would need to be ripped off at some point, and the earlier would be better.

@simonrw simonrw changed the title major: POC header comment API design feat: POC header comment API design Jul 24, 2024
@simonrw simonrw marked this pull request as ready for review July 24, 2024 20:14
@simonrw simonrw requested a review from cjordan July 24, 2024 20:15
@simonrw
Copy link
Owner Author

simonrw commented Jul 24, 2024

@sunipkm thanks for the feedback.

I think this is the best way to have implemented this. The write APIs are unaffected, and read API only needs an additional identifier to differentiate between the value and the comment. Awesome!

Your comment made me think of a backwards-compatible way of implementing this! I now implement ReadsKey for both primitive values (e.g. i64) and HeaderValue<T>. The caller can decide what they want. If they just want the value, they can use the primitive version. If they need the comment, they can use the HeaderValue<T> version.

My concern is that this seems like a backwards-compatible change, but it might not be. I think it now hugely depends on the type inference used by the caller. If it is not ambiguous (e.g. they pass the header value to a function that accepts an i32) then we are golden. If they pass the value to something that prints it (i.e. uses the Debug impl) then their output will change.

As for deprecating the old method and adding a new one, read_key sounds more generic than (e.g.) read_fkey.
On the other hand, we could follow the same rationale as dropping xrange (which was better than range in Python 2.7) and making the behaviour of range in Python 3 the same as Python 2.7 xrange. However, range and xrange did not differ in calling convention, only in behavior. I guess this band-aid would need to be ripped off at some point, and the earlier would be better.

Are you sure you want to cite the Python 2 -> 3 transition?!?!?! (I remember it being painless for the most part, but I waited ages to upgrade, by which time the libraries I used had caught up.)

I appreciate the idea of moving fast and breaking things, which we can do as we are pre-1.0, however I value stability higher than API correctness.

@cjordan: as a user of this crate, does your current code compile if you check out this branch?

@simonrw
Copy link
Owner Author

simonrw commented Jul 24, 2024

Note: the coverage has gone down because we include some functionality for the HeaderValue type that is not tested in the --lib tests, but it is tested in the --doc tests.

@cjordan
Copy link
Collaborator

cjordan commented Jul 25, 2024

Hi @simonrw. I'm not able to check that this change will affect the code I used to work on (as of this year, I'm also an ex-astronomer), because those crates depend on fitsio version 0.20. However, the code looks good to me. I think it's a nice quality-of-life improvement.

@sunipkm
Copy link

sunipkm commented Jul 25, 2024

@simonrw haha I did the same, waited till Py 2.7 went EOL. I guess Py 2 -> 3 transition isn’t the best example but the point I was trying to make is, we should encourage the assignment of the more flexible interface/implementation to the easier-to-read method name, because those stick. If we deprecate read_keys now with the old behavior, shouldn’t we technically remove that method in the future completely and not replace it with the new behavior method, since the calling conventions will be different?

I agree on the point that you bring up, that it looks backwards compatible but it isn’t. I guess as long as function calls are mostly fine, it should be alright for the end user. String conversion of a number and read-back of it may be error prone anyway, e.g. how 268 is ambiguous unless we know the number system used to write it — and this bit of inconsistency/inconvenience now will greatly benefit the users down the line.

Copy link
Owner Author

@simonrw simonrw left a comment

Choose a reason for hiding this comment

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

Thanks both for your comments. I think it is safe to release this with a minor (so pre-release major) bump. I think type inference should revert to the old read_key implementation, or the user would have to be explicit about the type anyway, so I think we are ok.

@simonrw simonrw enabled auto-merge (squash) July 26, 2024 13:29
@simonrw simonrw merged commit 65c9962 into main Jul 26, 2024
11 of 12 checks passed
@simonrw simonrw deleted the write-header-comments branch July 26, 2024 13:31
@github-actions github-actions bot mentioned this pull request Jul 26, 2024
simonrw added a commit that referenced this pull request Jul 26, 2024
## 🤖 New release
* `fitsio`: 0.21.2 -> 0.21.3
* `fitsio-sys`: 0.5.2 -> 0.5.3
* `fitsio-derive`: 0.2.0 -> 0.2.1

<details><summary><i><b>Changelog</b></i></summary><p>

## `fitsio`
<blockquote>

##
[0.21.3](fitsio-v0.21.2...fitsio-v0.21.3)
- 2024-07-26

### Added
- POC header comment API design
([#332](#332))

### Other
- Add TSHORT types for i16 and u16
- Add clippy feature
- Merge branch 'main' into main
- Pin minimal serde version
- Update criterion requirement from 0.3.5 to 0.5.1 in /fitsio
- Fix nightly compile errors
- Use TBYTE for *8 reads
([#277](#277))
- Allow/fix warnings that are blocking CI
</blockquote>

## `fitsio-sys`
<blockquote>

##
[0.5.3](fitsio-sys-v0.5.2...fitsio-sys-v0.5.3)
- 2024-07-26

### Other
- Fix clippy warnings
- Update bindgen requirement from 0.66 to 0.69 in /fitsio-sys
- Include new changelog for fitsio-sys
- Provide aliases to function "long names".
- Update bindgen requirement from 0.63 to 0.66 in /fitsio-sys
### Added

* Added aliases for cfitsio short names
([#258](#258))

### Changed
### Removed
</blockquote>

## `fitsio-derive`
<blockquote>

##
[0.2.1](fitsio-derive-v0.2.0...fitsio-derive-v0.2.1)
- 2024-07-26

### Other
- Rename myself
- Specify required patch versions for all deps.
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

---------

Co-authored-by: Simon Walker <s.r.walker101@googlemail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants