chore: fix typing of cloudinit.templater - #6989
Open
Mohith1612 wants to merge 1 commit into
Open
Conversation
JinjaSyntaxParsingException.format_error_message declared line_number as str, but the only production caller, __str__, passes self.lineno, which is an int. The annotation was wrong. Two test call sites had been written to match the wrong annotation and pass the string "2". Correct them to pass an int. Output is unchanged, since message_template renders line_number through str.format, so "2" and 2 produce the same string. With that fixed, cloudinit.templater and tests.unittests.test_templating both pass check_untyped_defs, so drop them from the mypy override list in pyproject.toml. Refs canonicalGH-5445
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.
Proposed Commit Message
Additional Context
Refs GH-5445.
There are six call sites of
format_error_messageacross five modules.Four pass an int, including the only production one, and two pass a string:
cloudinit/templater.py:49(__str__)self.lineno, an inttests/unittests/test_templating.py:2704tests/unittests/test_templating.py:2894tests/unittests/config/test_schema.py:10143tests/unittests/cmd/test_query.py:618"2"tests/unittests/cmd/devel/test_render.py:157"2"The two string call sites were the ones written against the incorrect
annotation. They sit in modules mypy already checks, which is why correcting
the annotation surfaces them. I went with
intrather than widening toUnion[int, str], since a line number is an int and the production calleralready treats it as one.
The mismatch went unnoticed because
message_templaterenders{line_number}through
str.format, so both types produce identical output. No assertion textchanges.
I kept this to the annotation and the call sites. The other functions in
templater.pyare untouched.One possible follow-up, not included here:
line_contentis declaredstr = "", buttests/unittests/test_templating.py:282parametrizes it over("", None), soOptional[str]would be more accurate. It does not affect anymypy result, so I left it out. Happy to do that as a separate PR if it's wanted.
Test Steps
Typing change with no runtime behaviour change, so no new tests. The existing
tests at
tests/unittests/test_templating.py:263-291already exerciseformat_error_messagewith an int line number, which is what showed theannotation was wrong.
Local runs on this branch:
Merge type