Skip to content

Recognise LiteralString as str #12559

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Apr 13, 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
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