Skip to content

Commit

Permalink
Use MaybeNone (alias to Any) when applicable (#12855)
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin authored Oct 18, 2024
1 parent 7fb8466 commit b2f68ec
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
13 changes: 7 additions & 6 deletions stdlib/email/message.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import MaybeNone
from collections.abc import Generator, Iterator, Sequence
from email import _ParamsType, _ParamType
from email.charset import Charset
Expand Down Expand Up @@ -42,17 +43,17 @@ class Message(Generic[_HeaderT, _HeaderParamT]):
def get_unixfrom(self) -> str | None: ...
def attach(self, payload: _PayloadType) -> None: ...
# `i: int` without a multipart payload results in an error
# `| Any`: can be None for cleared or unset payload, but annoying to check
# `| MaybeNone` acts like `| Any`: can be None for cleared or unset payload, but annoying to check
@overload # multipart
def get_payload(self, i: int, decode: Literal[True]) -> None: ...
@overload # multipart
def get_payload(self, i: int, decode: Literal[False] = False) -> _PayloadType | Any: ...
def get_payload(self, i: int, decode: Literal[False] = False) -> _PayloadType | MaybeNone: ...
@overload # either
def get_payload(self, i: None = None, decode: Literal[False] = False) -> _PayloadType | _MultipartPayloadType | Any: ...
def get_payload(self, i: None = None, decode: Literal[False] = False) -> _PayloadType | _MultipartPayloadType | MaybeNone: ...
@overload # not multipart
def get_payload(self, i: None = None, *, decode: Literal[True]) -> _EncodedPayloadType | Any: ...
def get_payload(self, i: None = None, *, decode: Literal[True]) -> _EncodedPayloadType | MaybeNone: ...
@overload # not multipart, IDEM but w/o kwarg
def get_payload(self, i: None, decode: Literal[True]) -> _EncodedPayloadType | Any: ...
def get_payload(self, i: None, decode: Literal[True]) -> _EncodedPayloadType | MaybeNone: ...
# If `charset=None` and payload supports both `encode` AND `decode`,
# then an invalid payload could be passed, but this is unlikely
# Not[_SupportsEncodeToPayload]
Expand All @@ -75,7 +76,7 @@ class Message(Generic[_HeaderT, _HeaderParamT]):
# This is important for protocols using __getitem__, like SupportsKeysAndGetItem
# Morally, the return type should be `AnyOf[_HeaderType, None]`,
# so using "the Any trick" instead.
def __getitem__(self, name: str) -> _HeaderT | Any: ...
def __getitem__(self, name: str) -> _HeaderT | MaybeNone: ...
def __setitem__(self, name: str, val: _HeaderParamT) -> None: ...
def __delitem__(self, name: str) -> None: ...
def keys(self) -> list[str]: ...
Expand Down
8 changes: 4 additions & 4 deletions stdlib/http/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import io
import ssl
import sys
import types
from _typeshed import ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer
from _typeshed import MaybeNone, ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer
from collections.abc import Callable, Iterable, Iterator, Mapping
from socket import socket
from typing import Any, BinaryIO, TypeVar, overload
from typing import BinaryIO, TypeVar, overload
from typing_extensions import Self, TypeAlias

__all__ = [
Expand Down Expand Up @@ -154,7 +154,7 @@ class HTTPConnection:
timeout: float | None
host: str
port: int
sock: socket | Any # can be `None` if `.connect()` was not called
sock: socket | MaybeNone # can be `None` if `.connect()` was not called
def __init__(
self,
host: str,
Expand Down Expand Up @@ -187,7 +187,7 @@ class HTTPConnection:

class HTTPSConnection(HTTPConnection):
# Can be `None` if `.connect()` was not called:
sock: ssl.SSLSocket | Any
sock: ssl.SSLSocket | MaybeNone
if sys.version_info >= (3, 12):
def __init__(
self,
Expand Down
11 changes: 6 additions & 5 deletions stdlib/itertools.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from _typeshed import MaybeNone
from collections.abc import Callable, Iterable, Iterator
from typing import Any, Generic, Literal, SupportsComplex, SupportsFloat, SupportsIndex, SupportsInt, TypeVar, overload
from typing_extensions import Self, TypeAlias
Expand Down Expand Up @@ -122,7 +123,7 @@ class zip_longest(Generic[_T_co]):
# In the overloads without fillvalue, all of the tuple members could theoretically be None,
# but we return Any instead to avoid false positives for code where we know one of the iterables
# is longer.
def __new__(cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /) -> zip_longest[tuple[_T1 | Any, _T2 | Any]]: ...
def __new__(cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone]]: ...
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], /, *, fillvalue: _T
Expand All @@ -131,7 +132,7 @@ class zip_longest(Generic[_T_co]):
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any]]: ...
) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone, _T3 | MaybeNone]]: ...
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], /, *, fillvalue: _T
Expand All @@ -140,7 +141,7 @@ class zip_longest(Generic[_T_co]):
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], /
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any, _T4 | Any]]: ...
) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone, _T3 | MaybeNone, _T4 | MaybeNone]]: ...
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], /, *, fillvalue: _T
Expand All @@ -149,7 +150,7 @@ class zip_longest(Generic[_T_co]):
@overload
def __new__(
cls, iter1: Iterable[_T1], iter2: Iterable[_T2], iter3: Iterable[_T3], iter4: Iterable[_T4], iter5: Iterable[_T5], /
) -> zip_longest[tuple[_T1 | Any, _T2 | Any, _T3 | Any, _T4 | Any, _T5 | Any]]: ...
) -> zip_longest[tuple[_T1 | MaybeNone, _T2 | MaybeNone, _T3 | MaybeNone, _T4 | MaybeNone, _T5 | MaybeNone]]: ...
@overload
def __new__(
cls,
Expand All @@ -174,7 +175,7 @@ class zip_longest(Generic[_T_co]):
iter6: Iterable[_T],
/,
*iterables: Iterable[_T],
) -> zip_longest[tuple[_T | Any, ...]]: ...
) -> zip_longest[tuple[_T | MaybeNone, ...]]: ...
@overload
def __new__(
cls,
Expand Down
4 changes: 2 additions & 2 deletions stdlib/optparse.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _typeshed import Incomplete
from _typeshed import Incomplete, MaybeNone
from abc import abstractmethod
from collections.abc import Callable, Iterable, Mapping, Sequence
from typing import IO, Any, AnyStr, Literal, NoReturn, overload
Expand Down Expand Up @@ -56,7 +56,7 @@ class HelpFormatter:
current_indent: int
default_tag: str
help_position: int
help_width: int | Any # initialized as None and computed later as int when storing option strings
help_width: int | MaybeNone # initialized as None and computed later as int when storing option strings
indent_increment: int
level: int
max_help_position: int
Expand Down
22 changes: 11 additions & 11 deletions stdlib/re.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import enum
import sre_compile
import sre_constants
import sys
from _typeshed import ReadableBuffer
from _typeshed import MaybeNone, ReadableBuffer
from collections.abc import Callable, Iterator, Mapping
from typing import Any, AnyStr, Generic, Literal, TypeVar, final, overload
from typing_extensions import TypeAlias
Expand Down Expand Up @@ -90,19 +90,19 @@ class Match(Generic[AnyStr]):
@overload
def group(self, group: Literal[0] = 0, /) -> AnyStr: ...
@overload
def group(self, group: str | int, /) -> AnyStr | Any: ...
def group(self, group: str | int, /) -> AnyStr | MaybeNone: ...
@overload
def group(self, group1: str | int, group2: str | int, /, *groups: str | int) -> tuple[AnyStr | Any, ...]: ...
def group(self, group1: str | int, group2: str | int, /, *groups: str | int) -> tuple[AnyStr | MaybeNone, ...]: ...
# Each item of groups()'s return tuple is either "AnyStr" or
# "AnyStr | None", depending on the pattern.
@overload
def groups(self) -> tuple[AnyStr | Any, ...]: ...
def groups(self) -> tuple[AnyStr | MaybeNone, ...]: ...
@overload
def groups(self, default: _T) -> tuple[AnyStr | _T, ...]: ...
# Each value in groupdict()'s return dict is either "AnyStr" or
# "AnyStr | None", depending on the pattern.
@overload
def groupdict(self) -> dict[str, AnyStr | Any]: ...
def groupdict(self) -> dict[str, AnyStr | MaybeNone]: ...
@overload
def groupdict(self, default: _T) -> dict[str, AnyStr | _T]: ...
def start(self, group: int | str = 0, /) -> int: ...
Expand All @@ -114,7 +114,7 @@ class Match(Generic[AnyStr]):
@overload
def __getitem__(self, key: Literal[0], /) -> AnyStr: ...
@overload
def __getitem__(self, key: int | str, /) -> AnyStr | Any: ...
def __getitem__(self, key: int | str, /) -> AnyStr | MaybeNone: ...
def __copy__(self) -> Match[AnyStr]: ...
def __deepcopy__(self, memo: Any, /) -> Match[AnyStr]: ...
if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -151,11 +151,11 @@ class Pattern(Generic[AnyStr]):
@overload
def fullmatch(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Match[AnyStr] | None: ...
@overload
def split(self: Pattern[str], string: str, maxsplit: int = 0) -> list[str | Any]: ...
def split(self: Pattern[str], string: str, maxsplit: int = 0) -> list[str | MaybeNone]: ...
@overload
def split(self: Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0) -> list[bytes | Any]: ...
def split(self: Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0) -> list[bytes | MaybeNone]: ...
@overload
def split(self, string: AnyStr, maxsplit: int = 0) -> list[AnyStr | Any]: ...
def split(self, string: AnyStr, maxsplit: int = 0) -> list[AnyStr | MaybeNone]: ...
# return type depends on the number of groups in the pattern
@overload
def findall(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> list[Any]: ...
Expand Down Expand Up @@ -270,11 +270,11 @@ def fullmatch(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -
@overload
def fullmatch(pattern: bytes | Pattern[bytes], string: ReadableBuffer, flags: _FlagsType = 0) -> Match[bytes] | None: ...
@overload
def split(pattern: str | Pattern[str], string: str, maxsplit: int = 0, flags: _FlagsType = 0) -> list[str | Any]: ...
def split(pattern: str | Pattern[str], string: str, maxsplit: int = 0, flags: _FlagsType = 0) -> list[str | MaybeNone]: ...
@overload
def split(
pattern: bytes | Pattern[bytes], string: ReadableBuffer, maxsplit: int = 0, flags: _FlagsType = 0
) -> list[bytes | Any]: ...
) -> list[bytes | MaybeNone]: ...
@overload
def findall(pattern: str | Pattern[str], string: str, flags: _FlagsType = 0) -> list[Any]: ...
@overload
Expand Down
6 changes: 3 additions & 3 deletions stdlib/sqlite3/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import ReadableBuffer, StrOrBytesPath, SupportsLenAndGetItem, Unused
from _typeshed import MaybeNone, ReadableBuffer, StrOrBytesPath, SupportsLenAndGetItem, Unused
from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Sequence
from sqlite3.dbapi2 import (
PARSE_COLNAMES as PARSE_COLNAMES,
Expand Down Expand Up @@ -401,9 +401,9 @@ class Cursor:
arraysize: int
@property
def connection(self) -> Connection: ...
# May be None, but using | Any instead to avoid slightly annoying false positives.
# May be None, but using `| MaybeNone` (`| Any`) instead to avoid slightly annoying false positives.
@property
def description(self) -> tuple[tuple[str, None, None, None, None, None, None], ...] | Any: ...
def description(self) -> tuple[tuple[str, None, None, None, None, None, None], ...] | MaybeNone: ...
@property
def lastrowid(self) -> int | None: ...
row_factory: Callable[[Cursor, Row], object] | None
Expand Down
4 changes: 2 additions & 2 deletions stdlib/subprocess.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import ReadableBuffer, StrOrBytesPath
from _typeshed import MaybeNone, ReadableBuffer, StrOrBytesPath
from collections.abc import Callable, Collection, Iterable, Mapping, Sequence
from types import TracebackType
from typing import IO, Any, AnyStr, Final, Generic, Literal, TypeVar, overload
Expand Down Expand Up @@ -1848,7 +1848,7 @@ class Popen(Generic[AnyStr]):
stdout: IO[AnyStr] | None
stderr: IO[AnyStr] | None
pid: int
returncode: int | Any
returncode: int | MaybeNone
universal_newlines: bool

if sys.version_info >= (3, 11):
Expand Down
6 changes: 3 additions & 3 deletions stdlib/tkinter/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _tkinter
import sys
from _typeshed import Incomplete, StrEnum, StrOrBytesPath
from _typeshed import Incomplete, MaybeNone, StrEnum, StrOrBytesPath
from collections.abc import Callable, Iterable, Mapping, Sequence
from tkinter.constants import *
from tkinter.font import _FontDescription
Expand Down Expand Up @@ -509,7 +509,7 @@ class Misc:
pad: _ScreenUnits = ...,
uniform: str = ...,
weight: int = ...,
) -> _GridIndexInfo | Any: ... # can be None but annoying to check
) -> _GridIndexInfo | MaybeNone: ... # can be None but annoying to check
def grid_rowconfigure(
self,
index: int | str | list[int] | tuple[int, ...],
Expand All @@ -519,7 +519,7 @@ class Misc:
pad: _ScreenUnits = ...,
uniform: str = ...,
weight: int = ...,
) -> _GridIndexInfo | Any: ... # can be None but annoying to check
) -> _GridIndexInfo | MaybeNone: ... # can be None but annoying to check
columnconfigure = grid_columnconfigure
rowconfigure = grid_rowconfigure
def grid_location(self, x: _ScreenUnits, y: _ScreenUnits) -> tuple[int, int]: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/tkinter/ttk.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _tkinter
import tkinter
from _typeshed import Incomplete
from _typeshed import Incomplete, MaybeNone
from collections.abc import Callable
from tkinter.font import _FontDescription
from typing import Any, Literal, TypedDict, overload
Expand Down Expand Up @@ -1156,7 +1156,7 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
background: str = ...,
font: _FontDescription = ...,
image: tkinter._ImageSpec = ...,
) -> _TreeviewTagDict | Any: ... # can be None but annoying to check
) -> _TreeviewTagDict | MaybeNone: ... # can be None but annoying to check
@overload
def tag_has(self, tagname: str, item: None = None) -> tuple[str, ...]: ...
@overload
Expand Down
6 changes: 3 additions & 3 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import SupportsKeysAndGetItem
from _typeshed import MaybeNone, SupportsKeysAndGetItem
from _typeshed.importlib import LoaderProtocol
from collections.abc import (
AsyncGenerator,
Expand Down Expand Up @@ -528,9 +528,9 @@ class FrameType:
def f_lasti(self) -> int: ...
# see discussion in #6769: f_lineno *can* sometimes be None,
# but you should probably file a bug report with CPython if you encounter it being None in the wild.
# An `int | None` annotation here causes too many false-positive errors.
# An `int | None` annotation here causes too many false-positive errors, so applying `int | Any`.
@property
def f_lineno(self) -> int | Any: ...
def f_lineno(self) -> int | MaybeNone: ...
@property
def f_locals(self) -> dict[str, Any]: ...
f_trace: Callable[[FrameType, str, Any], Any] | None
Expand Down

0 comments on commit b2f68ec

Please sign in to comment.