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

Add bytes.__new__() accepting object implementing .__getitem__() #5143

Closed
wants to merge 1 commit into from

Conversation

altendky
Copy link
Contributor

@altendky altendky commented Mar 27, 2021

Draft for:

  • Passing CI
  • Confirmation if this addition is wanted or not

Python 3.9.1 (default, Jan  6 2021, 10:22:00) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C:
...     def __getitem__(self, item):
...         if item == 0:
...             return 1
...         raise IndexError()
... 
>>> c = C()
>>> bytes(c)
b'\x01'

https://mypy-play.net/?mypy=latest&python=3.9&gist=9043979f47bb2c768999fdca30ac4b92

class C:
 def __getitem__(self, item):
     if item == 0:
         return 1
     raise IndexError()

c = C()
bytes(c)
main.py:8: error: No overload variant of "bytes" matches argument type "C"
main.py:8: note: Possible overload variants:
main.py:8: note:     def __new__(cls, ints: Iterable[int]) -> bytes
main.py:8: note:     def __new__(cls, length: int) -> bytes
main.py:8: note:     def __new__(cls, o: SupportsBytes) -> bytes
main.py:8: note:     <2 more non-matching overloads not shown>
Found 1 error in 1 file (checked 1 source file)

I don't think this will actually fix the issue I am exploring, but for the sake of linkage it is python-qt-tools/PyQt5-stubs#143 and relates to getting Mypy to be happy with bytes(PyQt5.QtCore.QByteArray()).

@JelleZijlstra
Copy link
Member

This is actually just the old-style Iterable protocol, which we've mostly avoided doing much with in typing.

@altendky altendky marked this pull request as draft March 27, 2021 16:30
@altendky
Copy link
Contributor Author

I think my actual case relates to the buffer C-API interface, so I can't say I've got a big drive to push this through beyond completeness. Are you suggesting that you think this shouldn't be added? Or, should I go ahead and try to fix the issues?

@JelleZijlstra
Copy link
Member

See python/typing#593 for background on the buffer protocol. I think we have a viable implementation plan, just no implementation.

I wouldn't be opposed to merging this as a special case if you can get it to work. Perhaps it would be simpler if you used a protocol that hardcoded the desired types instead of a generic protocol.

@altendky
Copy link
Contributor Author

QByteArray does have .__getitem__() but it returns a one element bytes object so this won't help me there. This is just a collateral PR at this point. I don't know the reasoning behind not describing this old-style iterable protocol here in typeshed, but given the statement that it has been avoided I don't think this really deserves an exception. If I have misunderstood and the change is particularly wanted, reopen it and I'll take a stab.

@altendky altendky closed this Mar 28, 2021
@JelleZijlstra
Copy link
Member

To be clear, I think we've mostly avoided it because it's rarely useful and somewhat hard to describe. If there is a use case, I wouldn't be opposed to adding support for it.

@altendky
Copy link
Contributor Author

@JelleZijlstra, thanks for the consideration and openness.

If anyone else finds this and needs it, feel free to fork and try to fix it. Or, poke me and maybe I'll have some time.

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

Successfully merging this pull request may close these issues.

2 participants