scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
404 stars 68 forks source link

Type Hint Issue with BitArray Constructor #276

Closed meggiman closed 1 year ago

meggiman commented 1 year ago

Hi there, First of all, let me thank you for this amazing library. It is my go-to solution whenever I am manipulating binary data within EDA Python scripts. However, while cleaning up one of my repositories and adding static type checking, I noticed that there is some issue with the constructor type hinting in bitstring:

The type hints of Bits 's __new__ function are causing issues with the derived class ByteArray. Since ByteArray does not provide its own implementation of __new__, it inherits the base classes version which is type hinted to always return a Bits instance. This causes issues during static type checking where now the following statement does not pass:

x: BitArray = BitArray("0x0")

Pylance/Pyright error: Expression of type "Bits" cannot be assigned to declared type "BitArray"

I created a quick fix. See the linked PR below.

scott-griffiths commented 1 year ago

Hi, thanks for the fix. I was actually looking at this just yesterday and read about the Self type for the first time, so it's useful to see it working.

Adding this would add a dependency on the external typing_extensions module, which I'm hesitant to do (perhaps I shouldn't be, but the next version of bitstring will be the first with any dependencies and I was hoping to keep it to just the one).

I'll take a look at what's the minimal I need to do when I get the chance, but a fix should go in for the next version.