Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type hint bug for frozenbitarray? #175

Open
larstokle opened this issue Apr 28, 2022 · 3 comments
Open

Type hint bug for frozenbitarray? #175

larstokle opened this issue Apr 28, 2022 · 3 comments

Comments

@larstokle
Copy link

It seems that type checkers give frozenbitarray() & bitarray() -> bitarray etc., whereas the implementation gives frozenbitarray. The implementation is a nice feature when one knows about it, and it would be nice if the type hints would reflect this.

It looks to me, after reading some mypy docs, like it perhaps can be solved with some TypeVar bound to bitarray for self and the return type in __init__.pyi. However, I do not know enough about bitarray and type hinting to know if this would be the way to go.

@ilanschnell
Copy link
Owner

Thank for your using bitarray! I'm not too familiar with mypy myself, but it would indeed be nice if mypy or other type checker could reflect this. I just wrote a file .t.py:

from bitarray import bitarray, frozenbitarray
a: frozenbitarray = frozenbitarray() & bitarray()
print(type(a))

Then:

$ python t.py
<class 'bitarray.frozenbitarray'>
$ mypy t.py
t.py:2: error: Incompatible types in assignment (expression has type "bitarray", variable has type "frozenbitarray")
Found 1 error in 1 file (checked 1 source file)

So mypy thinks a is of type bitarray even though it really is frozenbitarray.

I'm not familiar enough with type hinting to solve this, but I'm visiting PyCon 2022 over the next few days, and I'm sure someone can tell me the answer.

@larstokle
Copy link
Author

That was a quick response! 👍
Yes, this is, in essence, what I am experiencing. :)

I see that this also extends to frozenbitarray() & frozenbitarray() which type checks to bitarray, and also to other operators like | and ^, which I guess makes sense from the implementation of frozenbitarray.

If you want to try something, I suspect that making the following changes to the right methods in __init__.pyi should type check correctly:

BA_T1 = TypeVar("BA_T1", bound=bitarray)
BA_T2 = TypeVar("BA_T2", bound=bitarray)
[...]
    def __and__(self: BA_T1, other: BA_T2) -> BA_T1: ...

But do not take my word for it, as I guess there might be some unwanted consequences like usual when I am trying to generalise types hints like this. As you say, the people at PyCon probably have better input on this.

@ilanschnell
Copy link
Owner

What you are suggesting makes sense, as frozenbitarray is a subtype of bitarray. However, I still get the same error (I'm using the latest mypy 0.950). Let's see what the mypy expcerts say...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants