Changing PySequence and PyMapping checks
![](https://secure.gravatar.com/avatar/fba529808415c562f826c85b9fe62991.jpg?s=120&d=mm&r=g)
There's a long time issue of trying to differentiate mappings and sequences in the C-API in a fast and reliable way. Due to recent changes, we might be able to do so at last, by checking tp_flags + str/bytes/bytearray which are considered unique. This might be a breaking change in the Stable ABI promise but one that can be considered a bug fix as the current behavior is lacking. What do you think? bpo: https://bugs.python.org/issue46376 Best regards, Bar Harel
![](https://secure.gravatar.com/avatar/d995b462a98fea412efa79d17ba3787a.jpg?s=120&d=mm&r=g)
Personally, I dislike the presumption that you state in the bpo, that typing and linters have changed things so that being a sequence is more closely tied to implementing the Sequence abc these days. I consider it a flaw in typing and linters if that’s the case, not something to embrace. Paul. PS Writing on mobile, sorry for any typos On Sat, 15 Jan 2022 at 04:26, Bar Harel <bzvi7919@gmail.com> wrote:
There's a long time issue of trying to differentiate mappings and sequences in the C-API in a fast and reliable way.
Due to recent changes, we might be able to do so at last, by checking tp_flags + str/bytes/bytearray which are considered unique.
This might be a breaking change in the Stable ABI promise but one that can be considered a bug fix as the current behavior is lacking.
What do you think?
bpo: https://bugs.python.org/issue46376
Best regards, Bar Harel _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/WTIKR5DG... Code of Conduct: http://python.org/psf/codeofconduct/
![](https://secure.gravatar.com/avatar/176220408ba450411279916772c36066.jpg?s=120&d=mm&r=g)
On Sat, Jan 15, 2022 at 2:06 AM Paul Moore <p.f.moore@gmail.com> wrote:
Personally, I dislike the presumption that you state in the bpo, that typing and linters have changed things so that being a sequence is more closely tied to implementing the Sequence abc these days. I consider it a flaw in typing and linters if that’s the case, not something to embrace.
Exactly. *maybe* the ecosystem has shifted, but Python is and I hope will always remain fundamentally a dynamically, duck typed language. We sure don't want to build any other assumptions into the C API itself. From the bpo: "Wherever you'd try to pass sre_parse.SubPattern, the linter will throw an error saying it's not a Sequence even if it fully behaves like one" Then that linter is broken, plain and simple. If a static type checker fails on that, then that's a limitation of static type checking. In fact, ideally it would pass even if it didn't fully support the Sequence ABC. For example, if I have a function that needs only to use one or two methods of a Sequence, I should not type the input as Sequence, and also expect it to work in a duck typed context. You can (and if you're being thorough, should) define a type that only requires the methods that you need. I think MyPy's Protocol can be used for that, for example. -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
participants (3)
-
Bar Harel
-
Christopher Barker
-
Paul Moore