Skip to content

Commit 8eb14e2

Browse files
committed
tooling: Add MicroPython type stubs in typings/ for Pylance resolution.
1 parent 828de3a commit 8eb14e2

123 files changed

Lines changed: 26355 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"python.languageServer": "Pylance",
33
"python.analysis.typeCheckingMode": "basic",
44
"python.analysis.extraPaths": ["lib"],
5+
"python.analysis.stubPath": "typings",
56
"python.analysis.diagnosticSeverityOverrides": {
67
"reportMissingModuleSource": "none",
78
"reportWildcardImportFromLibrary": "none",

pyrightconfig.json

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

typings/__builtins__.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Allows for type checking of Micropython specific builtins by pyright and pylance.
2+
"""
3+
4+
from typing import Tuple, TypeVar
5+
6+
Const_T = TypeVar("Const_T", int, float, str, bytes, Tuple) # constant
7+
8+
def const(expr: Const_T) -> Const_T:
9+
"""
10+
Used to declare that the expression is a constant so that the compiler can
11+
optimise it. The use of this function should be as follows::
12+
13+
from micropython import const
14+
15+
CONST_X = const(123)
16+
CONST_Y = const(2 * CONST_X + 1)
17+
18+
Constants declared this way are still accessible as global variables from
19+
outside the module they are declared in. On the other hand, if a constant
20+
begins with an underscore then it is hidden, it is not available as a global
21+
variable, and does not take up any memory during execution.
22+
23+
This `const` function is recognised directly by the MicroPython parser and is
24+
provided as part of the :mod:`micropython` module mainly so that scripts can be
25+
written which run under both CPython and MicroPython, by following the above
26+
pattern.
27+
"""
28+
...

typings/_mpy_shed/IRQs.pyi

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
IRQ object types, used in the machine, bluetooth, _rp2 and rp2 modules
3+
4+
_IRQ is a union of the types _IRQ_ESP32, _IRQ_RP2 and _IRQ_PYB
5+
to allow the same stubs to support of the different ports of MicroPython.
6+
7+
"""
8+
9+
from typing import Type
10+
11+
from _typeshed import Incomplete
12+
from typing_extensions import TypeAlias
13+
14+
class _IRQ_ESP32:
15+
def trigger(self) -> int: ...
16+
# def flags(self) -> int: ...
17+
18+
class _IRQ_RP2:
19+
# rp2040
20+
# object <irq> is of type irq
21+
# flags -- <function>
22+
# trigger -- <function>
23+
def flags(self) -> int: ...
24+
def trigger(self) -> int: ...
25+
26+
# pybv11
27+
# TODO: Not sure what the correct implementation is
28+
# NoneType
29+
_IRQ_PYB: TypeAlias = None
30+
31+
_IRQ: TypeAlias = Type[_IRQ_ESP32] | Type[_IRQ_RP2] | Type[_IRQ_PYB] | Incomplete

typings/_mpy_shed/__init__.pyi

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
MicroPython-stubs base types that are not present in typeshed.
3+
4+
This is a collection of types that are not present in typeshed, but are used in the micropython stubs.
5+
6+
Common cases are:
7+
- MicroPython implementation is different from CPython, so the types are different.
8+
- MicroPython has some types that are not present in CPython.
9+
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import abc # type: ignore - not collections.abc
15+
import sys
16+
17+
from typing import Final, final
18+
19+
from _typeshed import Incomplete, structseq, AnyStr_co
20+
from typing_extensions import TypeAlias, TypeVar
21+
22+
from .subscriptable import Subscriptable as Subscriptable
23+
from .IRQs import _IRQ
24+
from .neopixelbase import _NeoPixelBase as _NeoPixelBase
25+
from .blockdevice import (
26+
_BlockDeviceProtocol as _BlockDeviceProtocol,
27+
_OldAbstractBlockDev,
28+
_OldAbstractReadOnlyBlockDev,
29+
)
30+
from .buffer_mp import AnyReadableBuf as AnyReadableBuf, AnyWritableBuf as AnyWritableBuf
31+
32+
from .io_mp import (
33+
BytesIO as BytesIO,
34+
FileIO as FileIO,
35+
IncrementalNewlineDecoder as IncrementalNewlineDecoder,
36+
StringIO as StringIO,
37+
TextIOWrapper as TextIOWrapper,
38+
IOBase_mp as IOBase_mp,
39+
_BufferedIOBase,
40+
_IOBase,
41+
_RawIOBase,
42+
_TextIOBase,
43+
open as open,
44+
)
45+
46+
from .time_mp import _TimeTuple as _TimeTuple
47+
from .pathlike import PathLike as PathLike
48+
49+
from .mp_implementation import _mp_implementation as _mp_implementation
50+
from .mp_available import mp_available as mp_available
51+
# ------------------
52+
# copied from _typeshed os.pyi as os.pyi cannot import from a module with the same name
53+
GenericAlias = type(list[int])
54+
55+
# ------------------------------------------------------------------------------------
56+
StrOrBytesPath: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes]
57+
_StrOrBytesT = TypeVar("_StrOrBytesT", str, bytes)
58+
59+
# ------------------------------------------------------------------------------------
60+
_AnyPath: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes]
61+
_FdOrAnyPath: TypeAlias = int | _AnyPath
62+
63+
# ------------------------------------------------------------------------------------
64+
# HID_Tuple is used in multiple pyb.submodules
65+
HID_Tuple: TypeAlias = tuple[int, int, int, int, bytes]
66+
67+
# ------------------------------------------------------------------------------------
68+
# copied from _typeshed os.pyi as os.pyi cannot import from a module with the same nam@final
69+
@final
70+
class uname_result(structseq[str], tuple[str, str, str, str, str]):
71+
if sys.version_info >= (3, 8):
72+
__match_args__: Final = ("sysname", "nodename", "release", "version", "machine")
73+
74+
@property
75+
def sysname(self) -> str: ...
76+
@property
77+
def nodename(self) -> str: ...
78+
@property
79+
def release(self) -> str: ...
80+
@property
81+
def version(self) -> str: ...
82+
@property
83+
def machine(self) -> str: ...
84+
85+
# ------------------------------------------------------------------------------------
86+
87+
###########################
88+
# HashLib
89+
90+
# manual addition to hashlib.pyi
91+
92+
class _Hash(abc.ABC):
93+
"""
94+
Abstract base class for hashing algorithms that defines methods available in all algorithms.
95+
"""
96+
97+
def update(self, data: AnyReadableBuf, /) -> None:
98+
"""
99+
Feed more binary data into hash.
100+
"""
101+
102+
def digest(self) -> bytes:
103+
"""
104+
Return hash for all data passed through hash, as a bytes object. After this
105+
method is called, more data cannot be fed into the hash any longer.
106+
"""
107+
108+
def hexdigest(self) -> str:
109+
"""
110+
This method is NOT implemented. Use ``binascii.hexlify(hash.digest())``
111+
to achieve a similar effect.
112+
"""
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import sys
2+
from abc import abstractmethod
3+
from types import MappingProxyType
4+
from typing import AbstractSet as Set # noqa: Y022,Y038
5+
from typing import AsyncGenerator as AsyncGenerator
6+
from typing import AsyncIterable as AsyncIterable
7+
from typing import AsyncIterator as AsyncIterator
8+
from typing import Awaitable as Awaitable
9+
from typing import Callable as Callable
10+
from typing import Collection as Collection
11+
from typing import Container as Container
12+
from typing import Coroutine as Coroutine
13+
from typing import Generator as Generator
14+
from typing import Generic
15+
from typing import Hashable as Hashable
16+
from typing import ItemsView as ItemsView
17+
from typing import Iterable as Iterable
18+
from typing import Iterator as Iterator
19+
from typing import KeysView as KeysView
20+
from typing import Mapping as Mapping
21+
from typing import MappingView as MappingView
22+
from typing import MutableMapping as MutableMapping
23+
from typing import MutableSequence as MutableSequence
24+
from typing import MutableSet as MutableSet
25+
from typing import Protocol
26+
from typing import Reversible as Reversible
27+
from typing import Sequence as Sequence
28+
from typing import Sized as Sized
29+
from typing import TypeVar
30+
from typing import ValuesView as ValuesView
31+
from typing import final, runtime_checkable
32+
33+
__all__ = [
34+
"Awaitable",
35+
"Coroutine",
36+
"AsyncIterable",
37+
"AsyncIterator",
38+
"AsyncGenerator",
39+
"Hashable",
40+
"Iterable",
41+
"Iterator",
42+
"Generator",
43+
"Reversible",
44+
"Sized",
45+
"Container",
46+
"Callable",
47+
"Collection",
48+
"Set",
49+
"MutableSet",
50+
"Mapping",
51+
"MutableMapping",
52+
"MappingView",
53+
"KeysView",
54+
"ItemsView",
55+
"ValuesView",
56+
"Sequence",
57+
"MutableSequence",
58+
]
59+
if sys.version_info < (3, 14):
60+
from typing import ByteString as ByteString # noqa: Y057
61+
62+
__all__ += ["ByteString"]
63+
64+
if sys.version_info >= (3, 12):
65+
__all__ += ["Buffer"]
66+
67+
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
68+
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
69+
70+
@final
71+
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
72+
def __eq__(self, value: object, /) -> bool: ...
73+
if sys.version_info >= (3, 13):
74+
def isdisjoint(self, other: Iterable[_KT_co], /) -> bool: ...
75+
if sys.version_info >= (3, 10):
76+
@property
77+
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
78+
79+
@final
80+
class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented
81+
if sys.version_info >= (3, 10):
82+
@property
83+
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
84+
85+
@final
86+
class dict_items(ItemsView[_KT_co, _VT_co]): # undocumented
87+
def __eq__(self, value: object, /) -> bool: ...
88+
if sys.version_info >= (3, 13):
89+
def isdisjoint(self, other: Iterable[tuple[_KT_co, _VT_co]], /) -> bool: ...
90+
if sys.version_info >= (3, 10):
91+
@property
92+
def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ...
93+
94+
if sys.version_info >= (3, 12):
95+
@runtime_checkable
96+
class Buffer(Protocol):
97+
@abstractmethod
98+
def __buffer__(self, flags: int, /) -> memoryview: ...

0 commit comments

Comments
 (0)