Skip to content
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

PEP 675: Switch to the name LiteralString #2292

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
PEP 675: set off "such as" phrases with commas
  • Loading branch information
pradeep90 committed Feb 1, 2022
commit c50dcdf7bbc3b56247cdcc3772cfec0bcca14f88
29 changes: 15 additions & 14 deletions pep-0675.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ There is currently no way to specify that a function parameter can be
of any literal string type; we have to specify the precise literal
string, such as ``Literal["foo"]``. This PEP introduces a supertype of
literal string types: ``LiteralString``. This allows a function to
accept arbitrary literal string types such as ``Literal["foo"]`` or
accept arbitrary literal string types, such as ``Literal["foo"]`` or
``Literal["bar"]``.


Motivation
==========

Expand Down Expand Up @@ -233,8 +234,8 @@ We want to specify that the value must be of some type
``LiteralString`` represents. ``LiteralString`` is the "supertype" of
all literal string types. In effect, this PEP just introduces a type
in the type hierarchy between ``Literal["foo"]`` and ``str``. Any
particular literal string such as ``Literal["foo"]`` or
``Literal["bar"]`` is compatible with ``LiteralString``, but not the
particular literal string, such as ``Literal["foo"]`` or
``Literal["bar"]``, is compatible with ``LiteralString``, but not the
other way around. The "supertype" of ``LiteralString`` itself is
``str``. So, ``LiteralString`` is compatible with ``str``, but not the
other way around.
Expand Down Expand Up @@ -759,9 +760,9 @@ Some notable alternatives were:

+ ``LiteralDerivedString``: This (along with
``MadeFromLiteralString``) best captures the technical meaning of
the type. It represents not just the type of literal expressions
such as ``"foo"`` but also that of expressions composed from
literals ``"foo" + "bar"``. However, both names seem wordy.
the type. It represents not just the type of literal expressions,
such as ``"foo"``, but also that of expressions composed from
literals, such as ``"foo" + "bar"``. However, both names seem wordy.

+ ``StringLiteral``: Users might confuse this with the existing
concept of `"string literals"
Expand All @@ -778,12 +779,12 @@ Some notable alternatives were:
strings.

+ ``StaticStr``: This suggests that the string is statically
computable, i.e., without running the program, which is not true. The
literal string may vary based on runtime flags, as seen in the
`Motivation`_ examples.
computable, i.e., computable without running the program, which is
not true. The literal string may vary based on runtime flags, as
seen in the `Motivation`_ examples.

+ ``LiteralOnly[str]``: This has the advantage of being extensible to
other literal types such as ``bytes`` or ``int``. However, we did
other literal types, such as ``bytes`` or ``int``. However, we did
not find the extensibility worth the loss of readability.

Overall, there was no clear winner on typing-sig over a long period,
pradeep90 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -793,8 +794,8 @@ so we decided to tip the scales in favor of ``LiteralString``.
``LiteralBytes``
----------------

We could generalize literal byte types such as ``Literal[b"foo"]`` to
``LiteralBytes``. However, literal byte types are used much less
We could generalize literal byte types, such as ``Literal[b"foo"]``,
to ``LiteralBytes``. However, literal byte types are used much less
frequently than literal string types and we did not find much user
demand for ``LiteralBytes``, so we decided not to include it in this
PEP. Others may, however, consider it in future PEPs.
Expand Down Expand Up @@ -879,7 +880,7 @@ only accept ``LiteralString``:
Server Side Template Injection (SSTI)
-------------------------------------

Templating frameworks such as Jinja allow Python expressions which
Templating frameworks, such as Jinja, allow Python expressions which
will be evaluated and substituted into the rendered result:

::
Expand Down Expand Up @@ -1268,7 +1269,7 @@ Literal String Types in Scala
Scala `uses
<https://www.scala-lang.org/api/2.13.x/scala/Singleton.html>`_
``Singleton`` as the supertype for singleton types, which includes
literal string types such as ``"foo"``. ``Singleton`` is Scala's
literal string types, such as ``"foo"``. ``Singleton`` is Scala's
generalized analogue of this PEP's ``LiteralString``.

Tamer Abdulradi showed how Scala's literal string types can be used
Expand Down