Skip to content

Commit f8ca233

Browse files
authored
Recognise LiteralString as str (#12559)
Linking #12554 Co-authored-by: hauntsaninja <>
1 parent 1cccb0b commit f8ca233

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

mypy/nodes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def get_column(self) -> int:
123123
'typing.DefaultDict': 'collections.defaultdict',
124124
'typing.Deque': 'collections.deque',
125125
'typing.OrderedDict': 'collections.OrderedDict',
126+
# HACK: a lie in lieu of actual support for PEP 675
127+
'typing.LiteralString': 'builtins.str',
126128
}
127129

128130
# This keeps track of the oldest supported Python version where the corresponding
@@ -137,12 +139,15 @@ def get_column(self) -> int:
137139
'typing.DefaultDict': (2, 7),
138140
'typing.Deque': (2, 7),
139141
'typing.OrderedDict': (3, 7),
142+
'typing.LiteralString': (3, 11),
140143
}
141144

142145
# This keeps track of aliases in `typing_extensions`, which we treat specially.
143146
typing_extensions_aliases: Final = {
144147
# See: https://github.com/python/mypy/issues/11528
145148
'typing_extensions.OrderedDict': 'collections.OrderedDict',
149+
# HACK: a lie in lieu of actual support for PEP 675
150+
'typing_extensions.LiteralString': 'builtins.str',
146151
}
147152

148153
reverse_builtin_aliases: Final = {
@@ -156,6 +161,8 @@ def get_column(self) -> int:
156161
_nongen_builtins.update((name, alias) for alias, name in type_aliases.items())
157162
# Drop OrderedDict from this for backward compatibility
158163
del _nongen_builtins['collections.OrderedDict']
164+
# HACK: consequence of hackily treating LiteralString as an alias for str
165+
del _nongen_builtins['builtins.str']
159166

160167

161168
def get_nongen_builtins(python_version: Tuple[int, int]) -> Dict[str, str]:

test-data/unit/check-type-aliases.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,21 @@ from typing_extensions import TypeAlias
752752
A: TypeAlias = str
753753
[builtins fixtures/bool.pyi]
754754
[out]
755+
756+
757+
[case testLiteralStringPep675]
758+
# flags: --python-version 3.11
759+
from typing import LiteralString as tpLS
760+
from typing_extensions import LiteralString as tpxLS
761+
762+
def f(a: tpLS, b: tpxLS) -> None:
763+
reveal_type(a) # N: Revealed type is "builtins.str"
764+
reveal_type(b) # N: Revealed type is "builtins.str"
765+
766+
# This isn't the correct behaviour, but should unblock use of LiteralString in typeshed
767+
f("asdf", "asdf")
768+
string: str
769+
f(string, string)
770+
771+
[builtins fixtures/tuple.pyi]
772+
[typing fixtures/typing-medium.pyi]

test-data/unit/fixtures/typing-medium.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ TypedDict = 0
2727
NoReturn = 0
2828
NewType = 0
2929
TypeAlias = 0
30+
LiteralString = 0
3031

3132
T = TypeVar('T')
3233
T_co = TypeVar('T_co', covariant=True)

0 commit comments

Comments
 (0)