Skip to content

bpo-47088: Add typing.LiteralString (PEP 675) #32064

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 6 commits into from
Apr 5, 2022

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Mar 23, 2022

@JelleZijlstra
Copy link
Member Author

cc @pradeep90 @gbleaney

Co-authored-by: Nick Pope <[email protected]>

Special type that includes only literal strings. A string
literal is compatible with ``LiteralString``, as is another
``LiteralString``, but an object typed as just ``str`` is not.
Copy link
Contributor

Choose a reason for hiding this comment

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

@JelleZijlstra Can we also point out that composing literal strings is fine too?

A one-line example below might be useful too.

Copy link
Member

Choose a reason for hiding this comment

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

I didn't follow the PEP closely -- how far does this go? I suppose "a" + "b" is good. What about "a" * 3? Or "a %s z" % "qqq"? Or ",".join(("x", "y", "z"))?

Copy link
Contributor

Choose a reason for hiding this comment

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

All three are fine. Basically, if all the inputs (including self) are LiteralString, then the output type is a LiteralString. (We listed str operations that preserve the LiteralString type.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

Copy link
Member Author

Choose a reason for hiding this comment

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

@pradeep90 any other feedback? Thanks for the review!

Lib/typing.py Outdated
...

query("SELECT * FROM table") # ok
query(f"SELECT * FROM {input()}") # not ok
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be worth including the + example here too. Otherwise, LGTM!

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, done

@JelleZijlstra
Copy link
Member Author

Planning to merge this once CI passes.

@JelleZijlstra JelleZijlstra merged commit cfb849a into python:main Apr 5, 2022
@JelleZijlstra JelleZijlstra deleted the pep675 branch April 5, 2022 14:21
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.

8 participants