Skip to content

Add missing function attributes to builtins.function #6804

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 10 commits into from
Jan 4, 2022
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
15 changes: 12 additions & 3 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ from _typeshed import (
SupportsWrite,
)
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
from types import CodeType, TracebackType
from types import CodeType, TracebackType, _Cell
from typing import (
IO,
AbstractSet,
Expand Down Expand Up @@ -764,13 +764,22 @@ class tuple(Sequence[_T_co], Generic[_T_co]):
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...

# Make sure this class definition stays roughly in line with `types.FunctionType`
@final
class function:
# TODO not defined in builtins!
__name__: str
__module__: str
__closure__: tuple[_Cell, ...] | None
__code__: CodeType
__defaults__: tuple[Any, ...] | None
__dict__: dict[str, Any]
__globals__: dict[str, Any]
__name__: str
__qualname__: str
__annotations__: dict[str, Any]
__kwdefaults__: dict[str, Any]
__module__: str
# mypy uses `builtins.function.__get__` to represent methods, properties, and getset_descriptors so we type the return as Any.
def __get__(self, obj: object | None, type: type | None = ...) -> Any: ...

class list(MutableSequence[_T], Generic[_T]):
@overload
Expand Down
3 changes: 2 additions & 1 deletion stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class _Cell:
__hash__: None # type: ignore[assignment]
cell_contents: Any

# Make sure this class definition stays roughly in line with `builtins.function`
@final
class FunctionType:
__closure__: tuple[_Cell, ...] | None
Expand All @@ -59,7 +60,7 @@ class FunctionType:
closure: tuple[_Cell, ...] | None = ...,
) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def __get__(self, obj: object | None, type: type | None) -> MethodType: ...
def __get__(self, obj: object | None, type: type | None = ...) -> MethodType: ...

LambdaType = FunctionType

Expand Down
3 changes: 3 additions & 0 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ pydoc.Helper.symbols_ # Loop variable in class https://github.com/python/typesh
pydoc.Helper.topic # Loop variable in class https://github.com/python/typeshed/issues/6401#issuecomment-981178522
# Dynamically specified by __getattr__, and thus don't exist on the class
tempfile._TemporaryFileWrapper.[\w_]+
# stubtest incorrectly highlights the type argument as not having a default value.
types.FunctionType.__get__
types.LambdaType.__get__
# Various classes in typing aren't types at runtime. In addition, mypy thinks some special forms are tautologically defined.
typing.[A-Z]\w+
typing_extensions\..*
Expand Down