From 182c18a97fa524950db7cac9540b5fdf926f346c Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 11 Apr 2022 00:35:47 -0400 Subject: [PATCH 1/3] Recognise LiteralString as str Linking #12554 --- mypy/nodes.py | 3 +++ test-data/unit/check-type-aliases.test | 18 ++++++++++++++++++ test-data/unit/fixtures/typing-medium.pyi | 1 + 3 files changed, 22 insertions(+) diff --git a/mypy/nodes.py b/mypy/nodes.py index 7fcf5d85673c..5682ae49f77e 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -123,6 +123,7 @@ def get_column(self) -> int: 'typing.DefaultDict': 'collections.defaultdict', 'typing.Deque': 'collections.deque', 'typing.OrderedDict': 'collections.OrderedDict', + 'typing.LiteralString': 'builtins.str', # a lie in lieu of actual support for PEP 675 } # This keeps track of the oldest supported Python version where the corresponding @@ -137,12 +138,14 @@ 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', + 'typing_extensions.LiteralString': 'builtins.str', # a lie in lieu of actual support for PEP 675 } reverse_builtin_aliases: Final = { diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test index 61d53870d724..111743e9235e 100644 --- a/test-data/unit/check-type-aliases.test +++ b/test-data/unit/check-type-aliases.test @@ -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] diff --git a/test-data/unit/fixtures/typing-medium.pyi b/test-data/unit/fixtures/typing-medium.pyi index 923ede2f0c00..568fe057c4cf 100644 --- a/test-data/unit/fixtures/typing-medium.pyi +++ b/test-data/unit/fixtures/typing-medium.pyi @@ -27,6 +27,7 @@ TypedDict = 0 NoReturn = 0 NewType = 0 TypeAlias = 0 +LiteralString = 0 T = TypeVar('T') T_co = TypeVar('T_co', covariant=True) From 75054d488c84d7f1884202ee3c45173ee0337877 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 11 Apr 2022 00:43:09 -0400 Subject: [PATCH 2/3] okay flake8 --- mypy/nodes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 5682ae49f77e..5f2d05717b02 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -123,7 +123,8 @@ def get_column(self) -> int: 'typing.DefaultDict': 'collections.defaultdict', 'typing.Deque': 'collections.deque', 'typing.OrderedDict': 'collections.OrderedDict', - 'typing.LiteralString': 'builtins.str', # a lie in lieu of actual support for PEP 675 + # 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 @@ -145,7 +146,8 @@ def get_column(self) -> int: typing_extensions_aliases: Final = { # See: https://github.com/python/mypy/issues/11528 'typing_extensions.OrderedDict': 'collections.OrderedDict', - 'typing_extensions.LiteralString': 'builtins.str', # a lie in lieu of actual support for PEP 675 + # a lie in lieu of actual support for PEP 675 + 'typing_extensions.LiteralString': 'builtins.str', } reverse_builtin_aliases: Final = { From 3dacbdf0c264d40799c6496d8c971a2229f0912c Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 11 Apr 2022 01:06:05 -0400 Subject: [PATCH 3/3] fix test --- mypy/nodes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 5f2d05717b02..ff9276213ddc 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -123,7 +123,7 @@ def get_column(self) -> int: 'typing.DefaultDict': 'collections.defaultdict', 'typing.Deque': 'collections.deque', 'typing.OrderedDict': 'collections.OrderedDict', - # a lie in lieu of actual support for PEP 675 + # HACK: a lie in lieu of actual support for PEP 675 'typing.LiteralString': 'builtins.str', } @@ -146,7 +146,7 @@ def get_column(self) -> int: typing_extensions_aliases: Final = { # See: https://github.com/python/mypy/issues/11528 'typing_extensions.OrderedDict': 'collections.OrderedDict', - # a lie in lieu of actual support for PEP 675 + # HACK: a lie in lieu of actual support for PEP 675 'typing_extensions.LiteralString': 'builtins.str', } @@ -161,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]: