Skip to content

Commit 819900f

Browse files
authored
Python 3 stubs: use str instead of typing.Text (#7638)
1 parent d802e65 commit 819900f

File tree

5 files changed

+56
-45
lines changed

5 files changed

+56
-45
lines changed

stubs/Markdown/markdown/core.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from _typeshed import Self
22
from collections.abc import Callable, Mapping, Sequence
3-
from typing import Any, BinaryIO, ClassVar, Text, TextIO
3+
from typing import Any, BinaryIO, ClassVar, TextIO
44
from typing_extensions import Literal
55
from xml.etree.ElementTree import Element
66

@@ -15,9 +15,9 @@ class Markdown:
1515
postprocessors: Registry
1616
parser: BlockParser
1717
htmlStash: HtmlStash
18-
output_formats: ClassVar[dict[Literal["xhtml", "html"], Callable[[Element], Text]]]
18+
output_formats: ClassVar[dict[Literal["xhtml", "html"], Callable[[Element], str]]]
1919
output_format: Literal["xhtml", "html"]
20-
serializer: Callable[[Element], Text]
20+
serializer: Callable[[Element], str]
2121
tab_length: int
2222
block_level_elements: list[str]
2323
def __init__(
@@ -30,12 +30,12 @@ class Markdown:
3030
) -> None: ...
3131
def build_parser(self) -> Markdown: ...
3232
def registerExtensions(self, extensions: Sequence[Extension | str], configs: Mapping[str, Mapping[str, Any]]) -> Markdown: ...
33-
def build_extension(self, ext_name: Text, configs: Mapping[str, str]) -> Extension: ...
33+
def build_extension(self, ext_name: str, configs: Mapping[str, str]) -> Extension: ...
3434
def registerExtension(self, extension: Extension) -> Markdown: ...
3535
def reset(self: Self) -> Self: ...
3636
def set_output_format(self, format: Literal["xhtml", "html"]) -> Markdown: ...
3737
def is_block_level(self, tag: str) -> bool: ...
38-
def convert(self, source: Text) -> Text: ...
38+
def convert(self, source: str) -> str: ...
3939
def convertFile(
4040
self,
4141
input: str | TextIO | BinaryIO | None = ...,
@@ -44,13 +44,13 @@ class Markdown:
4444
) -> Markdown: ...
4545

4646
def markdown(
47-
text: Text,
47+
text: str,
4848
*,
4949
extensions: Sequence[str | Extension] | None = ...,
5050
extension_configs: Mapping[str, Mapping[str, Any]] | None = ...,
5151
output_format: Literal["xhtml", "html"] | None = ...,
5252
tab_length: int | None = ...,
53-
) -> Text: ...
53+
) -> str: ...
5454
def markdownFromFile(
5555
*,
5656
input: str | TextIO | BinaryIO | None = ...,

stubs/PyMySQL/pymysql/cursors.pyi

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from _typeshed import Self
22
from collections.abc import Iterable, Iterator
3-
from typing import Any, Text
3+
from typing import Any
44

55
from .connections import Connection
66

77
class Cursor:
88
connection: Connection[Any]
9-
description: tuple[Text, ...]
9+
description: tuple[str, ...]
1010
rownumber: int
1111
rowcount: int
1212
arraysize: int
@@ -19,11 +19,11 @@ class Cursor:
1919
def setinputsizes(self, *args) -> None: ...
2020
def setoutputsizes(self, *args) -> None: ...
2121
def nextset(self) -> bool | None: ...
22-
def mogrify(self, query: Text, args: object = ...) -> str: ...
23-
def execute(self, query: Text, args: object = ...) -> int: ...
24-
def executemany(self, query: Text, args: Iterable[object]) -> int | None: ...
25-
def callproc(self, procname: Text, args: Iterable[Any] = ...) -> Any: ...
26-
def scroll(self, value: int, mode: Text = ...) -> None: ...
22+
def mogrify(self, query: str, args: object = ...) -> str: ...
23+
def execute(self, query: str, args: object = ...) -> int: ...
24+
def executemany(self, query: str, args: Iterable[object]) -> int | None: ...
25+
def callproc(self, procname: str, args: Iterable[Any] = ...) -> Any: ...
26+
def scroll(self, value: int, mode: str = ...) -> None: ...
2727
def __enter__(self: Self) -> Self: ...
2828
def __exit__(self, *exc_info: object) -> None: ...
2929
# Methods returning result tuples are below.
@@ -34,17 +34,17 @@ class Cursor:
3434

3535
class DictCursorMixin:
3636
dict_type: Any # TODO: add support if someone needs this
37-
def fetchone(self) -> dict[Text, Any] | None: ...
38-
def fetchmany(self, size: int | None = ...) -> tuple[dict[Text, Any], ...]: ...
39-
def fetchall(self) -> tuple[dict[Text, Any], ...]: ...
40-
def __iter__(self) -> Iterator[dict[Text, Any]]: ...
37+
def fetchone(self) -> dict[str, Any] | None: ...
38+
def fetchmany(self, size: int | None = ...) -> tuple[dict[str, Any], ...]: ...
39+
def fetchall(self) -> tuple[dict[str, Any], ...]: ...
40+
def __iter__(self) -> Iterator[dict[str, Any]]: ...
4141

4242
class SSCursor(Cursor):
4343
def fetchall(self) -> list[tuple[Any, ...]]: ... # type: ignore[override]
4444
def fetchall_unbuffered(self) -> Iterator[tuple[Any, ...]]: ...
45-
def scroll(self, value: int, mode: Text = ...) -> None: ...
45+
def scroll(self, value: int, mode: str = ...) -> None: ...
4646

4747
class DictCursor(DictCursorMixin, Cursor): ... # type: ignore[misc]
4848

4949
class SSDictCursor(DictCursorMixin, SSCursor): # type: ignore[misc]
50-
def fetchall_unbuffered(self) -> Iterator[dict[Text, Any]]: ... # type: ignore[override]
50+
def fetchall_unbuffered(self) -> Iterator[dict[str, Any]]: ... # type: ignore[override]

stubs/decorator/decorator.pyi

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from collections.abc import Callable, Iterator
3-
from typing import Any, NamedTuple, Pattern, Text, TypeVar
3+
from typing import Any, NamedTuple, Pattern, TypeVar
44
from typing_extensions import ParamSpec
55

66
_C = TypeVar("_C", bound=Callable[..., Any])
@@ -34,42 +34,42 @@ DEF: Pattern[str]
3434
_dict = dict # conflicts with attribute name
3535

3636
class FunctionMaker:
37-
args: list[Text]
38-
varargs: Text | None
39-
varkw: Text | None
37+
args: list[str]
38+
varargs: str | None
39+
varkw: str | None
4040
defaults: tuple[Any, ...]
41-
kwonlyargs: list[Text]
42-
kwonlydefaults: Text | None
43-
shortsignature: Text | None
44-
name: Text
45-
doc: Text | None
46-
module: Text | None
47-
annotations: _dict[Text, Any]
48-
signature: Text
49-
dict: _dict[Text, Any]
41+
kwonlyargs: list[str]
42+
kwonlydefaults: str | None
43+
shortsignature: str | None
44+
name: str
45+
doc: str | None
46+
module: str | None
47+
annotations: _dict[str, Any]
48+
signature: str
49+
dict: _dict[str, Any]
5050
def __init__(
5151
self,
5252
func: Callable[..., Any] | None = ...,
53-
name: Text | None = ...,
54-
signature: Text | None = ...,
53+
name: str | None = ...,
54+
signature: str | None = ...,
5555
defaults: tuple[Any, ...] | None = ...,
56-
doc: Text | None = ...,
57-
module: Text | None = ...,
58-
funcdict: _dict[Text, Any] | None = ...,
56+
doc: str | None = ...,
57+
module: str | None = ...,
58+
funcdict: _dict[str, Any] | None = ...,
5959
) -> None: ...
6060
def update(self, func: Any, **kw: Any) -> None: ...
6161
def make(
62-
self, src_templ: Text, evaldict: _dict[Text, Any] | None = ..., addsource: bool = ..., **attrs: Any
62+
self, src_templ: str, evaldict: _dict[str, Any] | None = ..., addsource: bool = ..., **attrs: Any
6363
) -> Callable[..., Any]: ...
6464
@classmethod
6565
def create(
6666
cls,
6767
obj: Any,
68-
body: Text,
69-
evaldict: _dict[Text, Any],
68+
body: str,
69+
evaldict: _dict[str, Any],
7070
defaults: tuple[Any, ...] | None = ...,
71-
doc: Text | None = ...,
72-
module: Text | None = ...,
71+
doc: str | None = ...,
72+
module: str | None = ...,
7373
addsource: bool = ...,
7474
**attrs: Any,
7575
) -> Callable[..., Any]: ...

stubs/pycurl/pycurl.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Text
1+
from typing import Any
22
from typing_extensions import final
33

44
GLOBAL_ACK_EINTR: int
@@ -24,7 +24,7 @@ class Curl:
2424
def setopt_string(self, option: int, value: str) -> None: ...
2525
def perform(self) -> None: ...
2626
def perform_rb(self) -> bytes: ...
27-
def perform_rs(self) -> Text: ...
27+
def perform_rs(self) -> str: ...
2828
def getinfo(self, info: Any) -> Any: ...
2929
def getinfo_raw(self, info: Any) -> Any: ...
3030
def reset(self) -> None: ...

tests/check_new_syntax.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
7979
)
8080
self.generic_visit(node)
8181

82+
class TextFinder(ast.NodeVisitor):
83+
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
84+
if node.module == "typing" and any(thing.name == "Text" for thing in node.names):
85+
errors.append(f"{path}:{node.lineno}: Use `str` instead of `typing.Text` in a Python-3-only stub.")
86+
87+
def visit_Attribute(self, node: ast.Attribute) -> None:
88+
if isinstance(node.value, ast.Name) and node.value.id == "typing" and node.attr == "Text":
89+
errors.append(f"{path}:{node.lineno}: Use `str` instead of `typing.Text` in a Python-3-only stub.")
90+
8291
class IfFinder(ast.NodeVisitor):
8392
def visit_If(self, node: ast.If) -> None:
8493
if (
@@ -96,6 +105,8 @@ def visit_If(self, node: ast.If) -> None:
96105

97106
if not python_2_support_required:
98107
ObjectClassdefFinder().visit(tree)
108+
if path != Path("stdlib/typing_extensions.pyi"):
109+
TextFinder().visit(tree)
99110

100111
OldSyntaxFinder().visit(tree)
101112
IfFinder().visit(tree)

0 commit comments

Comments
 (0)