Use MaybeNone (alias to Any) when applicable (#12855)

This commit is contained in:
Oleh Prypin
2024-10-18 23:07:52 +02:00
committed by GitHub
parent 7fb84668fc
commit b2f68ec2fe
10 changed files with 43 additions and 41 deletions

View File

@@ -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
@@ -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]
@@ -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]: ...

View File

@@ -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__ = [
@@ -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,
@@ -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,

View File

@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,
@@ -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,

View File

@@ -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
@@ -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

View File

@@ -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
@@ -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: ...
@@ -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):
@@ -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]: ...
@@ -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

View File

@@ -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,
@@ -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

View File

@@ -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
@@ -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):

View File

@@ -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
@@ -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, ...],
@@ -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]: ...

View File

@@ -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
@@ -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

View File

@@ -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,
@@ -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