Skip to content

Commit

Permalink
Add Literal support for docstrings (#17441)
Browse files Browse the repository at this point in the history
<!-- 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.
-->
  • Loading branch information
InvincibleRMC committed Jun 29, 2024
1 parent 69042d3 commit a27447d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Sig: _TypeAlias = Tuple[str, str]


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


Expand Down
1 change: 1 addition & 0 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def __init__(
"Iterable",
"Iterator",
"List",
"Literal",
"NamedTuple",
"Optional",
"Tuple",
Expand Down
11 changes: 11 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,17 @@ def test_is_valid_type(self) -> None:
assert is_valid_type("List[int]")
assert is_valid_type("Dict[str, int]")
assert is_valid_type("None")
assert is_valid_type("Literal[26]")
assert is_valid_type("Literal[0x1A]")
assert is_valid_type('Literal["hello world"]')
assert is_valid_type('Literal[b"hello world"]')
assert is_valid_type('Literal[u"hello world"]')
assert is_valid_type("Literal[True]")
assert is_valid_type("Literal[Color.RED]")
assert is_valid_type("Literal[None]")
assert is_valid_type(
'Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]'
)
assert not is_valid_type("foo-bar")
assert not is_valid_type("x->y")
assert not is_valid_type("True")
Expand Down

0 comments on commit a27447d

Please sign in to comment.