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
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ annotations. These include:
*Introducing* :data:`TypeGuard`
* :pep:`673`: Self type
*Introducing* :data:`Self`
* :pep:`675`: Arbitrary Literal String Type
*Introducing* :data:`LiteralString`

.. _type-aliases:

Expand Down Expand Up @@ -585,6 +587,33 @@ These can be used as types in annotations and do not support ``[]``.
avoiding type checker errors with classes that can duck type anywhere or
are highly dynamic.

.. data:: LiteralString

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!

A string created by composing ``LiteralString``-typed objects
is also acceptable as a ``LiteralString``.

Example::

def run_query(sql: LiteralString) -> ...
...

def caller(arbitrary_string: str, literal_string: LiteralString) -> None:
run_query("SELECT * FROM students") # ok
run_query(literal_string) # ok
run_query("SELECT * FROM " + literal_string) # ok
run_query(arbitrary_string) # type checker error
run_query( # type checker error
f"SELECT * FROM students WHERE name = {arbitrary_string}"
)

This is useful for sensitive APIs where arbitrary user-generated
strings could generate problems. For example, the two cases above
that generate type checker errors could be vulnerable to an SQL
injection attack.

.. data:: Never

The `bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_,
Expand Down
56 changes: 55 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
from typing import Annotated, ForwardRef
from typing import Self
from typing import Self, LiteralString
from typing import TypeAlias
from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs
from typing import TypeGuard
Expand Down Expand Up @@ -265,6 +265,60 @@ def test_alias(self):
self.assertEqual(get_args(alias_3), (Self,))


class LiteralStringTests(BaseTestCase):
def test_equality(self):
self.assertEqual(LiteralString, LiteralString)
self.assertIs(LiteralString, LiteralString)
self.assertNotEqual(LiteralString, None)

def test_basics(self):
class Foo:
def bar(self) -> LiteralString: ...
class FooStr:
def bar(self) -> 'LiteralString': ...
class FooStrTyping:
def bar(self) -> 'typing.LiteralString': ...

for target in [Foo, FooStr, FooStrTyping]:
with self.subTest(target=target):
self.assertEqual(gth(target.bar), {'return': LiteralString})
self.assertIs(get_origin(LiteralString), None)

def test_repr(self):
self.assertEqual(repr(LiteralString), 'typing.LiteralString')

def test_cannot_subscript(self):
with self.assertRaises(TypeError):
LiteralString[int]

def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class C(type(LiteralString)):
pass
with self.assertRaises(TypeError):
class C(LiteralString):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
LiteralString()
with self.assertRaises(TypeError):
type(LiteralString)()

def test_no_isinstance(self):
with self.assertRaises(TypeError):
isinstance(1, LiteralString)
with self.assertRaises(TypeError):
issubclass(int, LiteralString)

def test_alias(self):
alias_1 = Tuple[LiteralString, LiteralString]
alias_2 = List[LiteralString]
alias_3 = ClassVar[LiteralString]
self.assertEqual(get_args(alias_1), (LiteralString, LiteralString))
self.assertEqual(get_args(alias_2), (LiteralString,))
self.assertEqual(get_args(alias_3), (LiteralString,))

class TypeVarTests(BaseTestCase):
def test_basic_plain(self):
T = TypeVar('T')
Expand Down
31 changes: 30 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def _idfunc(_, x):
'get_origin',
'get_type_hints',
'is_typeddict',
'LiteralString',
'Never',
'NewType',
'no_type_check',
Expand Down Expand Up @@ -180,7 +181,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
if (isinstance(arg, _GenericAlias) and
arg.__origin__ in invalid_generic_forms):
raise TypeError(f"{arg} is not valid as type argument")
if arg in (Any, NoReturn, Never, Self, TypeAlias):
if arg in (Any, LiteralString, NoReturn, Never, Self, TypeAlias):
return arg
if allow_special_forms and arg in (ClassVar, Final):
return arg
Expand Down Expand Up @@ -523,6 +524,34 @@ def returns_self(self) -> Self:
raise TypeError(f"{self} is not subscriptable")


@_SpecialForm
def LiteralString(self, parameters):
"""Represents an arbitrary literal string.

Example::

from typing import LiteralString

def run_query(sql: LiteralString) -> ...
...

def caller(arbitrary_string: str, literal_string: LiteralString) -> None:
run_query("SELECT * FROM students") # ok
run_query(literal_string) # ok
run_query("SELECT * FROM " + literal_string) # ok
run_query(arbitrary_string) # type checker error
run_query( # type checker error
f"SELECT * FROM students WHERE name = {arbitrary_string}"
)

Only string literals and other LiteralStrings are compatible
with LiteralString. This provides a tool to help prevent
security issues such as SQL injection.

"""
raise TypeError(f"{self} is not subscriptable")


@_SpecialForm
def ClassVar(self, parameters):
"""Special type construct to mark class variables.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement :data:`typing.LiteralString`, part of :pep:`675`. Patch by Jelle
Zijlstra.