Based on the documentation for Python Typing, these are the ones that deal with Tuples...
Tuple[()]
- empty tupleTuple[int, int]
- tuple of two int objectsTuple[int, ...]
- tuple of an arbitrary number of int objects
I want to create a type where I have a Tuple
with at least 8 int
s.
My goal is somewhere in between the last two types (Tuple[int, int]
, and Tuple[int, ...]
).
If the tuple has...
- 8 ints? Good
- 9 ints? Good
- 32 ints? Good
- 4 ints? Bad
- 1 int? Bad
Is this even possible where PyCharm will not throw warnings? Here is what I tried below. However, PyCharm will "incorrectly" give me warnings on the assert
statement
# Type Alias
Byte = Tuple[int, int, int, int, int, int, int, int] # 8 bits
Nibble = Tuple[int, int, int, int] # 4 bits
# BytePlus Type is not correct. The following code does not work
BytePlus = Tuple[int, int, int, int, int, int, int, int, ...] # 8+ bits
def adder(byte1: Union[Byte, BytePlus], byte2: Byte) -> Byte:
pass
# Incorrect warnings for parameters.
# What I see...
# - parameter 1 (Byte | BytePlus) has no warning, but it should. nibble = 4 bits
# - parameter 2 (Byte) has a warning, good, but it will have a warning for 9 bits
assert adder(nibble(4), nibble(10)) == byte(14)
Here is the code for nibble()
and byte()
if you want...
def byte(number) -> Byte:
return (
(number & 128) // 128,
(number & 64) // 64,
(number & 32) // 32,
(number & 16) // 16,
(number & 8) // 8,
(number & 4) // 4,
(number & 2) // 2,
(number & 1) // 1
)
def nibble(number) -> Nibble:
return (
(number & 8) // 8,
(number & 4) // 4,
(number & 2) // 2,
(number & 1) // 1
)