Skip to content

Commit 8f29336

Browse files
committed
Use MaybeNone (alias to Any) when applicable
1 parent 7fb8466 commit 8f29336

File tree

10 files changed

+43
-41
lines changed

10 files changed

+43
-41
lines changed

stdlib/email/message.pyi

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import MaybeNone
12
from collections.abc import Generator, Iterator, Sequence
23
from email import _ParamsType, _ParamType
34
from email.charset import Charset
@@ -42,17 +43,17 @@ class Message(Generic[_HeaderT, _HeaderParamT]):
4243
def get_unixfrom(self) -> str | None: ...
4344
def attach(self, payload: _PayloadType) -> None: ...
4445
# `i: int` without a multipart payload results in an error
45-
# `| Any`: can be None for cleared or unset payload, but annoying to check
46+
# `| MaybeNone` acts like `| Any`: can be None for cleared or unset payload, but annoying to check
4647
@overload # multipart
4748
def get_payload(self, i: int, decode: Literal[True]) -> None: ...
4849
@overload # multipart
49-
def get_payload(self, i: int, decode: Literal[False] = False) -> _PayloadType | Any: ...
50+
def get_payload(self, i: int, decode: Literal[False] = False) -> _PayloadType | MaybeNone: ...
5051
@overload # either
51-
def get_payload(self, i: None = None, decode: Literal[False] = False) -> _PayloadType | _MultipartPayloadType | Any: ...
52+
def get_payload(self, i: None = None, decode: Literal[False] = False) -> _PayloadType | _MultipartPayloadType | MaybeNone: ...
5253
@overload # not multipart
53-
def get_payload(self, i: None = None, *, decode: Literal[True]) -> _EncodedPayloadType | Any: ...
54+
def get_payload(self, i: None = None, *, decode: Literal[True]) -> _EncodedPayloadType | MaybeNone: ...
5455
@overload # not multipart, IDEM but w/o kwarg
55-
def get_payload(self, i: None, decode: Literal[True]) -> _EncodedPayloadType | Any: ...
56+
def get_payload(self, i: None, decode: Literal[True]) -> _EncodedPayloadType | MaybeNone: ...
5657
# If `charset=None` and payload supports both `encode` AND `decode`,
5758
# then an invalid payload could be passed, but this is unlikely
5859
# Not[_SupportsEncodeToPayload]
@@ -75,7 +76,7 @@ class Message(Generic[_HeaderT, _HeaderParamT]):
7576
# This is important for protocols using __getitem__, like SupportsKeysAndGetItem
7677
# Morally, the return type should be `AnyOf[_HeaderType, None]`,
7778
# so using "the Any trick" instead.
78-
def __getitem__(self, name: str) -> _HeaderT | Any: ...
79+
def __getitem__(self, name: str) -> _HeaderT | MaybeNone: ...
7980
def __setitem__(self, name: str, val: _HeaderParamT) -> None: ...
8081
def __delitem__(self, name: str) -> None: ...
8182
def keys(self) -> list[str]: ...

stdlib/http/client.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import io
33
import ssl
44
import sys
55
import types
6-
from _typeshed import ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer
6+
from _typeshed import MaybeNone, ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer
77
from collections.abc import Callable, Iterable, Iterator, Mapping
88
from socket import socket
9-
from typing import Any, BinaryIO, TypeVar, overload
9+
from typing import BinaryIO, TypeVar, overload
1010
from typing_extensions import Self, TypeAlias
1111

1212
__all__ = [
@@ -154,7 +154,7 @@ class HTTPConnection:
154154
timeout: float | None
155155
host: str
156156
port: int
157-
sock: socket | Any # can be `None` if `.connect()` was not called
157+
sock: socket | MaybeNone # can be `None` if `.connect()` was not called
158158
def __init__(
159159
self,
160160
host: str,
@@ -187,7 +187,7 @@ class HTTPConnection:
187187

188188
class HTTPSConnection(HTTPConnection):
189189
# Can be `None` if `.connect()` was not called:
190-
sock: ssl.SSLSocket | Any
190+
sock: ssl.SSLSocket | MaybeNone
191191
if sys.version_info >= (3, 12):
192192
def __init__(
193193
self,

stdlib/itertools.pyi

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import MaybeNone
23
from collections.abc import Callable, Iterable, Iterator
34
from typing import Any, Generic, Literal, SupportsComplex, SupportsFloat, SupportsIndex, SupportsInt, TypeVar, overload
45
from typing_extensions import Self, TypeAlias
@@ -122,7 +123,7 @@ class zip_longest(Generic[_T_co]):
122123
# In the overloads without fillvalue, all of the tuple members could theoretically be None,
123124
# but we return Any instead to avoid false positives for code where we know one of the iterables
124125
# is longer.
125-
def __new__(cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /) -> zip_longest[tuple[_T1 | Any, _T2 | Any]]: ...
126+
def __new__(cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone]]: ...
126127
@overload
127128
def __new__(
128129
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /, *, fillvalue: _T
@@ -131,7 +132,7 @@ class zip_longest(Generic[_T_co]):
131132
@overload
132133
def __new__(
133134
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /
134-
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any]]: ...
135+
) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone, _T3 | MaybeNone]]: ...
135136
@overload
136137
def __new__(
137138
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /, *, fillvalue: _T
@@ -140,7 +141,7 @@ class zip_longest(Generic[_T_co]):
140141
@overload
141142
def __new__(
142143
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], /
143-
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any, _T4 | Any]]: ...
144+
) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone, _T3 | MaybeNone, _T4 | MaybeNone]]: ...
144145
@overload
145146
def __new__(
146147
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], /, *, fillvalue: _T
@@ -149,7 +150,7 @@ class zip_longest(Generic[_T_co]):
149150
@overload
150151
def __new__(
151152
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], iter5: Iterable[_T5], /
152-
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any, _T4 | Any, _T5 | Any]]: ...
153+
) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone, _T3 | MaybeNone, _T4 | MaybeNone, _T5 | MaybeNone]]: ...
153154
@overload
154155
def __new__(
155156
cls,
@@ -174,7 +175,7 @@ class zip_longest(Generic[_T_co]):
174175
iter6: Iterable[_T],
175176
/,
176177
*iterables: Iterable[_T],
177-
) -> zip_longest[tuple[_T | Any, ...]]: ...
178+
) -> zip_longest[tuple[_T | MaybeNone, ...]]: ...
178179
@overload
179180
def __new__(
180181
cls,

stdlib/optparse.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, MaybeNone
22
from abc import abstractmethod
33
from collections.abc import Callable, Iterable, Mapping, Sequence
44
from typing import IO, Any, AnyStr, Literal, NoReturn, overload
@@ -56,7 +56,7 @@ class HelpFormatter:
5656
current_indent: int
5757
default_tag: str
5858
help_position: int
59-
help_width: int | Any # initialized as None and computed later as int when storing option strings
59+
help_width: int | MaybeNone # initialized as None and computed later as int when storing option strings
6060
indent_increment: int
6161
level: int
6262
max_help_position: int

stdlib/re.pyi

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import enum
22
import sre_compile
33
import sre_constants
44
import sys
5-
from _typeshed import ReadableBuffer
5+
from _typeshed import MaybeNone, ReadableBuffer
66
from collections.abc import Callable, Iterator, Mapping
77
from typing import Any, AnyStr, Generic, Literal, TypeVar, final, overload
88
from typing_extensions import TypeAlias
@@ -90,19 +90,19 @@ class Match(Generic[AnyStr]):
9090
@overload
9191
def group(self, group: Literal[0] = 0, /) -> AnyStr: ...
9292
@overload
93-
def group(self, group: str | int, /) -> AnyStr | Any: ...
93+
def group(self, group: str | int, /) -> AnyStr | MaybeNone: ...
9494
@overload
95-
def group(self, group1: str | int, group2: str | int, /, *groups: str | int) -> tuple[AnyStr | Any, ...]: ...
95+
def group(self, group1: str | int, group2: str | int, /, *groups: str | int) -> tuple[AnyStr | MaybeNone, ...]: ...
9696
# Each item of groups()'s return tuple is either "AnyStr" or
9797
# "AnyStr | None", depending on the pattern.
9898
@overload
99-
def groups(self) -> tuple[AnyStr | Any, ...]: ...
99+
def groups(self) -> tuple[AnyStr | MaybeNone, ...]: ...
100100
@overload
101101
def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ...
102102
# Each value in groupdict()'s return dict is either "AnyStr" or
103103
# "AnyStr | None", depending on the pattern.
104104
@overload
105-
def groupdict(self) -> dict[str, AnyStr | Any]: ...
105+
def groupdict(self) -> dict[str, AnyStr | MaybeNone]: ...
106106
@overload
107107
def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ...
108108
def start(self, group: int | str = 0, /) -> int: ...
@@ -114,7 +114,7 @@ class Match(Generic[AnyStr]):
114114
@overload
115115
def __getitem__(self, key: Literal[0], /) -> AnyStr: ...
116116
@overload
117-
def __getitem__(self, key: int | str, /) -> AnyStr | Any: ...
117+
def __getitem__(self, key: int | str, /) -> AnyStr | MaybeNone: ...
118118
def __copy__(self) -> Match[AnyStr]: ...
119119
def __deepcopy__(self, memo: Any, /) -> Match[AnyStr]: ...
120120
if sys.version_info >= (3, 9):
@@ -151,11 +151,11 @@ class Pattern(Generic[AnyStr]):
151151
@overload
152152
def fullmatch(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Match[AnyStr] | None: ...
153153
@overload
154-
def split(self: Pattern[str], string: str, maxsplit: int = 0) -> list[str | Any]: ...
154+
def split(self: Pattern[str], string: str, maxsplit: int = 0) -> list[str | MaybeNone]: ...
155155
@overload
156-
def split(self: Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0) -> list[bytes | Any]: ...
156+
def split(self: Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0) -> list[bytes | MaybeNone]: ...
157157
@overload
158-
def split(self, string: AnyStr, maxsplit: int = 0) -> list[AnyStr | Any]: ...
158+
def split(self, string: AnyStr, maxsplit: int = 0) -> list[AnyStr | MaybeNone]: ...
159159
# return type depends on the number of groups in the pattern
160160
@overload
161161
def findall(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> list[Any]: ...
@@ -270,11 +270,11 @@ def fullmatch(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -
270270
@overload
271271
def fullmatch(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> Match[bytes] | None: ...
272272
@overload
273-
def split(pattern: str | Pattern[str], string: str, maxsplit: int = 0, flags: _FlagsType = 0) -> list[str | Any]: ...
273+
def split(pattern: str | Pattern[str], string: str, maxsplit: int = 0, flags: _FlagsType = 0) -> list[str | MaybeNone]: ...
274274
@overload
275275
def split(
276276
pattern: bytes | Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0, flags: _FlagsType = 0
277-
) -> list[bytes | Any]: ...
277+
) -> list[bytes | MaybeNone]: ...
278278
@overload
279279
def findall(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> list[Any]: ...
280280
@overload

stdlib/sqlite3/__init__.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import ReadableBuffer, StrOrBytesPath, SupportsLenAndGetItem, Unused
2+
from _typeshed import MaybeNone, ReadableBuffer, StrOrBytesPath, SupportsLenAndGetItem, Unused
33
from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Sequence
44
from sqlite3.dbapi2 import (
55
PARSE_COLNAMES as PARSE_COLNAMES,
@@ -401,9 +401,9 @@ class Cursor:
401401
arraysize: int
402402
@property
403403
def connection(self) -> Connection: ...
404-
# May be None, but using | Any instead to avoid slightly annoying false positives.
404+
# May be None, but using `| MaybeNone` (`| Any`) instead to avoid slightly annoying false positives.
405405
@property
406-
def description(self) -> tuple[tuple[str, None, None, None, None, None, None], ...] | Any: ...
406+
def description(self) -> tuple[tuple[str, None, None, None, None, None, None], ...] | MaybeNone: ...
407407
@property
408408
def lastrowid(self) -> int | None: ...
409409
row_factory: Callable[[Cursor, Row], object] | None

stdlib/subprocess.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import ReadableBuffer, StrOrBytesPath
2+
from _typeshed import MaybeNone, ReadableBuffer, StrOrBytesPath
33
from collections.abc import Callable, Collection, Iterable, Mapping, Sequence
44
from types import TracebackType
55
from typing import IO, Any, AnyStr, Final, Generic, Literal, TypeVar, overload
@@ -1848,7 +1848,7 @@ class Popen(Generic[AnyStr]):
18481848
stdout: IO[AnyStr] | None
18491849
stderr: IO[AnyStr] | None
18501850
pid: int
1851-
returncode: int | Any
1851+
returncode: int | MaybeNone
18521852
universal_newlines: bool
18531853

18541854
if sys.version_info >= (3, 11):

stdlib/tkinter/__init__.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _tkinter
22
import sys
3-
from _typeshed import Incomplete, StrEnum, StrOrBytesPath
3+
from _typeshed import Incomplete, MaybeNone, StrEnum, StrOrBytesPath
44
from collections.abc import Callable, Iterable, Mapping, Sequence
55
from tkinter.constants import *
66
from tkinter.font import _FontDescription
@@ -509,7 +509,7 @@ class Misc:
509509
pad: _ScreenUnits = ...,
510510
uniform: str = ...,
511511
weight: int = ...,
512-
) -> _GridIndexInfo | Any: ... # can be None but annoying to check
512+
) -> _GridIndexInfo | MaybeNone: ... # can be None but annoying to check
513513
def grid_rowconfigure(
514514
self,
515515
index: int | str | list[int] | tuple[int, ...],
@@ -519,7 +519,7 @@ class Misc:
519519
pad: _ScreenUnits = ...,
520520
uniform: str = ...,
521521
weight: int = ...,
522-
) -> _GridIndexInfo | Any: ... # can be None but annoying to check
522+
) -> _GridIndexInfo | MaybeNone: ... # can be None but annoying to check
523523
columnconfigure = grid_columnconfigure
524524
rowconfigure = grid_rowconfigure
525525
def grid_location(self, x: _ScreenUnits, y: _ScreenUnits) -> tuple[int, int]: ...

stdlib/tkinter/ttk.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _tkinter
22
import tkinter
3-
from _typeshed import Incomplete
3+
from _typeshed import Incomplete, MaybeNone
44
from collections.abc import Callable
55
from tkinter.font import _FontDescription
66
from typing import Any, Literal, TypedDict, overload
@@ -1156,7 +1156,7 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
11561156
background: str = ...,
11571157
font: _FontDescription = ...,
11581158
image: tkinter._ImageSpec = ...,
1159-
) -> _TreeviewTagDict | Any: ... # can be None but annoying to check
1159+
) -> _TreeviewTagDict | MaybeNone: ... # can be None but annoying to check
11601160
@overload
11611161
def tag_has(self, tagname: str, item: None = None) -> tuple[str, ...]: ...
11621162
@overload

stdlib/types.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import SupportsKeysAndGetItem
2+
from _typeshed import MaybeNone, SupportsKeysAndGetItem
33
from _typeshed.importlib import LoaderProtocol
44
from collections.abc import (
55
AsyncGenerator,
@@ -528,9 +528,9 @@ class FrameType:
528528
def f_lasti(self) -> int: ...
529529
# see discussion in #6769: f_lineno *can* sometimes be None,
530530
# but you should probably file a bug report with CPython if you encounter it being None in the wild.
531-
# An `int | None` annotation here causes too many false-positive errors.
531+
# An `int | None` annotation here causes too many false-positive errors, so applying `int | Any`.
532532
@property
533-
def f_lineno(self) -> int | Any: ...
533+
def f_lineno(self) -> int | MaybeNone: ...
534534
@property
535535
def f_locals(self) -> dict[str, Any]: ...
536536
f_trace: Callable[[FrameType, str, Any], Any] | None

0 commit comments

Comments
 (0)