8000 Add Literal support for docstrings (#17441) · python/mypy@a27447d · GitHub
[go: up one dir, main page]

Skip to content

Commit a27447d

Browse files
Add Literal support for docstrings (#17441)
<!-- If this pull request fixes an issue, add "Fixes #NNN" with the issue number. --> (Explain how this PR changes mypy.) Updates the is_valid_type regex to include quotes and . <!-- Checklist: - Read the [Contributing Guidelines](https://github.com/python/mypy/blob/master/CONTRIBUTING.md) - Add tests for all changed behaviour. - If you can't add a test, please explain why and how you verified your changes work. - Make sure CI passes. - Please do not force push to the PR once it has been reviewed. -->
1 parent 69042d3 commit a27447d

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

mypy/stubdoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Sig: _TypeAlias = Tuple[str, str]
2121

2222

23-
_TYPE_RE: Final = re.compile(r"^[a-zA-Z_][\w\[\], ]*(\.[a-zA-Z_][\w\[\], ]*)*$")
23+
_TYPE_RE: Final = re.compile(r"^[a-zA-Z_][\w\[\], .\"\']*(\.[a-zA-Z_][\w\[\], ]*)*$")
2424
_ARG_NAME_RE: Final = re.compile(r"\**[A-Za-z_][A-Za-z0-9_]*$")
2525

2626

mypy/stubgenc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def __init__(
252252
"Iterable",
253253
"Iterator",
254254
"List",
255+
"Literal",
255256
"NamedTuple",
256257
"Optional",
257258
"Tuple",

mypy/test/teststubgen.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,17 @@ def test_is_valid_type(self) -> None:
13571357
assert is_valid_type("List[int]")
13581358
assert is_valid_type("Dict[str, int]")
13591359
assert is_valid_type("None")
1360+
assert is_valid_type("Literal[26]")
1361+
assert is_valid_type("Literal[0x1A]")
1362+
assert is_valid_type('Literal["hello world"]')
1363+
assert is_valid_type('Literal[b"hello world"]')
1364+
assert is_valid_type('Literal[u"hello world"]')
1365+
assert is_valid_type("Literal[True]")
1366+
assert is_valid_type("Literal[Color.RED]")
1367+
assert is_valid_type("Literal[None]")
1368+
assert is_valid_type(
1369+
'Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]'
1370+
)
13601371
assert not is_valid_type("foo-bar")
13611372
assert not is_valid_type("x->y")
13621373
assert not is_valid_type("True")

0 commit comments

Comments
 (0)
0