Skip to content

Commit 2bd5d7e

Browse files
committed
Add wpistruct typing utilities
1 parent 3df8d8b commit 2bd5d7e

File tree

1 file changed

+23
-0
lines changed
  • subprojects/robotpy-wpiutil/wpiutil/wpistruct

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import ClassVar, Protocol
2+
3+
try:
4+
from typing import TypeGuard
5+
except ImportError:
6+
try:
7+
from typing_extensions import TypeGuard
8+
except ImportError:
9+
# Runtime fallback for Python 3.9 without typing_extensions
10+
class TypeGuard:
11+
def __class_getitem__(cls, key):
12+
return bool
13+
14+
15+
class StructSerializable(Protocol):
16+
"""Any type that can be serialized or deserialized as a WPILib Struct."""
17+
18+
WPIStruct: ClassVar
19+
20+
21+
def is_wpistruct_type(cls: type) -> TypeGuard[type[StructSerializable]]:
22+
"""Returns True if the given type supports WPILib Struct serialization."""
23+
return hasattr(cls, "WPIStruct")

0 commit comments

Comments
 (0)