Skip to content

feat: support constant folding in StringFormatterChecker.checkers_for_c_type [1/1]#19978

Open
BobTheBuidler wants to merge 8 commits into
python:masterfrom
BobTheBuidler:constant-fold-checkers-for-c-type
Open

feat: support constant folding in StringFormatterChecker.checkers_for_c_type [1/1]#19978
BobTheBuidler wants to merge 8 commits into
python:masterfrom
BobTheBuidler:constant-fold-checkers-for-c-type

Conversation

@BobTheBuidler

Copy link
Copy Markdown
Contributor

This PR implements constant folding in StringFormatterChecker.checkers_for_c_type

@BobTheBuidler BobTheBuidler changed the title feat: support constant folding in StringFormatterChecker.checkers_for_c_type feat: support constant folding in StringFormatterChecker.checkers_for_c_type [1/1] Oct 2, 2025
@github-actions

This comment has been minimized.

Comment thread mypy/checkstrformat.py
self.msg.requires_int_or_single_byte(context)
elif isinstance(expr, (StrExpr, BytesExpr)) and len(expr.value) != 1:
self.msg.requires_int_or_char(context)
elif isinstance(folded := constant_fold_expr(expr, "<unused>"), str):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: can I get cur_mod_id from context?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to pass cur_mod_id here for this PR to work properly. I'm just not entirely sure how we can get access to it here.

@github-actions

This comment has been minimized.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a mypy checker test case.

@BobTheBuidler

BobTheBuidler commented Oct 4, 2025

Copy link
Copy Markdown
Contributor Author

I forgot I can't use Final for my test here since we don't pass cur_mod_id into the constant_fold_expr call (should we? where can I get it from?) so my current test is not good.

But once we merge #19982 I can do a simple "abc"[1] for the test for this PR

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

3 participants