#Type hint help
1 messages ยท Page 1 of 1 (latest)
@property
def calibrate_offset(self):
x_offset, y_offset, z_offset = unpack("<bbb", self._read_register(_REG_OFSX, 3))
return x_offset, y_offset, z_offset
@calibrate_offset.setter
def calibrate_offset(self, val):
x_offset, y_offset, z_offset = val
self._write_register_byte(_REG_OFSX, x_offset)
self._write_register_byte(_REG_OFSY, y_offset)
self._write_register_byte(_REG_OFSZ, z_offset)```
@sonic oriole That's what needs to be done.
the offsets are ints if that matters.
Type hint help
Oh gotcha, just import Tuple from typing and type the return of getter and input of setter as Tuple[int, int, int]
Return of setter is None
Tuple's already imported, so that's good
That should be good then!
I have no idea how to add it. Is this right? ```py
@property
def calibrate_offset(self) -> Tuple[int, int, int]:
x_offset, y_offset, z_offset = unpack("<bbb", self._read_register(_REG_OFSX, 3))
return x_offset, y_offset, z_offset
@calibrate_offset.setter
def calibrate_offset(self, val: Tuple[int, int, int]) -> None:
x_offset, y_offset, z_offset = val
self._write_register_byte(_REG_OFSX, x_offset)
self._write_register_byte(_REG_OFSY, y_offset)
self._write_register_byte(_REG_OFSZ, z_offset)```
Yup!

Of course!
If you have large ones (or something that returns arbitrarily length tuple, maybe they can change depending on the arguments), as long as they're homogeneous in typing you can do Tuple[int, ...] is my understanding. I typically write it out so it's clear exactly how many are returned, especially if it's always the same number.
Oh nice.
But yeah this is clearer to me, and therefore likely to other newbies. So I'll stick with it.
Sweet! Glad I could help!
Much appreciated!
@sonic oriole You available again for another type hint question?
I'm going to ping Foamyguy. ๐
oh!
I'm struggling to figure out Arduino so please give me Python ๐
OK so, is this right? Even though address=None for reasons? py def __init__(self, i2c: busio.I2C, address: int = None): super().__init__( i2c, address if address is not None else _ADXL375_DEFAULT_ADDRESS )
If a parameter accepts None it should be Optional (importable from typing)
Hmm.
So address as written here should be Optional[int]
It doesn't really want none, it really needs an address, but Pylint is a jerk because the address is the same for this and the inherited version.
Still Optional?
Pylint thinks I'm not changing anything because the addresses are the same
Can you make the default address _ADXL375_DEFAULT_ADDRESS instead?
But I needed it to be extensible in case they change the address on the next one.
Ohhhhh
The next 37x version
So Pylint right now complains that I don't need to have it in the super
because it thinks I'm not changing anything from the inherited class
And won't let me do it right without this workaround.
Got it so this implementation is intentionally not wanting to define a default explicit address for the class
Yeah. To get around Pylint basically.
That's why it's setting it to DEFAULT_ADDRESS in the super with that nonsense
Got it, then yeah you should type the __init__ parameter address as Optional[int]
Ok
You can also type it as returning None
def __init__(self, i2c: busio.I2C, address: Optional[int] = None):
super().__init__(
i2c, address if address is not None else _ADXL375_DEFAULT_ADDRESS
)``` ???
and imported it above
Yup!
Excellent! Thank you
Trying not to make more work for folks by not type hinting the new library I wrote. Since it's a shorty. And I was able to copy most of it from the lib it's based on.
Yeah, I'm trying to keep posted on new libraries for that reason, so that's awesome
Good on you!
And no problem! Back to whatever the heck a text preprocessor is!