Skip to content

Ensure fits Header subclasses are preserved on slicing and copying - #9229

Merged
bsipocz merged 3 commits into
astropy:masterfrom
mhvk:io-fits-header-subclass-preservation
Sep 17, 2019
Merged

bsipocz merged 3 commits into
astropy:masterfrom
mhvk:io-fits-header-subclass-preservation

Conversation

@mhvk

@mhvk mhvk commented Sep 14, 2019

Copy link
Copy Markdown
Contributor

This PR makes Header sub-class safe, by ensuring that __getitem__ and copy return a self.__class__ instance rather than a Header instance. It adds a(nother) hack to CompImageHeader to ensure that for that case slicing and copying continues to return a Header instance.

fixes #7379

@mhvk mhvk added io.fits Refactoring API change PRs and issues that change an existing API, possibly requiring a deprecation period labels Sep 14, 2019
@mhvk mhvk added this to the v4.0 milestone Sep 14, 2019
@mhvk
mhvk force-pushed the io-fits-header-subclass-preservation branch from 05fe0a3 to fea594d Compare September 14, 2019 19:08
@bsipocz
bsipocz requested review from MSeifert04 and saimn September 14, 2019 21:51
@MSeifert04

Copy link
Copy Markdown
Contributor

The change looks reasonable, however I wonder if it's worth it to make Header subclass-safe. Was there any specific issue that requires this change or is it "just" to adhere to good-practices?

I'm really conservative if it comes to io.fits, and I wouldn't want to introduce API-changes that aren't motivated by some real issues/problems. What do you think @saimn ?

Comment thread astropy/io/fits/hdu/compressed.py Resolved
@mhvk

mhvk commented Sep 15, 2019

Copy link
Copy Markdown
Contributor Author

@MSeifert04 - I subclass Header myself, for a FITS-like header of radio baseband data. And mostly this works very well, but copying and slicing were exceptions (which I why I needed to override them: https://github.com/mhvk/baseband/blob/master/baseband/guppi/header.py#L92).

It is partially because the Header class is otherwise suitable for subclassing that I think it is worth to correct the few places where Header is hard-coded.

@MSeifert04

Copy link
Copy Markdown
Contributor

Ah, thanks for the examples. 👍

@saimn

saimn commented Sep 15, 2019

Copy link
Copy Markdown
Contributor

This looks good to me, subclassing Header can be useful and I see no reason why we shouldn't support this. I will let you approve and merge if you agree @MSeifert04 ;)

@bsipocz

bsipocz commented Sep 17, 2019

Copy link
Copy Markdown
Member

Approved, so I merge it now.

Thanks @mhvk!

@bsipocz
bsipocz merged commit 4c46eb6 into astropy:master Sep 17, 2019
@mhvk
mhvk deleted the io-fits-header-subclass-preservation branch September 17, 2019 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API change PRs and issues that change an existing API, possibly requiring a deprecation period io.fits Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fits Header does not preserve subclasses in copying and slicing.

4 participants