#Type hint help

1 messages ยท Page 1 of 1 (latest)

tawdry prism
#
    @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

sonic oriole
#

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

tawdry prism
#

Tuple's already imported, so that's good

sonic oriole
#

That should be good then!

tawdry prism
#

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)```
sonic oriole
#

Yup!

tawdry prism
#

Oh snap.

#

Ok!

#

Thank you!

sonic oriole
#

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.

tawdry prism
#

Oh nice.

#

But yeah this is clearer to me, and therefore likely to other newbies. So I'll stick with it.

sonic oriole
#

Sweet! Glad I could help!

tawdry prism
#

Much appreciated!

tawdry prism
#

@sonic oriole You available again for another type hint question?

#

I'm going to ping Foamyguy. ๐Ÿ™‚

sonic oriole
#

Oops just saw this!

#

What's up?

tawdry prism
#

oh!

sonic oriole
#

I'm struggling to figure out Arduino so please give me Python ๐Ÿ˜‚

tawdry prism
#

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 )

sonic oriole
#

If a parameter accepts None it should be Optional (importable from typing)

tawdry prism
#

Hmm.

sonic oriole
#

So address as written here should be Optional[int]

tawdry prism
#

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?

sonic oriole
#

Is there a default int address you can use as the default instead?

#

Oh!

tawdry prism
#

Pylint thinks I'm not changing anything because the addresses are the same

sonic oriole
#

Can you make the default address _ADXL375_DEFAULT_ADDRESS instead?

tawdry prism
#

But I needed it to be extensible in case they change the address on the next one.

sonic oriole
#

Ohhhhh

tawdry prism
#

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.

sonic oriole
#

Got it so this implementation is intentionally not wanting to define a default explicit address for the class

tawdry prism
#

Yeah. To get around Pylint basically.

#

That's why it's setting it to DEFAULT_ADDRESS in the super with that nonsense

sonic oriole
#

Got it, then yeah you should type the __init__ parameter address as Optional[int]

tawdry prism
#

Ok

sonic oriole
#

You can also type it as returning None

tawdry prism
#
    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

sonic oriole
#

Yup!

tawdry prism
#

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.

sonic oriole
#

Yeah, I'm trying to keep posted on new libraries for that reason, so that's awesome

tawdry prism
#

Good on you!

sonic oriole
#

And no problem! Back to whatever the heck a text preprocessor is!