Added static type checker test for unknown error codes - #1388
Conversation
Coverage Report for CI Build 35781130294Coverage increased (+0.03%) to 90.907%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
| def test_ignores_unknown_message_error_code(self) -> None: | ||
| """Adds a message with an unknown error code, which should be ignored.""" | ||
| with self.assertNoMessages(): | ||
| self.checker._add_message({"code": "unknown-code"}, {}) |
There was a problem hiding this comment.
In general, private methods should not be called directly in tests. A better test case is one that uses a Python file that triggers a mypy error, but an error that does not appear in the dictionary of supported errors in the checker.
There was a problem hiding this comment.
Thank you for the feedback!
I started implementing a test case using a Python file triggering an unsupported mypy error. However, in the process_module method, any unsupported mypy errors are already filtered out if they do not match any of the SPECIFIC_PATTERNS regex. Thus, the _add_message method would never get called, and line 123 (the early return that was previously not covered by unit tests) would subsequently not be reached.
Lines 103-108 of static_type_checker.py:
specific_pattern = self.SPECIFIC_PATTERNS.get(common_data["code"]) # The error code would not be in SPECIFIC_PATTERNS, so this returns None
if specific_pattern: # Since specific_pattern is None, we do not enter this if block
specific_match = specific_pattern.search(common_data["message"])
if specific_match:
specific_data = specific_match.groupdict()
self._add_message(common_data, specific_data) # This call is never reachedCould you please advise on what next steps would be best to take?
There was a problem hiding this comment.
@angelayzheng ah excellent question. In this case, please mock the subprocess.run call to return an error message that isn't on the list, in the expected format (you could try running mypy separately to see the output format). You can use unittest.mock's patch function (take a look at other tests where we've done some mocking).
There was a problem hiding this comment.
Hi Professor @david-yz-liu,
I've pushed a new commit with the updated tests, which mock the subprocess.run call.
One question: I believe I may have taken a slightly different approach than what you were referring to. With the patch.dict method already adding the unknown pattern into SPECIFIC_PATTERNS during the call to process_module, the subprocess.run patch could technically be removed. Then mypy would be actually called on mypy_unknown_error.py, and it would still enter StaticTypeChecker._add_message (due to the patch to SPECIFIC_PATTERNS), inside which the mypy code func-returns-value would be unrecognized and return early.
Please let me know what you think of this approach. Thank you!
There was a problem hiding this comment.
Hi @angelayzheng, yes I'm happy patching SPECIFIC_PATTERNS and removing the patch for subprocess.run.
There was a problem hiding this comment.
Hi Professor @david-yz-liu, sounds good! I've pushed a new commit with the subprocess.run patch removed.
david-yz-liu
left a comment
There was a problem hiding this comment.
Nice work, @angelayzheng!
Proposed Changes
Added a test to improve code coverage in
static_type_checker.py. Ensures thatStaticTypeChecker._add_messagereturns early without error if an unsupported mypy error code is passed in.Previous coverage report: https://coveralls.io/builds/81479893/source?filename=packages%2Fpython-ta%2Fsrc%2Fpython_ta%2Fcheckers%2Fstatic_type_checker.py
...
Screenshots of your changes (if applicable)
Type of Change
(Write an
Xor a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]into a[x]in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)