Skip to content

Commit

Permalink
Recognise LiteralString as str (#12559)
Browse files Browse the repository at this point in the history
Linking #12554

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja committed Apr 13, 2022
1 parent 1cccb0b commit f8ca233
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def get_column(self) -> int:
'typing.DefaultDict': 'collections.defaultdict',
'typing.Deque': 'collections.deque',
'typing.OrderedDict': 'collections.OrderedDict',
# HACK: a lie in lieu of actual support for PEP 675
'typing.LiteralString': 'builtins.str',
}

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

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

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


def get_nongen_builtins(python_version: Tuple[int, int]) -> Dict[str, str]:
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,21 @@ from typing_extensions import TypeAlias
A: TypeAlias = str
[builtins fixtures/bool.pyi]
[out]


[case testLiteralStringPep675]
# flags: --python-version 3.11
from typing import LiteralString as tpLS
from typing_extensions import LiteralString as tpxLS

def f(a: tpLS, b: tpxLS) -> None:
reveal_type(a) # N: Revealed type is "builtins.str"
reveal_type(b) # N: Revealed type is "builtins.str"

# This isn't the correct behaviour, but should unblock use of LiteralString in typeshed
f("asdf", "asdf")
string: str
f(string, string)

[builtins fixtures/tuple.pyi]
[typing fixtures/typing-medium.pyi]
1 change: 1 addition & 0 deletions test-data/unit/fixtures/typing-medium.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TypedDict = 0
NoReturn = 0
NewType = 0
TypeAlias = 0
LiteralString = 0

T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
Expand Down

0 comments on commit f8ca233

Please sign in to comment.