Skip to content

Fixing make stub warnings and mypy issues #3621

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 2 commits into from
Oct 30, 2020
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
8 changes: 4 additions & 4 deletions shared-bindings/canio/CAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//| loopback: bool = False,
//| silent: bool = False,
//| auto_restart: bool = False,
//| ):
//| ) -> None:
//| """A common shared-bus protocol. The rx and tx pins are generally
//| connected to a transceiver which controls the H and L pins on a
//| shared bus.
Expand Down Expand Up @@ -171,7 +171,7 @@ STATIC const mp_obj_property_t canio_can_receive_error_count_obj = {
(mp_obj_t)mp_const_none},
};

//| state: State
//| state: BusState
//| """The current state of the bus. (read-only)"""
STATIC mp_obj_t canio_can_state_get(mp_obj_t self_in) {
canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
Expand Down Expand Up @@ -291,7 +291,7 @@ STATIC const mp_obj_property_t canio_can_loopback_obj = {
};


//| def send(message: Union[RemoteTransmissionRequest, Message]) -> None:
//| def send(self, message: Union[RemoteTransmissionRequest, Message]) -> None:
//| """Send a message on the bus with the given data and id.
//| If the message could not be sent due to a full fifo or a bus error condition, RuntimeError is raised.
//| """
Expand Down Expand Up @@ -352,7 +352,7 @@ STATIC mp_obj_t canio_can_enter(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_can_enter_obj, canio_can_enter);

//| def __exit__(self, unused1, unused2, unused3) -> None:
//| def __exit__(self, unused1: Optional[Type[BaseException]], unused2: Optional[BaseException], unused3: Optional[TracebackType]) -> None:
//| """Calls deinit()"""
//| ...
STATIC mp_obj_t canio_can_exit(size_t num_args, const mp_obj_t args[]) {
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/canio/Listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ STATIC mp_obj_t canio_listener_enter(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_enter_obj, canio_listener_enter);

//| def __exit__(self, unused1, unused2, unused3) -> None:
//| def __exit__(self, unused1: Optional[Type[BaseException]], unused2: Optional[BaseException], unused3: Optional[TracebackType]) -> None:
//| """Calls deinit()"""
//| ...
STATIC mp_obj_t canio_listener_exit(size_t num_args, const mp_obj_t args[]) {
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/canio/Match.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//| """Describe CAN bus messages to match"""
//|
//|
//| def __init__(self, id: int, *, mask: Optional[int] = None, extended: bool = False):
//| def __init__(self, id: int, *, mask: Optional[int] = None, extended: bool = False) -> None:
//| """Construct a Match with the given properties.
//|
//| If mask is not None, then the filter is for any id which matches all
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/canio/Message.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "py/runtime.h"

//| class Message:
//| def __init__(self, id: int, data: bytes, *, extended: bool = False):
//| def __init__(self, id: int, data: bytes, *, extended: bool = False) -> None:
//| """Construct a Message to send on a CAN bus.
//|
//| :param int id: The numeric ID of the message
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/canio/RemoteTransmissionRequest.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "py/runtime.h"

//| class RemoteTransmissionRequest:
//| def __init__(self, id: int, length: int, *, extended: bool = False):
//| def __init__(self, id: int, length: int, *, extended: bool = False) -> None:
//| """Construct a RemoteTransmissionRequest to send on a CAN bus.
//|
//| :param int id: The numeric ID of the requested message
Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/displayio/Bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
return mp_const_none;
}

//| def blit(self, x: int, y: int, source_bitmap: bitmap, *, x1: int, y1: int, x2: int, y2: int, skip_index: int) -> None:
//| def blit(self, x: int, y: int, source_bitmap: Bitmap, *, x1: int, y1: int, x2: int, y2: int, skip_index: int) -> None:
//| """Inserts the source_bitmap region defined by rectangular boundaries
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
//|
Expand Down Expand Up @@ -274,7 +274,7 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 4, displayio_bitmap_obj_blit);
// `displayio_bitmap_obj_blit` requires at least 4 arguments

//| def fill(self, value: Any) -> None:
//| def fill(self, value: int) -> None:
//| """Fills the bitmap with the supplied palette index value."""
//| ...
//|
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/displayio/Display.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "shared-module/displayio/__init__.h"
#include "supervisor/shared/translate.h"

//| _DisplayBus = Union[FourWire, ParallelBus, I2CDisplay]
//| _DisplayBus = Union['FourWire', 'ParallelBus', 'I2CDisplay']
//| """:py:class:`FourWire`, :py:class:`ParallelBus` or :py:class:`I2CDisplay`"""
//|

Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/socketpool/Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ STATIC mp_obj_t socketpool_socket_close(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_close_obj, socketpool_socket_close);

//| def connect(self, address: tuple) -> None:
//| def connect(self, address: Tuple[str, int]) -> None:
//| """Connect a socket to a remote address
//|
//| :param ~tuple address: tuple of (remote_address, remote_port)"""
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/socketpool/SocketPool.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ STATIC mp_obj_t socketpool_socketpool_socket(size_t n_args, const mp_obj_t *pos_
}
MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_socket_obj, 1, socketpool_socketpool_socket);

//| def getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> tuple:
//| def getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> Tuple[int, int, int, str, Tuple[str, int]]:
//| """Gets the address information for a hostname and port
//|
//| Returns the appropriate family, socket type, socket protocol and
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/ssl/SSLContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ STATIC mp_obj_t ssl_sslcontext_make_new(const mp_obj_type_t *type, size_t n_args
return MP_OBJ_FROM_PTR(s);
}

//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: str = None) -> socketpool.Socket:
//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: Optional[str] = None) -> socketpool.Socket:
//| """Wraps the socket into a socket-compatible class that handles SSL negotiation.
//| The socket must be of type SOCK_STREAM."""
//| ...
Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const mp_obj_property_t wifi_radio_mac_address_obj = {
};


//| def start_scanning_networks(self, *, start_channel=1, stop_channel=11) -> Iterable[Network]:
//| def start_scanning_networks(self, *, start_channel: int = 1, stop_channel: int = 11) -> Iterable[Network]:
//| """Scans for available wifi networks over the given channel range. Make sure the channels are allowed in your country."""
//| ...
//|
Expand Down Expand Up @@ -283,7 +283,7 @@ const mp_obj_property_t wifi_radio_ap_info_obj = {
(mp_obj_t)&mp_const_none_obj },
};

//| def ping(self, ip, *, timeout: float = 0.5) -> float:
//| def ping(self, ip: ipaddress.IPv4Address, *, timeout: Optional[float] = 0.5) -> float:
//| """Ping an IP to test connectivity. Returns echo time in seconds.
//| Returns None when it times out."""
//| ...
Expand Down
9 changes: 8 additions & 1 deletion tools/extract_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@


IMPORTS_IGNORE = frozenset({'int', 'float', 'bool', 'str', 'bytes', 'tuple', 'list', 'set', 'dict', 'bytearray', 'slice', 'file', 'buffer', 'range', 'array', 'struct_time'})
IMPORTS_TYPING = frozenset({'Any', 'Optional', 'Union', 'Tuple', 'List', 'Sequence', 'NamedTuple', 'Iterable', 'Iterator', 'Callable', 'AnyStr', 'overload'})
IMPORTS_TYPING = frozenset({'Any', 'Optional', 'Union', 'Tuple', 'List', 'Sequence', 'NamedTuple', 'Iterable', 'Iterator', 'Callable', 'AnyStr', 'overload', 'Type'})
IMPORTS_TYPES = frozenset({'TracebackType'})
CPY_TYPING = frozenset({'ReadableBuffer', 'WriteableBuffer', 'AudioSample', 'FrameBuffer'})


Expand Down Expand Up @@ -63,6 +64,7 @@ def find_stub_issues(tree):
def extract_imports(tree):
modules = set()
typing = set()
types = set()
cpy_typing = set()

def collect_annotations(anno_tree):
Expand All @@ -74,6 +76,8 @@ def collect_annotations(anno_tree):
continue
elif node.id in IMPORTS_TYPING:
typing.add(node.id)
elif node.id in IMPORTS_TYPES:
types.add(node.id)
elif node.id in CPY_TYPING:
cpy_typing.add(node.id)
elif isinstance(node, ast.Attribute):
Expand All @@ -94,6 +98,7 @@ def collect_annotations(anno_tree):
return {
"modules": sorted(modules),
"typing": sorted(typing),
"types": sorted(types),
"cpy_typing": sorted(cpy_typing),
}

Expand Down Expand Up @@ -181,6 +186,8 @@ def convert_folder(top_level, stub_directory):
# Add import statements
imports = extract_imports(tree)
import_lines = ["from __future__ import annotations"]
if imports["types"]:
import_lines.append("from types import " + ", ".join(imports["types"]))
if imports["typing"]:
import_lines.append("from typing import " + ", ".join(imports["typing"]))
if imports["cpy_typing"]:
Expand Down