Skip to content

Commit 66dde14

Browse files
authored
stubgen: Fix valid type detection to allow pipe unions (#18726)
<!-- If this pull request fixes an issue, add "Fixes #NNN" with the issue number. --> `stubgen` has a regex which it uses to reject invalid types that are extracted from docstrings. It needed to be updated to support union shorthand: `str | int`. <!-- 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 7914b2d commit 66dde14

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

mypy/stubdoc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Sig: _TypeAlias = tuple[str, str]
2222

2323

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

2727

mypy/test/teststubgen.py

+3
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,9 @@ def test_is_valid_type(self) -> None:
14051405
assert is_valid_type("Literal[True]")
14061406
assert is_valid_type("Literal[Color.RED]")
14071407
assert is_valid_type("Literal[None]")
1408+
assert is_valid_type("str | int")
1409+
assert is_valid_type("dict[str, int] | int")
1410+
assert is_valid_type("tuple[str, ...]")
14081411
assert is_valid_type(
14091412
'Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]'
14101413
)

0 commit comments

Comments
 (0)