Skip to content

Commit 320b883

Browse files
committed
Typeshed cherry-pick: stdlib/xml: fix return types for toxml/toprettyxml methods (#10061)
1 parent 6a68049 commit 320b883

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

mypy/typeshed/stdlib/xml/dom/minidom.pyi

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import xml.dom
33
from _typeshed import Incomplete, ReadableBuffer, SupportsRead, SupportsWrite
4-
from typing import NoReturn, TypeVar
4+
from typing import NoReturn, TypeVar, overload
55
from typing_extensions import Literal, Self
66
from xml.dom.minicompat import NodeList
77
from xml.dom.xmlbuilder import DocumentLS, DOMImplementationLS
@@ -30,13 +30,69 @@ class Node(xml.dom.Node):
3030
def localName(self) -> str | None: ...
3131
def __bool__(self) -> Literal[True]: ...
3232
if sys.version_info >= (3, 9):
33-
def toxml(self, encoding: str | None = None, standalone: bool | None = None) -> str: ...
33+
@overload
34+
def toxml(self, encoding: str, standalone: bool | None = None) -> bytes: ...
35+
@overload
36+
def toxml(self, encoding: None = None, standalone: bool | None = None) -> str: ...
37+
@overload
3438
def toprettyxml(
35-
self, indent: str = "\t", newl: str = "\n", encoding: str | None = None, standalone: bool | None = None
39+
self,
40+
indent: str = "\t",
41+
newl: str = "\n",
42+
# Handle any case where encoding is not provided or where it is passed with None
43+
encoding: None = None,
44+
standalone: bool | None = None,
3645
) -> str: ...
46+
@overload
47+
def toprettyxml(
48+
self,
49+
indent: str,
50+
newl: str,
51+
# Handle cases where encoding is passed as str *positionally*
52+
encoding: str,
53+
standalone: bool | None = None,
54+
) -> bytes: ...
55+
@overload
56+
def toprettyxml(
57+
self,
58+
indent: str = "\t",
59+
newl: str = "\n",
60+
# Handle all cases where encoding is passed as a keyword argument; because standalone
61+
# comes after, it will also have to be a keyword arg if encoding is
62+
*,
63+
encoding: str,
64+
standalone: bool | None = None,
65+
) -> bytes: ...
3766
else:
38-
def toxml(self, encoding: str | None = None): ...
39-
def toprettyxml(self, indent: str = "\t", newl: str = "\n", encoding: str | None = None): ...
67+
@overload
68+
def toxml(self, encoding: str) -> bytes: ...
69+
@overload
70+
def toxml(self, encoding: None = None) -> str: ...
71+
@overload
72+
def toprettyxml(
73+
self,
74+
indent: str = "\t",
75+
newl: str = "\n",
76+
# Handle any case where encoding is not provided or where it is passed with None
77+
encoding: None = None,
78+
) -> str: ...
79+
@overload
80+
def toprettyxml(
81+
self,
82+
indent: str,
83+
newl: str,
84+
# Handle cases where encoding is passed as str *positionally*
85+
encoding: str,
86+
) -> bytes: ...
87+
@overload
88+
def toprettyxml(
89+
self,
90+
indent: str = "\t",
91+
newl: str = "\n",
92+
# Handle all cases where encoding is passed as a keyword argument
93+
*,
94+
encoding: str,
95+
) -> bytes: ...
4096

4197
def hasChildNodes(self) -> bool: ...
4298
def insertBefore(self, newChild, refChild): ...
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from typing_extensions import assert_type
5+
from xml.dom.minidom import Document
6+
7+
document = Document()
8+
9+
assert_type(document.toxml(), str)
10+
assert_type(document.toxml(encoding=None), str)
11+
assert_type(document.toxml(encoding="UTF8"), bytes)
12+
assert_type(document.toxml("UTF8"), bytes)
13+
if sys.version_info >= (3, 9):
14+
assert_type(document.toxml(standalone=True), str)
15+
assert_type(document.toxml("UTF8", True), bytes)
16+
assert_type(document.toxml(encoding="UTF8", standalone=True), bytes)
17+
18+
19+
# Because toprettyxml can mix positional and keyword variants of the "encoding" argument, which
20+
# determines the return type, the proper stub typing isn't immediately obvious. This is a basic
21+
# brute-force sanity check.
22+
# Test cases like toxml
23+
assert_type(document.toprettyxml(), str)
24+
assert_type(document.toprettyxml(encoding=None), str)
25+
assert_type(document.toprettyxml(encoding="UTF8"), bytes)
26+
if sys.version_info >= (3, 9):
27+
assert_type(document.toprettyxml(standalone=True), str)
28+
assert_type(document.toprettyxml(encoding="UTF8", standalone=True), bytes)
29+
# Test cases unique to toprettyxml
30+
assert_type(document.toprettyxml(" "), str)
31+
assert_type(document.toprettyxml(" ", "\r\n"), str)
32+
assert_type(document.toprettyxml(" ", "\r\n", "UTF8"), bytes)
33+
if sys.version_info >= (3, 9):
34+
assert_type(document.toprettyxml(" ", "\r\n", "UTF8", True), bytes)
35+
assert_type(document.toprettyxml(" ", "\r\n", standalone=True), str)

0 commit comments

Comments
 (0)