| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2020 The Meson development team |
| 3 |
# Copyright © 2020-2024 Intel Corporation |
| 4 |
|
| 5 |
"""Meson specific typing helpers. |
| 6 |
|
| 7 |
Holds typing helper classes, such as the ImmutableProtocol classes |
| 8 |
""" |
| 9 |
|
| 10 |
__all__ = [ |
| 11 |
'Protocol', |
| 12 |
'ImmutableListProtocol' |
| 13 |
] |
| 14 |
|
| 15 |
import typing |
| 16 |
|
| 17 |
# We can change this to typing when we require python 3.8 |
| 18 |
from typing_extensions import Protocol |
| 19 |
|
| 20 |
|
| 21 |
T = typing.TypeVar('T') |
| 22 |
|
| 23 |
|
| 24 |
class StringProtocol(Protocol): |
| 25 |
def __str__(self) -> str: ... |
| 26 |
|
| 27 |
class SizedStringProtocol(Protocol, StringProtocol, typing.Sized): |
| 28 |
pass |
| 29 |
|
| 30 |
class ImmutableListProtocol(Protocol[T]): |
| 31 |
|
| 32 |
"""A protocol used in cases where a list is returned, but should not be |
| 33 |
mutated. |
| 34 |
|
| 35 |
This provides all of the methods of a Sequence, as well as copy(). copy() |
| 36 |
returns a list, which allows mutation as it's a copy and that's (hopefully) |
| 37 |
safe. |
| 38 |
|
| 39 |
One particular case this is important is for cached values, since python is |
| 40 |
a pass-by-reference language. |
| 41 |
""" |
| 42 |
|
| 43 |
def __iter__(self) -> typing.Iterator[T]: ... |
| 44 |
|
| 45 |
@typing.overload |
| 46 |
def __getitem__(self, index: int, /) -> T: ... |
| 47 |
@typing.overload |
| 48 |
def __getitem__(self, index: slice, /) -> typing.List[T]: ... |
| 49 |
|
| 50 |
def __contains__(self, item: T, /) -> bool: ... |
| 51 |
|
| 52 |
def __reversed__(self) -> typing.Iterator[T]: ... |
| 53 |
|
| 54 |
def __len__(self) -> int: ... |
| 55 |
|
| 56 |
def __add__(self, other: typing.List[T], /) -> typing.List[T]: ... |
| 57 |
|
| 58 |
def __eq__(self, other: typing.Any, /) -> bool: ... |
| 59 |
def __ne__(self, other: typing.Any, /) -> bool: ... |
| 60 |
def __le__(self, other: typing.Any, /) -> bool: ... |
| 61 |
def __lt__(self, other: typing.Any, /) -> bool: ... |
| 62 |
def __gt__(self, other: typing.Any, /) -> bool: ... |
| 63 |
def __ge__(self, other: typing.Any, /) -> bool: ... |
| 64 |
|
| 65 |
def count(self, item: T, /) -> int: ... |
| 66 |
|
| 67 |
def index(self, item: T, /) -> int: ... |
| 68 |
|
| 69 |
def copy(self) -> typing.List[T]: ... |