Skip to content

Commit fa56bc7

Browse files
committed
tooling: Override typeshed stdlib stubs with MicroPython versions.
1 parent dcbfce3 commit fa56bc7

13 files changed

Lines changed: 1773 additions & 0 deletions

File tree

pyrightconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"typeCheckingMode": "basic",
55
"extraPaths": ["lib", "typings"],
66
"stubPath": "typings",
7+
"typeshedPath": "typings",
78
"exclude": [
89
".build",
910
"node_modules",

typings/stdlib/binascii.pyi

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Binary/ASCII conversions.
3+
4+
MicroPython module: https://docs.micropython.org/en/v1.27.0/library/binascii.html
5+
6+
CPython module: :mod:`python:binascii` https://docs.python.org/3/library/binascii.html .
7+
8+
This module implements conversions between binary data and various
9+
encodings of it in ASCII form (in both directions).
10+
11+
---
12+
Module: 'binascii' on micropython-v1.27.0-stm32-PYBV11-NETWORK
13+
"""
14+
15+
# MCU: {'variant': 'NETWORK', 'build': '', 'arch': 'armv7emsp', 'port': 'stm32', 'board': 'PYBV11', 'board_id': 'PYBV11-NETWORK', 'mpy': 'v6.3', 'ver': '1.27.0', 'family': 'micropython', 'cpu': 'STM32F405RG', 'version': '1.27.0'}
16+
# Stubber: v1.26.4
17+
from __future__ import annotations
18+
from _typeshed import Incomplete
19+
from typing import Any, Optional
20+
from typing_extensions import Awaitable, TypeAlias, TypeVar
21+
22+
def crc32(data, value: Optional[Any] = None) -> Incomplete:
23+
"""
24+
Compute CRC-32, the 32-bit checksum of *data*, starting with an initial CRC
25+
of *value*. The default initial CRC is zero. The algorithm is consistent
26+
with the ZIP file checksum.
27+
"""
28+
...
29+
30+
def hexlify(data: bytes, sep: str | bytes = ..., /) -> bytes:
31+
"""
32+
Convert the bytes in the *data* object to a hexadecimal representation.
33+
Returns a bytes object.
34+
35+
If the additional argument *sep* is supplied it is used as a separator
36+
between hexadecimal values.
37+
"""
38+
...
39+
40+
def unhexlify(data: str | bytes, /) -> bytes:
41+
"""
42+
Convert hexadecimal data to binary representation. Returns bytes string.
43+
(i.e. inverse of hexlify)
44+
"""
45+
...
46+
47+
def b2a_base64(data: bytes, /) -> bytes:
48+
"""
49+
Encode binary data in base64 format, as in `RFC 3548
50+
<https://tools.ietf.org/html/rfc3548.html>`_. Returns the encoded data
51+
followed by a newline character if newline is true, as a bytes object.
52+
"""
53+
...
54+
55+
def a2b_base64(data: str | bytes, /) -> bytes:
56+
"""
57+
Decode base64-encoded data, ignoring invalid characters in the input.
58+
Conforms to `RFC 2045 s.6.8 <https://tools.ietf.org/html/rfc2045#section-6.8>`_.
59+
Returns a bytes object.
60+
"""
61+
...

typings/stdlib/cmath.pyi

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
Mathematical functions for complex numbers.
3+
4+
MicroPython module: https://docs.micropython.org/en/v1.27.0/library/cmath.html
5+
6+
CPython module: :mod:`python:cmath` https://docs.python.org/3/library/cmath.html .
7+
8+
The ``cmath`` module provides some basic mathematical functions for
9+
working with complex numbers.
10+
11+
Availability: not available on WiPy and ESP8266. Floating point support
12+
required for this module.
13+
14+
---
15+
Module: 'cmath' on micropython-v1.27.0-stm32-PYBV11-NETWORK
16+
"""
17+
18+
# MCU: {'variant': 'NETWORK', 'build': '', 'arch': 'armv7emsp', 'port': 'stm32', 'board': 'PYBV11', 'board_id': 'PYBV11-NETWORK', 'mpy': 'v6.3', 'ver': '1.27.0', 'family': 'micropython', 'cpu': 'STM32F405RG', 'version': '1.27.0'}
19+
# Stubber: v1.26.4
20+
from __future__ import annotations
21+
from _typeshed import Incomplete
22+
from typing import SupportsComplex, SupportsFloat, SupportsIndex, Tuple
23+
from typing_extensions import Awaitable, TypeAlias, TypeVar
24+
25+
_C: TypeAlias = SupportsFloat | SupportsComplex | SupportsIndex | complex
26+
27+
e: float = 2.7182818
28+
"""base of the natural logarithm"""
29+
pi: float = 3.1415928
30+
"""the ratio of a circle's circumference to its diameter"""
31+
32+
def polar(z: _C, /) -> Tuple:
33+
"""
34+
Returns, as a tuple, the polar form of ``z``.
35+
"""
36+
...
37+
38+
def sqrt(z: _C, /) -> complex:
39+
"""
40+
Return the square-root of ``z``.
41+
"""
42+
...
43+
44+
def rect(r: float, phi: float, /) -> float:
45+
"""
46+
Returns the complex number with modulus ``r`` and phase ``phi``.
47+
"""
48+
...
49+
50+
def sin(z: _C, /) -> float:
51+
"""
52+
Return the sine of ``z``.
53+
"""
54+
...
55+
56+
def exp(z: _C, /) -> float:
57+
"""
58+
Return the exponential of ``z``.
59+
"""
60+
...
61+
62+
def cos(z: _C, /) -> float:
63+
"""
64+
Return the cosine of ``z``.
65+
"""
66+
...
67+
68+
def phase(z: _C, /) -> float:
69+
"""
70+
Returns the phase of the number ``z``, in the range (-pi, +pi].
71+
"""
72+
...
73+
74+
def log(z: _C, /) -> float:
75+
"""
76+
Return the natural logarithm of ``z``. The branch cut is along the negative real axis.
77+
"""
78+
...
79+
80+
def log10(z: _C, /) -> float:
81+
"""
82+
Return the base-10 logarithm of ``z``. The branch cut is along the negative real axis.
83+
"""
84+
...

typings/stdlib/errno.pyi

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
System error codes.
3+
4+
MicroPython module: https://docs.micropython.org/en/v1.27.0/library/errno.html
5+
6+
CPython module: :mod:`python:errno` https://docs.python.org/3/library/errno.html .
7+
8+
This module provides access to symbolic error codes for `OSError` exception.
9+
A particular inventory of codes depends on :term:`MicroPython port`.
10+
11+
---
12+
Module: 'errno' on micropython-v1.27.0-stm32-PYBV11-NETWORK
13+
"""
14+
15+
# MCU: {'variant': 'NETWORK', 'build': '', 'arch': 'armv7emsp', 'port': 'stm32', 'board': 'PYBV11', 'board_id': 'PYBV11-NETWORK', 'mpy': 'v6.3', 'ver': '1.27.0', 'family': 'micropython', 'cpu': 'STM32F405RG', 'version': '1.27.0'}
16+
# Stubber: v1.26.4
17+
from __future__ import annotations
18+
from typing import Dict, Final
19+
from _typeshed import Incomplete
20+
from typing_extensions import Awaitable, TypeAlias, TypeVar
21+
22+
ENOBUFS: Final[int] = 105
23+
"""No buffer space available"""
24+
ENODEV: Final[int] = 19
25+
"""No such device"""
26+
ENOENT: Final[int] = 2
27+
"""No such file or directory"""
28+
EISDIR: Final[int] = 21
29+
"""Is a directory"""
30+
EIO: Final[int] = 5
31+
"""I/O error"""
32+
EINVAL: Final[int] = 22
33+
"""Invalid argument"""
34+
EPERM: Final[int] = 1
35+
"""Operation not permitted"""
36+
ETIMEDOUT: Final[int] = 110
37+
"""Connection timed out"""
38+
ENOMEM: Final[int] = 12
39+
"""Out of memory"""
40+
EOPNOTSUPP: Final[int] = 95
41+
"""Operation not supported"""
42+
ENOTCONN: Final[int] = 107
43+
"""Transport endpoint is not connected"""
44+
errorcode: dict = {}
45+
"""\
46+
Dictionary mapping numeric error codes to strings with symbolic error
47+
code (see above)::
48+
49+
>>> print(errno.errorcode[errno.EEXIST])
50+
EEXIST
51+
"""
52+
EAGAIN: Final[int] = 11
53+
"""\
54+
Error codes, based on ANSI C/POSIX standard. All error codes start with
55+
"E". As mentioned above, inventory of the codes depends on
56+
:term:`MicroPython port`. Errors are usually accessible as ``exc.errno``
57+
where ``exc`` is an instance of `OSError`. Usage example::
58+
59+
try:
60+
os.mkdir("my_dir")
61+
except OSError as exc:
62+
if exc.errno == errno.EEXIST:
63+
print("Directory already exists")
64+
"""
65+
EALREADY: Final[int] = 114
66+
"""Operation already in progress"""
67+
EBADF: Final[int] = 9
68+
"""Bad file descriptor"""
69+
EADDRINUSE: Final[int] = 98
70+
"""Address already in use"""
71+
EACCES: Final[int] = 13
72+
"""Permission denied"""
73+
EINPROGRESS: Final[int] = 115
74+
"""Operation now in progress"""
75+
EEXIST: Final[int] = 17
76+
"""\
77+
Error codes, based on ANSI C/POSIX standard. All error codes start with
78+
"E". As mentioned above, inventory of the codes depends on
79+
:term:`MicroPython port`. Errors are usually accessible as ``exc.errno``
80+
where ``exc`` is an instance of `OSError`. Usage example::
81+
82+
try:
83+
os.mkdir("my_dir")
84+
except OSError as exc:
85+
if exc.errno == errno.EEXIST:
86+
print("Directory already exists")
87+
"""
88+
EHOSTUNREACH: Final[int] = 113
89+
"""Host is unreachable"""
90+
ECONNABORTED: Final[int] = 103
91+
"""Connection aborted"""
92+
ECONNRESET: Final[int] = 104
93+
"""Connection reset by peer"""
94+
ECONNREFUSED: Final[int] = 111
95+
"""Connection refused"""
96+
ENOTSUP: Final[int] = ...
97+
"""Operation not supported"""

typings/stdlib/gc.pyi

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
Control the garbage collector.
3+
4+
MicroPython module: https://docs.micropython.org/en/v1.27.0/library/gc.html
5+
6+
CPython module: :mod:`python:gc` https://docs.python.org/3/library/gc.html .
7+
8+
---
9+
Module: 'gc' on micropython-v1.27.0-stm32-PYBV11-NETWORK
10+
"""
11+
12+
# MCU: {'variant': 'NETWORK', 'build': '', 'arch': 'armv7emsp', 'port': 'stm32', 'board': 'PYBV11', 'board_id': 'PYBV11-NETWORK', 'mpy': 'v6.3', 'ver': '1.27.0', 'family': 'micropython', 'cpu': 'STM32F405RG', 'version': '1.27.0'}
13+
# Stubber: v1.26.4
14+
from __future__ import annotations
15+
from _typeshed import Incomplete
16+
from typing import overload
17+
from typing_extensions import Awaitable, TypeAlias, TypeVar
18+
19+
def mem_alloc() -> int:
20+
"""
21+
Return the number of bytes of heap RAM that are allocated by Python code.
22+
23+
Admonition:Difference to CPython
24+
:class: attention
25+
26+
This function is MicroPython extension.
27+
"""
28+
...
29+
30+
def isenabled(*args, **kwargs) -> Incomplete: ...
31+
def mem_free() -> int:
32+
"""
33+
Return the number of bytes of heap RAM that is available for Python
34+
code to allocate, or -1 if this amount is not known.
35+
36+
Admonition:Difference to CPython
37+
:class: attention
38+
39+
This function is MicroPython extension.
40+
"""
41+
...
42+
43+
@overload
44+
def threshold() -> int:
45+
"""
46+
Set or query the additional GC allocation threshold. Normally, a collection
47+
is triggered only when a new allocation cannot be satisfied, i.e. on an
48+
out-of-memory (OOM) condition. If this function is called, in addition to
49+
OOM, a collection will be triggered each time after *amount* bytes have been
50+
allocated (in total, since the previous time such an amount of bytes
51+
have been allocated). *amount* is usually specified as less than the
52+
full heap size, with the intention to trigger a collection earlier than when the
53+
heap becomes exhausted, and in the hope that an early collection will prevent
54+
excessive memory fragmentation. This is a heuristic measure, the effect
55+
of which will vary from application to application, as well as
56+
the optimal value of the *amount* parameter.
57+
58+
Calling the function without argument will return the current value of
59+
the threshold. A value of -1 means a disabled allocation threshold.
60+
61+
Admonition:Difference to CPython
62+
:class: attention
63+
64+
This function is a MicroPython extension. CPython has a similar
65+
function - ``set_threshold()``, but due to different GC
66+
implementations, its signature and semantics are different.
67+
"""
68+
69+
@overload
70+
def threshold(amount: int) -> None:
71+
"""
72+
Set or query the additional GC allocation threshold. Normally, a collection
73+
is triggered only when a new allocation cannot be satisfied, i.e. on an
74+
out-of-memory (OOM) condition. If this function is called, in addition to
75+
OOM, a collection will be triggered each time after *amount* bytes have been
76+
allocated (in total, since the previous time such an amount of bytes
77+
have been allocated). *amount* is usually specified as less than the
78+
full heap size, with the intention to trigger a collection earlier than when the
79+
heap becomes exhausted, and in the hope that an early collection will prevent
80+
excessive memory fragmentation. This is a heuristic measure, the effect
81+
of which will vary from application to application, as well as
82+
the optimal value of the *amount* parameter.
83+
84+
Calling the function without argument will return the current value of
85+
the threshold. A value of -1 means a disabled allocation threshold.
86+
87+
Admonition:Difference to CPython
88+
:class: attention
89+
90+
This function is a MicroPython extension. CPython has a similar
91+
function - ``set_threshold()``, but due to different GC
92+
implementations, its signature and semantics are different.
93+
"""
94+
95+
def collect() -> None:
96+
"""
97+
Run a garbage collection.
98+
"""
99+
...
100+
101+
def enable() -> None:
102+
"""
103+
Enable automatic garbage collection.
104+
"""
105+
...
106+
107+
def disable() -> None:
108+
"""
109+
Disable automatic garbage collection. Heap memory can still be allocated,
110+
and garbage collection can still be initiated manually using :meth:`gc.collect`.
111+
"""
112+
...

0 commit comments

Comments
 (0)