[Python-ideas] Re: Add Subscriptable ABC

2019-10-11 Thread Neil Girdhar
On Fri., Oct. 11, 2019, 2:38 a.m. Neil Girdhar, wrote: > > > On Fri, Oct 11, 2019 at 2:28 AM Andrew Barnert wrote: > >> > On Oct 10, 2019, at 22:13, Neil Girdhar wrote: >> > >> > That's funny, I always thought of that as legacy. The iterator >> protocol has been a special case in so many

[Python-ideas] Re: Add Subscriptable ABC

2019-10-10 Thread Neil Girdhar
On Monday, September 30, 2019 at 12:24:30 AM UTC-4, Andrew Barnert via Python-ideas wrote: > > On Sep 29, 2019, at 16:41, Steven D'Aprano > wrote: > > > >> On Sun, Sep 29, 2019 at 11:27:32AM +0100, Oscar Benjamin wrote: > >> > >> That's the point that I would make as well. What can you do

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Greg Ewing
Chris Angelico wrote: Python's base object has a lot of extremely useful functionality, Yes, I agree that all that stuff is useful. I'm not saying that Python's object type shouldn't have it, just that it's incompatible with having a strict type system. -- Greg

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Andrew Barnert via Python-ideas
On Oct 1, 2019, at 15:59, Chris Angelico wrote: > >> On Wed, Oct 2, 2019 at 8:51 AM Greg Ewing >> wrote: >> >> Chris Angelico wrote: >>> The question then would be: why is object() not hashable? >> >> It's not hashable because it's supposed to be the ultimate >> base type for all other

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Steven D'Aprano
On Tue, Oct 01, 2019 at 09:47:56AM -0700, Andrew Barnert via Python-ideas wrote: > On Oct 1, 2019, at 06:23, Richard Musil wrote: > > > > Where *virtual* is a hyperlink to an ABC definition. > > > > `issubclass` will be fine without virtual and I am not sure why > > virtual has been added to

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Oscar Benjamin
On Tue, 1 Oct 2019 at 11:24, Steven D'Aprano wrote: > > On Mon, Sep 30, 2019 at 06:00:44PM +0900, Stephen J. Turnbull wrote: > > > Technical questions: does "Subscriptable" mean non-negative ints only, > > or does it include the negative "count from the end" protocol? How > > about slices? > >

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Andrew Barnert via Python-ideas
On Oct 1, 2019, at 16:01, Christopher Barker wrote: > > > But I can’t see an argument that something called Subscriptable could (or > > should) actually mean indexable rather than subscriptable. > > Now we are getting into naming things, which is hard, but not the critical > step. > > But as

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Christopher Barker
> But I can’t see an argument that something called Subscriptable could (or should) actually mean indexable rather than subscriptable. > Now we are getting into naming things, which is hard, but not the critical step. But as you point out, it’s actually not that clear exactly what the OP (or

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Chris Angelico
On Wed, Oct 2, 2019 at 8:51 AM Greg Ewing wrote: > > Chris Angelico wrote: > > The question then would be: why is object() not hashable? > > It's not hashable because it's supposed to be the ultimate > base type for all other objects, and not every object is > hashable. > > It only seems odd if

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Greg Ewing
Chris Angelico wrote: The question then would be: why is object() not hashable? It's not hashable because it's supposed to be the ultimate base type for all other objects, and not every object is hashable. It only seems odd if you're used to the idea that you get a bunch of default behaviours

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Andrew Barnert via Python-ideas
On Oct 1, 2019, at 11:53, Richard Musil wrote: > >> On Tue, Oct 1, 2019 at 6:48 PM Andrew Barnert wrote: > >> Virtual subclassing is key to the way ABCs work. It would be nice if it were >> explained better in the docs, but I suppose usually people don’t need to >> know the details unless

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Chris Angelico
On Wed, Oct 2, 2019 at 8:29 AM Greg Ewing wrote: > > Chris Angelico wrote: > > The hashability issue is a logical consequence of accepting that the > > above violations are reasonable and practically useful. > > A more principled way to handle this wouild be for object not > to be hashable, and

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Greg Ewing
Chris Angelico wrote: The hashability issue is a logical consequence of accepting that the above violations are reasonable and practically useful. A more principled way to handle this wouild be for object not to be hashable, and have another base type for hashable objects. Hashable would then

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Christopher Barker
On Tue, Oct 1, 2019 at 7:02 AM Greg Ewing wrote: > > >>> issubclass(list, object) > > True > > >>> issubclass(object, collections.abc.Hashable) > > True > > >>> issubclass(list, collections.abc.Hashable) > > False > > This isn't really the fault of

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Chris Angelico
On Wed, Oct 2, 2019 at 5:18 AM Ben Rudiak-Gould wrote: > > On Tue, Oct 1, 2019 at 9:26 AM Andrew Barnert wrote: > > On Sep 30, 2019, at 23:46, Ben Rudiak-Gould wrote: > > > ABC membership is a subtype relationship in some sense, and ordinary > > > Python subclassing is a subtype relationship

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Ben Rudiak-Gould
On Tue, Oct 1, 2019 at 9:26 AM Andrew Barnert wrote: > On Sep 30, 2019, at 23:46, Ben Rudiak-Gould wrote: > > ABC membership is a subtype relationship in some sense, and ordinary Python > > subclassing is a subtype relationship in some sense, but they aren't quite > > the same sense, > > But

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Richard Musil
On Tue, Oct 1, 2019 at 6:48 PM Andrew Barnert wrote: > Virtual subclassing is key to the way ABCs work. It would be nice if it > were explained better in the docs, but I suppose usually people don’t need > to know the details unless they’re actually getting involved in something > like designing

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread M.-A. Lemburg
On 30.09.2019 01:41, Steven D'Aprano wrote: > [...] > But subscripting (indexing) is a conspicuous exception. There's no ABC > for testing whether something supports subscripting. +1 for adding an ABC to signal support for indexing. BTW: Something I miss in the ABCs is the distinction between

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Andrew Barnert via Python-ideas
On Oct 1, 2019, at 06:23, Richard Musil wrote: > > Where *virtual* is a hyperlink to an ABC definition. > > `issubclass` will be fine without virtual and I am not sure why virtual has > been added to it Virtual subclassing is key to the way ABCs work. It would be nice if it were explained

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Andrew Barnert via Python-ideas
On Sep 30, 2019, at 23:46, Ben Rudiak-Gould wrote: > > On Mon, Sep 30, 2019 at 10:08 AM Andrew Barnert via Python-ideas > wrote: >> Also, what we’re checking for really is subtyping. > > Is it? Subtyping in type theory satisfies some axioms, one of which is > transitivity. The addition of the

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Greg Ewing
On Tue, Oct 1, 2019 at 8:48 AM Ben Rudiak-Gould > wrote: Is it? Subtyping in type theory satisfies some axioms, one of which is transitivity. The addition of the ABCs broke transitivity: >>> issubclass(list, object) True >>>

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Richard Musil
On Tue, Oct 1, 2019 at 8:48 AM Ben Rudiak-Gould wrote: > Is it? Subtyping in type theory satisfies some axioms, one of which is > transitivity. The addition of the ABCs broke transitivity: > > >>> issubclass(list, object) > True > >>> issubclass(object, collections.abc.Hashable) >

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Steven D'Aprano
On Mon, Sep 30, 2019 at 06:00:44PM +0900, Stephen J. Turnbull wrote: > Technical questions: does "Subscriptable" mean non-negative ints only, > or does it include the negative "count from the end" protocol? How > about slices? It means the class defines a __getitem__ method. Like __call__, the

[Python-ideas] Re: Add Subscriptable ABC

2019-10-01 Thread Ben Rudiak-Gould
On Mon, Sep 30, 2019 at 10:08 AM Andrew Barnert via Python-ideas wrote: > Also, what we’re checking for really is subtyping. Is it? Subtyping in type theory satisfies some axioms, one of which is transitivity. The addition of the ABCs broke transitivity: >>> issubclass(list, object)

[Python-ideas] Re: Add Subscriptable ABC

2019-09-30 Thread Christopher Barker
> > > > That applies to many interface checks. Both the built-in callable() and > the Callable ABC don't tell you what arguments the object accepts, or > what it does; the Sequence ABC check doesn't tell you what valid indexes > are accepted. Sure — and Callable is probably the closest ABC to

[Python-ideas] Re: Add Subscriptable ABC

2019-09-30 Thread Andrew Barnert via Python-ideas
On Sep 30, 2019, at 02:38, Paul Moore wrote: > > It's (in my view) sad that the simple hasattr test is no longer > sufficient, and in particular that if you want robustness, Python has > changed to the point where a pseudo subclass check is the "right" way > to check for an object that has

[Python-ideas] Re: Add Subscriptable ABC

2019-09-30 Thread Paul Moore
On Mon, 30 Sep 2019 at 10:02, Stephen J. Turnbull wrote: > > Steven D'Aprano writes: > > On Sun, Sep 29, 2019 at 11:27:32AM +0100, Oscar Benjamin wrote: > > > > > That's the point that I would make as well. What can you do with an > > > object that is only known to be Subscriptable? > > > >

[Python-ideas] Re: Add Subscriptable ABC

2019-09-30 Thread Stephen J. Turnbull
Steven D'Aprano writes: > On Sun, Sep 29, 2019 at 11:27:32AM +0100, Oscar Benjamin wrote: > > > That's the point that I would make as well. What can you do with an > > object that is only known to be Subscriptable? > > I can subscript it. What did you expect the answer to be? Technical

[Python-ideas] Re: Add Subscriptable ABC

2019-09-29 Thread Brendan Barnwell
On 2019-09-29 21:15, Steven D'Aprano wrote: Clearly we can't have an ABC for every imaginable combination of magic methods. There would be hundreds. What would we name them? We should (and do!) have ABCs for the most common combinations, like Sequence, MutableMapping, Container etc. And we

[Python-ideas] Re: Add Subscriptable ABC

2019-09-29 Thread Steven D'Aprano
On Sun, Sep 29, 2019 at 11:27:32AM +0100, Oscar Benjamin wrote: > That's the point that I would make as well. What can you do with an > object that is only known to be Subscriptable? I can subscript it. What did you expect the answer to be? If you want to know if an object has a length, you can

[Python-ideas] Re: Add Subscriptable ABC

2019-09-29 Thread Christopher Barker
On Sun, Sep 29, 2019 at 5:04 AM Ricky Teachey wrote: > That's the point that I would make as well. What can you do with an >> > object that is only known to be Subscriptable? >> >> def do_subscriptable_things(obj): >> if isinstance(obj, Subscriptable): >> # Now what? > > I'm going to

[Python-ideas] Re: Add Subscriptable ABC

2019-09-29 Thread Ricky Teachey
> > That's the point that I would make as well. What can you do with an > object that is only known to be Subscriptable? > > def do_subscriptable_things(obj): > if isinstance(obj, Subscriptable): > # Now what? > Maybe if you want to use/abuse it as an alternative function calling

[Python-ideas] Re: Add Subscriptable ABC

2019-09-29 Thread Oscar Benjamin
On Sun, 29 Sep 2019 at 08:28, Kyle Stanley wrote: > > Raymond also brought up a strong point to consider in > https://bugs.python.org/issue25988: > > > The OP has a sense that Mapping and Sequence are "too heavy" > but I think the reality that useful classes almost never use > __getitem__ in

[Python-ideas] Re: Add Subscriptable ABC

2019-09-29 Thread Kyle Stanley
> This supports my point that this ought to be handled once, correctly, in the standard library, like the other ABCs, instead of leaving it up to people like me to get it wrong. It would be pretty reasonable to assume that most users would get this incorrectly. My first assumption to this problem

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2019 at 10:11:15AM -0700, Andrew Barnert wrote: > On Sep 27, 2019, at 09:05, Steven D'Aprano wrote: > > > > What > > I'd really like to do is use the collections.abc module to do the check: > > > >if isinstance(obj, collections.abc.Subscriptable): ... > > What about

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Steven D'Aprano
On Fri, Sep 27, 2019 at 12:03:52PM -0700, Andrew Barnert via Python-ideas wrote: > On Sep 27, 2019, at 10:15, MRAB wrote: > > > >> On 2019-09-27 17:05, Steven D'Aprano wrote: > >> > >> But doing it correctly is too painful: > >> if any(getattr(T, '__getitem__', None) is not None for T in >

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Andrew Barnert via Python-ideas
On Sep 27, 2019, at 10:15, MRAB wrote: > >> On 2019-09-27 17:05, Steven D'Aprano wrote: >> >> But doing it correctly is too painful: >> if any(getattr(T, '__getitem__', None) is not None for T in >> type(obj).mro()) > [snip] > > Is there are reason why you're using 'getattr' instead of

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Andrew Barnert via Python-ideas
On Sep 27, 2019, at 09:05, Steven D'Aprano wrote: > > What > I'd really like to do is use the collections.abc module to do the check: > >if isinstance(obj, collections.abc.Subscriptable): ... What about isinstance(obj, (Sequence, Mapping))? That isn’t quite the same thing, since you can

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Devin Jeanpierre
On Fri, Sep 27, 2019 at 9:14 AM Steven D'Aprano wrote: > But doing it correctly is too painful: > > if any(getattr(T, '__getitem__', None) is not None for T in > type(obj).mro()) > For what it's worth, walking the MRO isn't necessary, and the None trick is only necessary if you want to

[Python-ideas] Re: Add Subscriptable ABC

2019-09-27 Thread Ivan Levkivskyi
Just FYI there is a closely related issue on b.p.o. https://bugs.python.org/issue25988. FWIW I am in favor of the idea, but some people are against it (see e.g. the issue). -- Ivan On Fri, 27 Sep 2019 at 17:12, Steven D'Aprano wrote: > I keep finding myself needing to test for objects that