Skip to content

Commit 182c18a

Browse files
author
hauntsaninja
committed
Recognise LiteralString as str
Linking python#12554
1 parent 6a24e1d commit 182c18a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

mypy/nodes.py

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def get_column(self) -> int:
123123
'typing.DefaultDict': 'collections.defaultdict',
124124
'typing.Deque': 'collections.deque',
125125
'typing.OrderedDict': 'collections.OrderedDict',
126+
'typing.LiteralString': 'builtins.str', # a lie in lieu of actual support for PEP 675
126127
}
127128

128129
# This keeps track of the oldest supported Python version where the corresponding
@@ -137,12 +138,14 @@ def get_column(self) -> int:
137138
'typing.DefaultDict': (2, 7),
138139
'typing.Deque': (2, 7),
139140
'typing.OrderedDict': (3, 7),
141+
'typing.LiteralString': (3, 11),
140142
}
141143

142144
# This keeps track of aliases in `typing_extensions`, which we treat specially.
143145
typing_extensions_aliases: Final = {
144146
# See: https://github.com/python/mypy/issues/11528
145147
'typing_extensions.OrderedDict': 'collections.OrderedDict',
148+
'typing_extensions.LiteralString': 'builtins.str', # a lie in lieu of actual support for PEP 675
146149
}
147150

148151
reverse_builtin_aliases: Final = {

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

+18
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

+1
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)