Am 19.05.20 um 19:43 schrieb Jelle Zijlstra:


El mar., 19 may. 2020 a las 10:29, Guido van Rossum (<guido@python.org>) escribió:


AFAIK mypy doesn't actually know anything about bytes except what's said about it in typeshed. So maybe we can just define a proper class representing the buffer protocol in an appropriate place (maybe stick it in types.pyi with an underscore name because it's not in the stdlib types.py) and ensure that bytes, bytearray etc. inherit from it? Would that kind of class hierarchy help?

Yes, that's the idea, but nobody so far has stepped up to actually do it. I would want it to be in typing(_extensions) with a public name so that third-party libraries can also use it.

A slightly troublesome aspect is that the "buffer protocol" is C-only and there are no Python methods that correspond to it. A standard Protocol therefore doesn't work; instead we should create an empty class that bytes and friends inherit from. I proposed this in https://github.com/python/typing/issues/593#issuecomment-440327001.

I hadn't realized that the solution was this straight forward. Sounds like a good solution.

 - Sebastian