Skip to content

builtins stubtest exceptions #5219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 106 additions & 79 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,12 @@ class bytes(ByteString):
def zfill(self, __width: int) -> bytes: ...
@classmethod
def fromhex(cls, __s: str) -> bytes: ...
@classmethod
def maketrans(cls, frm: bytes, to: bytes) -> bytes: ...
@staticmethod
def maketrans(__frm: bytes, __to: bytes) -> bytes: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[int]: ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
def __int__(self) -> int: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these were added on purpose to make bytes fulfill the SupportsInt and SupportsFloat protocols. int(b'12') works at runtime. Probably safer to keep them.

Copy link
Contributor Author

@hatal175 hatal175 Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. no mypy primer diffs
  2. the int __new__ function specifically gets bytes (so does float) and treats it as a string: https://github.com/python/cpython/blob/62ec63864822de1dd1933225584e8503ac40c1f9/Objects/longobject.c#L4960
  3. Other functions that receive SupportsInt fail on bytes - itertools.count(b'1',1)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I will merge

def __float__(self) -> float: ...
def __hash__(self) -> int: ...
@overload
def __getitem__(self, i: int) -> int: ...
Expand Down Expand Up @@ -508,6 +506,7 @@ class bytearray(MutableSequence[int], ByteString):
def __init__(self, string: str, encoding: str, errors: str = ...) -> None: ...
@overload
def __init__(self, length: int) -> None: ...
def append(self, __item: int) -> None: ...
def capitalize(self) -> bytearray: ...
def center(self, __width: int, __fillchar: bytes = ...) -> bytearray: ...
def count(self, __sub: Union[bytes, int], __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ...
Expand All @@ -517,6 +516,7 @@ class bytearray(MutableSequence[int], ByteString):
self, __suffix: Union[bytes, Tuple[bytes, ...]], __start: Optional[int] = ..., __end: Optional[int] = ...
) -> bool: ...
def expandtabs(self, tabsize: int = ...) -> bytearray: ...
def extend(self, __iterable_of_ints: Iterable[int]) -> None: ...
def find(self, __sub: Union[bytes, int], __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ...
if sys.version_info >= (3, 8):
def hex(self, sep: Union[str, bytes] = ..., bytes_per_sep: int = ...) -> str: ...
Expand Down Expand Up @@ -561,14 +561,12 @@ class bytearray(MutableSequence[int], ByteString):
def zfill(self, __width: int) -> bytearray: ...
@classmethod
def fromhex(cls, __string: str) -> bytearray: ...
@classmethod
def maketrans(cls, __frm: bytes, __to: bytes) -> bytes: ...
@staticmethod
def maketrans(__frm: bytes, __to: bytes) -> bytes: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[int]: ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
__hash__: None # type: ignore
@overload
def __getitem__(self, i: int) -> int: ...
Expand Down Expand Up @@ -890,6 +888,9 @@ class range(Sequence[int]):
def __reversed__(self) -> Iterator[int]: ...

class property(object):
fget: Optional[Callable[[Any], Any]]
fset: Optional[Callable[[Any, Any], None]]
fdel: Optional[Callable[[Any], None]]
def __init__(
self,
fget: Optional[Callable[[Any], Any]] = ...,
Expand All @@ -903,9 +904,6 @@ class property(object):
def __get__(self, obj: Any, type: Optional[type] = ...) -> Any: ...
def __set__(self, obj: Any, value: Any) -> None: ...
def __delete__(self, obj: Any) -> None: ...
def fget(self) -> Any: ...
def fset(self, value: Any) -> None: ...
def fdel(self) -> None: ...

class _NotImplementedType(Any): # type: ignore
# A little weird, but typing the __call__ as NotImplemented makes the error message
Expand Down Expand Up @@ -973,10 +971,15 @@ def exec(
__locals: Optional[Mapping[str, Any]] = ...,
) -> Any: ...
def exit(code: object = ...) -> NoReturn: ...
@overload
def filter(__function: None, __iterable: Iterable[Optional[_T]]) -> Iterator[_T]: ...
@overload
def filter(__function: Callable[[_T], Any], __iterable: Iterable[_T]) -> Iterator[_T]: ...

class filter(Iterator[_T], Generic[_T]):
@overload
def __init__(self, __function: None, __iterable: Iterable[Optional[_T]]) -> None: ...
@overload
def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ...
def __iter__(self) -> Iterator[_T]: ...
def __next__(self) -> _T: ...

def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
def getattr(__o: Any, name: str, __default: Any = ...) -> Any: ...
def globals() -> Dict[str, Any]: ...
Expand All @@ -997,42 +1000,50 @@ def issubclass(__cls: type, __class_or_tuple: Union[type, Tuple[Union[type, Tupl
def len(__obj: Sized) -> int: ...
def license() -> None: ...
def locals() -> Dict[str, Any]: ...
@overload
def map(__func: Callable[[_T1], _S], __iter1: Iterable[_T1]) -> Iterator[_S]: ...
@overload
def map(__func: Callable[[_T1, _T2], _S], __iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> Iterator[_S]: ...
@overload
def map(
__func: Callable[[_T1, _T2, _T3], _S], __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]
) -> Iterator[_S]: ...
@overload
def map(
__func: Callable[[_T1, _T2, _T3, _T4], _S],
__iter1: Iterable[_T1],
__iter2: Iterable[_T2],
__iter3: Iterable[_T3],
__iter4: Iterable[_T4],
) -> Iterator[_S]: ...
@overload
def map(
__func: Callable[[_T1, _T2, _T3, _T4, _T5], _S],
__iter1: Iterable[_T1],
__iter2: Iterable[_T2],
__iter3: Iterable[_T3],
__iter4: Iterable[_T4],
__iter5: Iterable[_T5],
) -> Iterator[_S]: ...
@overload
def map(
__func: Callable[..., _S],
__iter1: Iterable[Any],
__iter2: Iterable[Any],
__iter3: Iterable[Any],
__iter4: Iterable[Any],
__iter5: Iterable[Any],
__iter6: Iterable[Any],
*iterables: Iterable[Any],
) -> Iterator[_S]: ...

class map(Iterator[_S], Generic[_S]):
@overload
def __init__(self, __func: Callable[[_T1], _S], __iter1: Iterable[_T1]) -> None: ...
@overload
def __init__(self, __func: Callable[[_T1, _T2], _S], __iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> None: ...
@overload
def __init__(
self, __func: Callable[[_T1, _T2, _T3], _S], __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]
) -> None: ...
@overload
def __init__(
self,
__func: Callable[[_T1, _T2, _T3, _T4], _S],
__iter1: Iterable[_T1],
__iter2: Iterable[_T2],
__iter3: Iterable[_T3],
__iter4: Iterable[_T4],
) -> None: ...
@overload
def __init__(
self,
__func: Callable[[_T1, _T2, _T3, _T4, _T5], _S],
__iter1: Iterable[_T1],
__iter2: Iterable[_T2],
__iter3: Iterable[_T3],
__iter4: Iterable[_T4],
__iter5: Iterable[_T5],
) -> None: ...
@overload
def __init__(
self,
__func: Callable[..., _S],
__iter1: Iterable[Any],
__iter2: Iterable[Any],
__iter3: Iterable[Any],
__iter4: Iterable[Any],
__iter5: Iterable[Any],
__iter6: Iterable[Any],
*iterables: Iterable[Any],
) -> None: ...
def __iter__(self) -> Iterator[_S]: ...
def __next__(self) -> _S: ...

@overload
def max(
__arg1: SupportsLessThanT, __arg2: SupportsLessThanT, *_args: SupportsLessThanT, key: None = ...
Expand Down Expand Up @@ -1201,10 +1212,15 @@ else:
def pow(__base: _SupportsPow3[_E, _M, _T_co], __exp: _E, __mod: _M) -> _T_co: ...

def quit(code: object = ...) -> NoReturn: ...
@overload
def reversed(__sequence: Sequence[_T]) -> Iterator[_T]: ...
@overload
def reversed(__sequence: Reversible[_T]) -> Iterator[_T]: ...

class reversed(Iterator[_T], Generic[_T]):
@overload
def __init__(self, __sequence: Sequence[_T]) -> None: ...
@overload
def __init__(self, __sequence: Reversible[_T]) -> None: ...
def __iter__(self) -> Iterator[_T]: ...
def __next__(self) -> _T: ...

def repr(__obj: object) -> str: ...
@overload
def round(number: SupportsRound[Any]) -> int: ...
Expand All @@ -1231,30 +1247,41 @@ else:
def sum(__iterable: Iterable[_T], __start: _S) -> Union[_T, _S]: ...

def vars(__object: Any = ...) -> Dict[str, Any]: ...
@overload
def zip(__iter1: Iterable[_T1]) -> Iterator[Tuple[_T1]]: ...
@overload
def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> Iterator[Tuple[_T1, _T2]]: ...
@overload
def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]) -> Iterator[Tuple[_T1, _T2, _T3]]: ...
@overload
def zip(
__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4]
) -> Iterator[Tuple[_T1, _T2, _T3, _T4]]: ...
@overload
def zip(
__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4], __iter5: Iterable[_T5]
) -> Iterator[Tuple[_T1, _T2, _T3, _T4, _T5]]: ...
@overload
def zip(
__iter1: Iterable[Any],
__iter2: Iterable[Any],
__iter3: Iterable[Any],
__iter4: Iterable[Any],
__iter5: Iterable[Any],
__iter6: Iterable[Any],
*iterables: Iterable[Any],
) -> Iterator[Tuple[Any, ...]]: ...

class zip(Iterator[_T_co], Generic[_T_co]):
@overload
def __new__(cls, __iter1: Iterable[_T1]) -> zip[Tuple[_T1]]: ...
@overload
def __new__(cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> zip[Tuple[_T1, _T2]]: ...
@overload
def __new__(cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]) -> zip[Tuple[_T1, _T2, _T3]]: ...
@overload
def __new__(
cls, __iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3], __iter4: Iterable[_T4]
) -> zip[Tuple[_T1, _T2, _T3, _T4]]: ...
@overload
def __new__(
cls,
__iter1: Iterable[_T1],
__iter2: Iterable[_T2],
__iter3: Iterable[_T3],
__iter4: Iterable[_T4],
__iter5: Iterable[_T5],
) -> zip[Tuple[_T1, _T2, _T3, _T4, _T5]]: ...
@overload
def __new__(
cls,
__iter1: Iterable[Any],
__iter2: Iterable[Any],
__iter3: Iterable[Any],
__iter4: Iterable[Any],
__iter5: Iterable[Any],
__iter6: Iterable[Any],
*iterables: Iterable[Any],
) -> zip[Tuple[Any, ...]]: ...
def __iter__(self) -> Iterator[_T_co]: ...
def __next__(self) -> _T_co: ...

def __import__(
name: str,
globals: Optional[Mapping[str, Any]] = ...,
Expand Down
29 changes: 7 additions & 22 deletions tests/stubtest_whitelists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,15 @@ asyncio.locks.Condition.locked
asyncio.locks.Condition.release
asyncio.proactor_events.BaseProactorEventLoop.sock_recv # nbytes parameter has different name 'n' in implementation
asyncio.selector_events.BaseSelectorEventLoop.sock_recv # nbytes parameter has different name 'n' in implementation
builtins.bytearray.__float__
builtins.bytearray.__int__
builtins.bytearray.append
builtins.bytearray.extend
builtins.bytearray.maketrans
builtins.bytes.__float__
builtins.bytes.__int__
builtins.bytes.maketrans
builtins.classmethod.__get__
builtins.ellipsis
builtins.filter # not a function at runtime
builtins.classmethod.__get__ # this function can accept no value for the type parameter.
builtins.ellipsis # type is not exposed anywhere
builtins.function
builtins.map # not a function at runtime
builtins.memoryview.__contains__
builtins.memoryview.__iter__
builtins.memoryview.__contains__ # C type that implements __getitem__
builtins.memoryview.__iter__ # C type that implements __getitem__
builtins.memoryview.cast # inspect.signature is incorrect about shape being kw-only
builtins.object.__init__
builtins.property.__get__
builtins.property.fdel
builtins.property.fget
builtins.property.fset
builtins.reversed # not a function at runtime
builtins.staticmethod.__get__
builtins.zip # not a function at runtime
builtins.object.__init__ # default C signature is incorrect
builtins.property.__get__ # this function can accept no value for the type parameter.
builtins.staticmethod.__get__ # this function can accept no value for the type parameter.
bz2.BZ2Decompressor.__init__ # function does not accept parameters but C signature is set
# The following methods were changed in point releases from Python 3.6 to 3.9
# as part of a security fix. These excludes can be removed when the GitHub
Expand Down