Skip to content

Commit ff64deb

Browse files
jpy-gitAlexWaygood
andauthored
Add missing function attributes to builtins.function (#6804)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 44f9e36 commit ff64deb

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

stdlib/builtins.pyi

+12-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ from _typeshed import (
2323
SupportsWrite,
2424
)
2525
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
26-
from types import CodeType, TracebackType
26+
from types import CodeType, TracebackType, _Cell
2727
from typing import (
2828
IO,
2929
AbstractSet,
@@ -764,13 +764,22 @@ class tuple(Sequence[_T_co], Generic[_T_co]):
764764
if sys.version_info >= (3, 9):
765765
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
766766

767+
# Make sure this class definition stays roughly in line with `types.FunctionType`
768+
@final
767769
class function:
768770
# TODO not defined in builtins!
769-
__name__: str
770-
__module__: str
771+
__closure__: tuple[_Cell, ...] | None
771772
__code__: CodeType
773+
__defaults__: tuple[Any, ...] | None
774+
__dict__: dict[str, Any]
775+
__globals__: dict[str, Any]
776+
__name__: str
772777
__qualname__: str
773778
__annotations__: dict[str, Any]
779+
__kwdefaults__: dict[str, Any]
780+
__module__: str
781+
# mypy uses `builtins.function.__get__` to represent methods, properties, and getset_descriptors so we type the return as Any.
782+
def __get__(self, obj: object | None, type: type | None = ...) -> Any: ...
774783

775784
class list(MutableSequence[_T], Generic[_T]):
776785
@overload

stdlib/types.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _Cell:
3939
__hash__: None # type: ignore[assignment]
4040
cell_contents: Any
4141

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

6465
LambdaType = FunctionType
6566

tests/stubtest_allowlists/py3_common.txt

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ pydoc.Helper.symbols_ # Loop variable in class https://github.com/python/typesh
270270
pydoc.Helper.topic # Loop variable in class https://github.com/python/typeshed/issues/6401#issuecomment-981178522
271271
# Dynamically specified by __getattr__, and thus don't exist on the class
272272
tempfile._TemporaryFileWrapper.[\w_]+
273+
# stubtest incorrectly highlights the type argument as not having a default value.
274+
types.FunctionType.__get__
275+
types.LambdaType.__get__
273276
# Various classes in typing aren't types at runtime. In addition, mypy thinks some special forms are tautologically defined.
274277
typing.[A-Z]\w+
275278
typing_extensions\..*

0 commit comments

Comments
 (0)