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

[BUG] Source says "Not a typo.", but I'm pretty sure it's a typo #65295

Closed
bendikro opened this issue Sep 26, 2023 · 6 comments · Fixed by #65540
Closed

[BUG] Source says "Not a typo.", but I'm pretty sure it's a typo #65295

bendikro opened this issue Sep 26, 2023 · 6 comments · Fixed by #65540
Labels
Bug broken, incorrect, or confusing behavior needs-triage

Comments

@bendikro
Copy link

Description
On line 445 in salt/modules/nftables.py we find:

    # Not a typo. Invert the dictionary twice to get unique values only.
    nft_families = {v: k for k, v in _NFTABLES_FAMILIES.items()}
    nft_families = {v: k for k, v in _NFTABLES_FAMILIES.items()}

How do these two identical lines make sense?

@bendikro bendikro added Bug broken, incorrect, or confusing behavior needs-triage labels Sep 26, 2023
@welcome
Copy link

welcome bot commented Sep 26, 2023

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!

@lkubb
Copy link
Contributor

lkubb commented Sep 26, 2023

I suppose it was meant as

    # Not a typo. Invert the dictionary twice to get unique values only.
    nft_families = {v: k for k, v in _NFTABLES_FAMILIES.items()}
    nft_families = {v: k for k, v in nft_families.items()}

to filter unique values and then get back some set of corresponding labels. Since each label has an identity mapping, it would make much more sense to just write set(_NFTABLES_FAMILIES.values()). Although nonsensical and inefficient, the current code should still function as expected from what I glanced.

@bendikro
Copy link
Author

bendikro commented Sep 26, 2023

Your suggestion would make sense. However, if the code is supposed to be

    # Not a typo. Invert the dictionary twice to get unique values only.
    nft_families = {v: k for k, v in _NFTABLES_FAMILIES.items()}
    nft_families = {v: k for k, v in nft_families.items()}

the resulting dict depends on the order of the key/values in _NFTABLES_FAMILIES returned by items(). Specifically the keys that have the same values. Is that intended?

The current code works, however, the second line has no effect.

@lkubb
Copy link
Contributor

lkubb commented Sep 26, 2023

From what I can tell (I'm not the author), the only purpose of the resulting dict's keys would be to be turned back into their values again, so order should not matter, just the uniqueness of values.

for family in nft_families:
out = get_rules(family)

nft_family = _NFTABLES_FAMILIES[family]

@whytewolf
Copy link
Collaborator

@nicholasmhughes any comment on this? this was your code change.

@nicholasmhughes
Copy link
Collaborator

I suppose it was meant as

    # Not a typo. Invert the dictionary twice to get unique values only.
    nft_families = {v: k for k, v in _NFTABLES_FAMILIES.items()}
    nft_families = {v: k for k, v in nft_families.items()}

to filter unique values and then get back some set of corresponding labels. Since each label has an identity mapping, it would make much more sense to just write set(_NFTABLES_FAMILIES.values()). Although nonsensical and inefficient, the current code should still function as expected from what I glanced.

it's been forever since I messed with this... at first glance, I think it was probably intended to be this ☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior needs-triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants