Skip to content

[mypyc] Refactor: reuse format string parser #10894

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

Merged
merged 13 commits into from
Aug 2, 2021

Conversation

97littleleaf11
Copy link
Collaborator

@97littleleaf11 97littleleaf11 commented Jul 30, 2021

Description

parse_conversion_specifiers and parse_format_value from mypy.checkstrformat can be reused when compiling string formatting. can_optimize_format and split_braces are useless now.

Adding a start_pos as an attribute of ConversionSpecifier can not only help parse literals but also help generate useful error messages in the future.

Test Plan

@97littleleaf11 97littleleaf11 marked this pull request as ready for review August 1, 2021 18:02
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Nice! This reduces duplicated functionality and thus reduces the code that needs to be maintained, and it will be easier to support new format string features in mypyc as the parsing is already implemented.

Left a few minor comments, otherwise looks good.

# The empty Context and MessageBuilder for parse_format_value().
# They wouldn't be used since the code has passed the type-checking.
EMPTY_CONTEXT = Context()
EMPTY_MSG = MessageBuilder(Errors(), {'': MypyFile([], [])})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is mutable, it would be better to create this on each translate_str_format call. We don't want to risk accumulating state here.

Why do we need the '': MypyFile(...) entry in the dictionary? What happens if we leave it out? If we actually need it, please add a comment explaining why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can I mark EMPTY_MSG as Final?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't help -- see my other comment.

if not can_optimize_format(format_str):
return None
specifiers = parse_format_value(format_str, EMPTY_CONTEXT, EMPTY_MSG)
assert specifiers is not None
Copy link
Collaborator

Choose a reason for hiding this comment

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

This may not be safe to do. For example, what if there is a malformed format string and the error has been # type: ignored? I think that it would be better to fall back to the default slow path but still generate IR if we can't parse the format string. What do you think?

# The empty Context and MessageBuilder for parse_format_value().
# They wouldn't be used since the code has passed the type-checking.
EMPTY_CONTEXT: Final = Context()
EMPTY_MSG: Final = MessageBuilder(Errors(), {})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Making EMPTY_MSG final doesn't help with the issue I mentioned earlier. Errors() is mutable, so it's now shared mutable state, which we want to usually avoid. Please create MessageBuilder and Errors within translate_str_format to avoid the global state.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Looks good now!

@JukkaL JukkaL merged commit 9f409c3 into python:master Aug 2, 2021
sthagen added a commit to sthagen/python-mypy that referenced this pull request Aug 3, 2021
[mypyc] Refactor: reuse format string parser (python#10894)
@97littleleaf11 97littleleaf11 deleted the refactor-checkstrformat branch August 5, 2021 12:53
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