Repository navigation
fix ruff rule B007 violation in astropy/wcs - #17866
Conversation
|
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.
|
nstarman
left a comment
There was a problem hiding this comment.
Thanks @kangqiwang for the targeted PR! Looking at the code, there's an even larger simplification that can be done using a list comprehension.
| missing_keys = [] | ||
| for kw, val in full_header.items(): | ||
| for kw in full_header.keys(): | ||
| if kw not in header: | ||
| missing_keys.append(kw) |
There was a problem hiding this comment.
missing_keys = [k for k in full_header.keys() if k not in header]
There was a problem hiding this comment.
This can be even shorter:
missing_keys = [k for k in full_header if k not in header]
There was a problem hiding this comment.
I didn't check if full_header is a dict. .items() (and .keys()) doesn't guarantee it has dict-like iteration over keys, though it's definitely bad API if it doesn't iterate like that.
There was a problem hiding this comment.
full_header coming from to_header() function, which return astropy.io.fits.Header
There was a problem hiding this comment.
Sorry for the delayed response. I just read through the source code and documentation. I tried to find relevant test cases in test_header and attempted to verify my understanding using a for loop on header, but I couldn't find any direct tests. However, from the source code, it looks like header has both dict-like and list-like behavior for FITS headers.
here's init method:
def __init__(self, cards=[], copy=False):
self.clear()
if isinstance(cards, Header):
if copy:
cards = cards.copy()
cards = cards.cards
elif isinstance(cards, dict):
cards = cards.items()
for card in cards:
self.append(card, end=True)
self._modified = False
And here's iter function
def __iter__(self):
for card in self._cards:
yield card.keyword
Let me know if this makes sense to you!
There was a problem hiding this comment.
The __iter__ is then the same as for k in keys()?
|
@kangqiwang can you rebase this branch and resolve merge conflicts ? |
Co-authored-by: Eero Vaher <[email protected]>
7c2e0c8 to
a3633bb
Compare
slove merge issues update ruff.toml update ruff.toml update ruff.toml
a3633bb to
b671c86
Compare
done, let me know u find more issues |
fix ruff rule B007 violation in astropy/wcs
Description
This pull request is to address to split pull request #17844 , and it ensure compliance of astropy/wcs to Ruff rule unused-loop-control-variable (B007)
Fixes partially #14818