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

Keep the dict sorted by keys to spot two duplicates #1998

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Nov 21, 2024

Keep dict keys sorted to make it easy to spot missing keys and nearly impossible to insert duplicates.

% ruff check bbot/core/helpers/libmagic.py

bbot/core/helpers/libmagic.py:63:9: F601 Dictionary key literal `"application/x-zstd-compressed-tar"` repeated
   |
61 |         "application/x-zoo": "zoo",  # Zoo archive
62 |         "application/x-arc": "arc",  # ARC archive
63 |         "application/x-zstd-compressed-tar": "zstd",  # Zstandard compressed Tar archive
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F601
64 |         "application/x-lz4-compressed-tar": "lz4",  # LZ4 compressed Tar archive
65 |         "application/vnd.comicbook-rar": "rar",  # Comic book archive (RAR)
   |
   = help: Remove repeated key literal `"application/x-zstd-compressed-tar"`

bbot/core/helpers/libmagic.py:64:9: F601 Dictionary key literal `"application/x-lz4-compressed-tar"` repeated
   |
62 |         "application/x-arc": "arc",  # ARC archive
63 |         "application/x-zstd-compressed-tar": "zstd",  # Zstandard compressed Tar archive
64 |         "application/x-lz4-compressed-tar": "lz4",  # LZ4 compressed Tar archive
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F601
65 |         "application/vnd.comicbook-rar": "rar",  # Comic book archive (RAR)
66 |     }
   |
   = help: Remove repeated key literal `"application/x-lz4-compressed-tar"`

Found 2 errors.

% ruff rule F601

multi-value-repeated-key-literal (F601)

Derived from the Pyflakes linter.

Fix is sometimes available.

What it does

Checks for dictionary literals that associate multiple values with the
same key.

Why is this bad?

Dictionary keys should be unique. If a key is associated with multiple values,
the earlier values will be overwritten. Including multiple values for the
same key in a dictionary literal is likely a mistake.

Example

foo = {
    "bar": 1,
    "baz": 2,
    "baz": 3,
}
foo["baz"]  # 3

Use instead:

foo = {
    "bar": 1,
    "baz": 2,
}
foo["baz"]  # 2

References

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93%. Comparing base (2323594) to head (a51f0ef).
Report is 8 commits behind head on dev.

Additional details and impacted files
@@          Coverage Diff          @@
##             dev   #1998   +/-   ##
=====================================
- Coverage     93%     93%   -0%     
=====================================
  Files        370     370           
  Lines      28312   28311    -1     
=====================================
- Hits       26133   26098   -35     
- Misses      2179    2213   +34     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@TheTechromancer TheTechromancer merged commit fea0e17 into blacklanternsecurity:dev Nov 21, 2024
13 of 14 checks passed
@cclauss cclauss deleted the duplicate-dict-keys-in-libmagic.py branch November 21, 2024 14:42
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.

2 participants