Replace direct type checks with isinstance - #2717
Open
BharatDeva wants to merge 1 commit into
Open
Conversation
Use isinstance() for direct type comparisons across the code paths listed in issue certtools#2636. This keeps the behavior the same for the checked builtin and IntelMQ message types while following the preferred Python style and allowing subclasses where that is appropriate. The exact class equality check in Message.__eq__ is intentionally left as type(self) is type(other), because it distinguishes different Message subclasses after confirming that the other object is a Message instance. Signed-off-by: BharatDeva <278575558+BharatDeva@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2636.
This replaces direct
type(...) is ...checks withisinstance(...)in the code paths listed in the issue. The affected checks are simple builtin or IntelMQ message type checks, soisinstancekeeps the intended behavior while following the preferred Python style.One exact class comparison remains in
Message.__eq__:That comparison is intentional because message equality should still distinguish different
Messagesubclasses after confirming thatotheris aMessageinstance.Testing
Ran on WSL Ubuntu with Python 3.12.3:
PYTHONPATH=. .venv-wsl-pr/bin/python -m pytest -o addopts= intelmq/tests/lib/test_message.py intelmq/tests/lib/test_pipeline.py intelmq/tests/lib/test_utils.py155 passed, 7 skippedPYTHONPATH=. .venv-wsl-pr/bin/python -m pytest -o addopts= intelmq/tests/bots/experts/format_field/test_expert.py intelmq/tests/bots/parsers/generic/test_parser_csv.py intelmq/tests/bots/parsers/html_table/test_parser.py12 passed, 3 skippedPYTHONPATH=. .venv-wsl-pr/bin/python -m compileall -q intelmqgit diff --check