Skip to content

Typing fixes #35

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 9 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
19 changes: 17 additions & 2 deletions circuitpython_typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Typing.git"

import array
from typing import Union, Optional
from typing_extensions import Protocol, TypeAlias # Safety import for Python 3.7
from typing import TYPE_CHECKING, Optional, Union

# Protocol was introduced in Python 3.8, TypeAlias in 3.10
from typing_extensions import Protocol, TypeAlias

if TYPE_CHECKING:
import alarm
import audiocore
import audiomixer
import audiomp3
import rgbmatrix
import synthio
import ulab
from alarm.pin import PinAlarm
from alarm.time import TimeAlarm
from ulab.numpy import ndarray


# Lists below are alphabetized.

Expand Down
3 changes: 2 additions & 1 deletion circuitpython_typing/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
* Author(s): Alec Delaney
"""

from adafruit_requests import Response

# Protocol was introduced in Python 3.8.
from typing_extensions import Protocol
from adafruit_requests import Response


class HTTPProtocol(Protocol):
Expand Down
6 changes: 5 additions & 1 deletion circuitpython_typing/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def value(self) -> float:
on the specifics of the class.
"""

# TODO: this should be `value(self, input_value: float, /)` but can't
# because currently mpy files are built and the `/` param isn't supported
# in micro-python.
# https://github.com/adafruit/Adafruit_CircuitPython_Typing/issues/36
# pylint: disable=no-self-use,unused-argument
@value.setter
def value(self, input_value: float, /):
def value(self, input_value: float):
...
3 changes: 2 additions & 1 deletion circuitpython_typing/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* Author(s): Alec Delaney
"""

from typing import Tuple, Union

# Protocol was introduced in Python 3.8, TypeAlias in 3.10
from typing import Union, Tuple
from typing_extensions import Protocol, TypeAlias

ColorBasedColorUnion: TypeAlias = Union[int, Tuple[int, int, int]]
Expand Down
6 changes: 4 additions & 2 deletions circuitpython_typing/pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
* Author(s): Alec Delaney
"""

from typing import Tuple, Optional, Callable
from typing_extensions import Protocol # Safety import for Python 3.7
from typing import Callable, Optional, Tuple

# Protocol was introduced in Python 3.8
from typing_extensions import Protocol


class PixelAccess(Protocol):
Expand Down
17 changes: 15 additions & 2 deletions circuitpython_typing/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# Protocol was introduced in Python 3.8, TypeAlias in 3.10
from typing_extensions import Protocol, TypeAlias


# Based on https://github.com/python/typeshed/blob/master/stdlib/_socket.pyi

__all__ = [
Expand Down Expand Up @@ -126,4 +125,18 @@ def TLS_MODE(self) -> int: # pylint: disable=invalid-name
"""Constant representing that a socket's connection mode is TLS."""


SSLContextType: TypeAlias = Union[SSLContext, "_FakeSSLContext"]
# pylint: disable=too-few-public-methods
class _FakeSSLSocket:
"""Describes the structure every fake SSL socket type must have."""


class _FakeSSLContext:
"""Describes the structure every fake SSL context type must have."""

def wrap_socket(
self, socket: CircuitPythonSocketType, server_hostname: Optional[str] = None
) -> _FakeSSLSocket:
"""Wrap socket and return a new one that uses the methods from the original."""


SSLContextType: TypeAlias = Union[SSLContext, _FakeSSLContext]