Skip to content

Commit f8112e4

Browse files
Add documentation for top level builtins
1 parent e963bd5 commit f8112e4

File tree

1 file changed

+180
-24
lines changed

1 file changed

+180
-24
lines changed

lang/en/typeshed/stdlib/builtins.pyi

Lines changed: 180 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@ class type(object):
141141
self, name: str, bases: Tuple[type, ...], dict: dict[str, Any], **kwds: Any
142142
) -> None: ...
143143
@overload
144-
def __new__(cls, o: object) -> type: ...
144+
def __new__(cls, o: object) -> type:
145+
"""Get the type of an object.
146+
147+
Example: ``type("hello, world)``
148+
149+
:return: The type of the object passed in.
150+
"""
151+
...
145152
@overload
146153
def __new__(
147154
cls: Type[_TT],
@@ -171,15 +178,32 @@ class super(object):
171178
def __init__(self) -> None: ...
172179

173180
class int:
181+
"""Get an integer from a number or a string.
182+
"""
174183
@overload
175184
def __new__(
176185
cls: Type[_T],
177186
x: str | bytes | SupportsInt | SupportsIndex | _SupportsTrunc = ...,
178-
) -> _T: ...
187+
) -> _T:
188+
"""Get an integer from a number or a string.
189+
190+
Example: ``int("1.2")``
191+
192+
:return: Zero if no argument is provided, or an ``int`` from a number or string truncated toward zero.
193+
"""
194+
...
179195
@overload
180196
def __new__(
181197
cls: Type[_T], x: str | bytes | bytearray, base: SupportsIndex
182-
) -> _T: ...
198+
) -> _T:
199+
"""Get an integer from a number or a string.
200+
201+
Example: ``int("8.3", 2)``
202+
203+
:param base: (default=10) Allowed bases are 0 and 2–36.
204+
:return: Zero if no argument is provided, or an ``int`` from a number or string truncated toward zero.
205+
"""
206+
...
183207
def to_bytes(
184208
self,
185209
length: SupportsIndex,
@@ -251,7 +275,14 @@ class int:
251275
class float:
252276
def __new__(
253277
cls: Type[_T], x: SupportsFloat | SupportsIndex | str | bytes | bytearray = ...
254-
) -> _T: ...
278+
) -> _T:
279+
"""Convert a string or number to a floating point number, if possible.
280+
281+
Example: ``float("1.2")``
282+
283+
:return: ``0.0`` if no argument is provided, or a ``float`` from a number or string.
284+
"""
285+
...
255286
def __add__(self, x: float) -> float: ...
256287
def __sub__(self, x: float) -> float: ...
257288
def __mul__(self, x: float) -> float: ...
@@ -326,11 +357,29 @@ class complex:
326357

327358
class str(Sequence[str]):
328359
@overload
329-
def __new__(cls: Type[_T], o: object = ...) -> _T: ...
360+
def __new__(cls: Type[_T], object: object = "") -> _T:
361+
"""Get a string version of an object.
362+
363+
Example: ``print("The number " + str(42) " is great!")``
364+
365+
:param object: (default="") Object to return a string version of.
366+
:return: A string reprentation of an object.
367+
"""
368+
...
330369
@overload
331370
def __new__(
332-
cls: Type[_T], o: bytes, encoding: str = ..., errors: str = ...
333-
) -> _T: ...
371+
cls: Type[_T], object: bytes = b"", encoding: str = "uft-8", errors: str = "strict"
372+
) -> _T:
373+
"""Get a string version of an object.
374+
375+
Example: ``print("The number " + str(42) " is great!")``
376+
377+
:param object: (default=b"") Object to return a string version of as bytes or a bytearray.
378+
:param encoding: (default="uft-8") Encoding used to decode object.
379+
:param errors: (default="strict") Something about error handling...
380+
:return: A string reprentation of an object.
381+
"""
382+
...
334383
def count(
335384
self,
336385
x: str,
@@ -813,11 +862,29 @@ class range(Sequence[int]):
813862
stop: int
814863
step: int
815864
@overload
816-
def __init__(self, stop: SupportsIndex) -> None: ...
865+
def __init__(self, stop: SupportsIndex) -> None:
866+
"""Get a range of values from 0 up tp the stop parameter.
867+
868+
Example: ``rangeTimesTwo = [x * 2 for x in range(5)]``
869+
870+
:param stop: An integer to determine the end of the range (exclusive).
871+
:return: A range object
872+
"""
873+
...
817874
@overload
818875
def __init__(
819876
self, start: SupportsIndex, stop: SupportsIndex, step: SupportsIndex = ...
820-
) -> None: ...
877+
) -> None:
878+
"""Get a range of values from 0 up tp the stop parameter.
879+
880+
Example: ``rangeTimesTwo = [x * 2 for x in range(1, 11, 2)]``
881+
882+
:param start: (default=0) An integer to determine the start of the range (inclusive).
883+
:param stop: An integer to determine the end of the range (exclusive).
884+
:param step: (default=1) The increment for each value in the range.
885+
:return: A range object
886+
"""
887+
...
821888
def __len__(self) -> int: ...
822889
def __contains__(self, o: object) -> bool: ...
823890
def __iter__(self) -> Iterator[int]: ...
@@ -853,7 +920,15 @@ class _NotImplementedType(Any): # type: ignore
853920

854921
NotImplemented: _NotImplementedType
855922

856-
def abs(__x: SupportsAbs[_T]) -> _T: ...
923+
def abs(__x: SupportsAbs[_T]) -> _T:
924+
"""Get the absolute value of a number.
925+
926+
Example: ``abs(-42)``
927+
928+
:param __x: A number.
929+
:return: The length of or number of items in an object.
930+
"""
931+
...
857932
def all(__iterable: Iterable[object]) -> bool: ...
858933
def any(__iterable: Iterable[object]) -> bool: ...
859934
def bin(__number: int | SupportsIndex) -> str: ...
@@ -862,7 +937,15 @@ if sys.version_info >= (3, 7):
862937
def breakpoint(*args: Any, **kws: Any) -> None: ...
863938

864939
def callable(__obj: object) -> bool: ...
865-
def chr(__i: int) -> str: ...
940+
def chr(__i: int) -> str:
941+
"""Get a Unicode string representation of an integer.
942+
943+
Example: ``chr(97)``
944+
945+
:param __i: An integer within the range 0..1,114,111.
946+
:return: A Unicode string representation of a number within a valid range.
947+
"""
948+
...
866949

867950
# We define this here instead of using os.PathLike to avoid import cycle issues.
868951
# See https://github.com/python/typeshed/pull/991#issuecomment-288160993
@@ -919,10 +1002,30 @@ def getattr(__o: object, name: str, __default: _T) -> Any | _T: ...
9191002
def globals() -> dict[str, Any]: ...
9201003
def hasattr(__obj: object, __name: str) -> bool: ...
9211004
def hash(__obj: object) -> int: ...
922-
def help(*args: Any, **kwds: Any) -> None: ...
923-
def hex(__number: int | SupportsIndex) -> str: ...
1005+
def help(request: object) -> None:
1006+
"""Starts interactive help or provides help for the request object, if valid.
1007+
1008+
Example: ``help("print")``
1009+
"""
1010+
...
1011+
def hex(__number: int | SupportsIndex) -> str:
1012+
"""Get the hexadecimal representation of an integer.
1013+
1014+
Example: ``hex(42)``
1015+
1016+
:return: The hexadecimal representation of an integer as a string.
1017+
"""
1018+
...
9241019
def id(__obj: object) -> int: ...
925-
def input(__prompt: Any = ...) -> str: ...
1020+
def input(prompt: object = "") -> str:
1021+
"""Get user input as a string.
1022+
1023+
Example: ``prompt("Enter your name: ")``
1024+
1025+
:param prompt: (default="") Text prompt seen by users.
1026+
:return: The length of or number of items in an object.
1027+
"""
1028+
...
9261029
@overload
9271030
def iter(__iterable: Iterable[_T]) -> Iterator[_T]: ...
9281031
@overload
@@ -952,7 +1055,15 @@ else:
9521055
__cls: type, __class_or_tuple: type | Tuple[type | Tuple[Any, ...], ...]
9531056
) -> bool: ...
9541057

955-
def len(__obj: Sized) -> int: ...
1058+
def len(__obj: Sized) -> int:
1059+
"""Get the length of, or number of items in an object.
1060+
1061+
Example: ``len("Hello, world")``
1062+
1063+
:param __obj: A sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
1064+
:return: The length of or number of items in an object.
1065+
"""
1066+
...
9561067
def locals() -> dict[str, Any]: ...
9571068

9581069
class map(Iterator[_S], Generic[_S]):
@@ -1104,14 +1215,33 @@ def open(
11041215
closefd: bool = ...,
11051216
opener: _Opener | None = ...,
11061217
) -> IO[Any]: ...
1107-
def ord(__c: str | bytes) -> int: ...
1218+
def ord(__c: str | bytes) -> int:
1219+
"""Get an integer representation of a Unicode character.
1220+
1221+
Example: ``ord("a")``
1222+
1223+
:param __c: A Unicode character.
1224+
:return: The length of or number of items in an object.
1225+
"""
1226+
...
11081227
def print(
11091228
*values: object,
1110-
sep: str | None = ...,
1111-
end: str | None = ...,
1112-
file: SupportsWrite[str] | None = ...,
1113-
flush: bool = ...,
1114-
) -> None: ...
1229+
sep: str | None = " ",
1230+
end: str | None = "\n",
1231+
file: SupportsWrite[str] | None = None,
1232+
flush: bool = False,
1233+
) -> None:
1234+
"""Prints values to a stream or standard output, typically the serial console.
1235+
1236+
Example: ``print("Hello, world")``
1237+
1238+
:param *values: Arguments or literals to print.
1239+
:param sep: (default=" ") A string separator inserted between values.
1240+
:param end: (default="\n") A string appended after the last value.
1241+
:param file: (default=None) A file-like object (stream).
1242+
:param flush: (default=False) Whether to forcibly flush the stream.
1243+
"""
1244+
...
11151245

11161246
_E = TypeVar("_E", contravariant=True)
11171247
_M = TypeVar("_M", contravariant=True)
@@ -1160,11 +1290,37 @@ class reversed(Iterator[_T], Generic[_T]):
11601290

11611291
def repr(__obj: object) -> str: ...
11621292
@overload
1163-
def round(number: SupportsRound[Any]) -> int: ...
1293+
def round(number: SupportsRound[Any]) -> int:
1294+
"""Round a number to the nearest integer.
1295+
1296+
Example: ``round(42.42)``
1297+
1298+
:param number: A number to round to the nearest integer.
1299+
:return: Round a number to the nearest integer.
1300+
"""
1301+
...
11641302
@overload
1165-
def round(number: SupportsRound[Any], ndigits: None) -> int: ...
1303+
def round(number: SupportsRound[Any], ndigits: None) -> int:
1304+
"""Round a number to the nearest integer.
1305+
1306+
Example: ``round(42.42, None)``
1307+
1308+
:param number: A number to round to the nearest integer.
1309+
:param ndigits: The number of decimal digits to round to.
1310+
:return: Round a number to the nearest integer.
1311+
"""
1312+
...
11661313
@overload
1167-
def round(number: SupportsRound[_T], ndigits: SupportsIndex) -> _T: ...
1314+
def round(number: SupportsRound[_T], ndigits: SupportsIndex) -> _T:
1315+
"""Round a number to the nearest integer.
1316+
1317+
Example: ``round(42.42, 1)``
1318+
1319+
:param number: A number to round to the number of decimal digits specified.
1320+
:param ndigits: The number of decimal digits to round to.
1321+
:return: The input number rounded to the number of decimal digits specified.
1322+
"""
1323+
...
11681324
def setattr(__obj: object, __name: str, __value: Any) -> None: ...
11691325
@overload
11701326
def sorted(

0 commit comments

Comments
 (0)