-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"))
?
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done
Planning to merge this once CI passes. |
https://bugs.python.org/issue47088