Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"You'll pay to know what you really think." -- J. R. "Bob" Dobbs


devel / comp.lang.python / Odd types.get_original_bases() behavior for classes with generic bases but no type arguments

SubjectAuthor
o Odd types.get_original_bases() behavior for classes with genericChris Bouchard

1
Odd types.get_original_bases() behavior for classes with generic bases but no type arguments

<mailman.192.1690254342.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23823&group=comp.lang.python#23823

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: chr...@upliftinglemma.net (Chris Bouchard)
Newsgroups: comp.lang.python
Subject: Odd types.get_original_bases() behavior for classes with generic
bases but no type arguments
Date: Mon, 24 Jul 2023 22:59:17 -0400
Lines: 85
Message-ID: <mailman.192.1690254342.23016.python-list@python.org>
References: <CAFmgF4VuXLP3NAyShRcODftcayiysO3A3Dq=yy-ufLEcFk-cuw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de BN8yXw7Ij+2UyAqidmrwrwbHZSuaHQBhv/z4ovfmcyZQ==
Cancel-Lock: sha1:1fRv0LAv4ZA1STZ8UktwHiLNGBw= sha256:uj3o8H72Xo7zSERYN6ja8xgsakhORFyxK1WH6i+7UoA=
Return-Path: <javajunkie314@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: UNSURE 0.216
X-Spam-Level: **
X-Spam-Evidence: '*H*': 0.57; '*S*': 0.00; '(to': 0.07; 'subject:but':
0.09; 'types.': 0.09; '(unless': 0.16; 'bug,': 0.16; 'inspecting':
0.16; 'odd': 0.16; 'received:209.85.222.180': 0.16; 'received
:mail-qk1-f180.google.com': 0.16; 'subject:() ': 0.16; 'tries':
0.16; 'unfortunate': 0.16; 'bug': 0.19; 'to:addr:python-list':
0.20; 'first,': 0.22; 'skip:_ 10': 0.22; 'anything': 0.25;
'tried': 0.26; 'behavior': 0.26; 'function': 0.27; '>>>': 0.28;
'chris': 0.28; 'expect': 0.28; 'example,': 0.28; 'think': 0.32;
"doesn't": 0.32; 'evening': 0.32; 'personally': 0.32; 'returning':
0.32; 'said,': 0.32; 'words,': 0.32; 'message-id:@mail.gmail.com':
0.32; 'but': 0.32; "i'm": 0.33; 'subject:for': 0.33; 'same': 0.34;
"didn't": 0.34; 'received:google.com': 0.34; 'addressed': 0.36;
'thanks,': 0.36; 'missing': 0.37; 'currently': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'class': 0.37; 'way': 0.38; 'could':
0.38; 'received:209': 0.39; 'received:209.85.222': 0.39; 'base':
0.40; 'difficult': 0.40; 'both': 0.40; 'something': 0.40;
'search': 0.61; 'today': 0.61; 'pass': 0.64; 'clear': 0.64;
'down': 0.64; 'hesitate': 0.65; 'skip:t 20': 0.66; 'types': 0.67;
'back': 0.67; 'skip:t 30': 0.67; 'that,': 0.67; 'from:addr:chris':
0.69; 'playing': 0.69; 'types,': 0.69; 'skip:f 20': 0.75; 'finds':
0.76; '....': 0.76; 'happens': 0.84; '(first,': 0.84; 'hacks':
0.84; 'inheritance': 0.84; 'subject: \n ': 0.84;
'subject:behavior': 0.84; 'subject:type': 0.91; 'fall': 0.95
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1690253969; x=1690858769;
h=content-transfer-encoding:to:subject:message-id:date:from
:mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
:reply-to;
bh=kMQjtKWhj0b1YroruuZ4YyCJQGrpYaRbXmjiEOVaf20=;
b=A+q9a+yMHdHfTtqhQL+xk54EQLpqFdLWogd6VJJMbI440H0sX/G3SzQJTemCqvBpEf
acdIXG0IcC1FrW3gy35sSlMs9PeqWDsdvSecoZCdKUfernUUnLBbOQD/IqIeypz4nEPe
g0XnT9A4qGW6teKIW7pNNHWnmuDMRyJRi2n/dYW/RTUpGO/9sw5AO2SfohajuQ37RJpM
/jPgbxOmuumCLqhj4JVXm3wFhAL0Cjr7EPNWBhj9IGu7T40l2GC8v41Mc5PUoHGtrqHX
S/GATkxfdNiGG1PeBK1Jn2qq1XWrlOUYDxlSyMlnGag9CWi2HZ2mCYTwOOG8zg7rUmWb
4dBQ==
X-Gm-Message-State: ABy/qLYyYv6TyMVT+hor46Ue4op6vu1zPm6jJF6zjqECPee6NHDBEsbh
Xkm5UJxqUtZJD+qIR/l3C479mOk2lAeoVQ==
X-Google-Smtp-Source: APBJJlGochnLaIel9s6MCPFDSnWprM/ceHbs9+HmT1ByofLYnFkI5DYkYdhVHtLPFL2cdOgjC3HkAg==
X-Received: by 2002:a05:620a:8ec3:b0:765:36a5:9207 with SMTP id
rg3-20020a05620a8ec300b0076536a59207mr1203525qkn.0.1690253969521;
Mon, 24 Jul 2023 19:59:29 -0700 (PDT)
X-Received: by 2002:a05:620a:215b:b0:76a:dd4f:a61 with SMTP id
m27-20020a05620a215b00b0076add4f0a61mr1583187qkm.22.1690253968607; Mon, 24
Jul 2023 19:59:28 -0700 (PDT)
X-Gmail-Original-Message-ID: <CAFmgF4VuXLP3NAyShRcODftcayiysO3A3Dq=yy-ufLEcFk-cuw@mail.gmail.com>
X-Mailman-Approved-At: Mon, 24 Jul 2023 23:05:41 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAFmgF4VuXLP3NAyShRcODftcayiysO3A3Dq=yy-ufLEcFk-cuw@mail.gmail.com>
 by: Chris Bouchard - Tue, 25 Jul 2023 02:59 UTC

(Apologies if this has already been discussed. I tried to search and didn't find
anything relevant.)

I was playing around with 3.12.0b4 this evening and noticed an odd (to me, at
least) behavior with types.get_original_bases(). I hesitate to call it a bug
because I think I understand *why* it's behaving this way—I'm just not sure it's
desirable.

>>> T = typing.TypeVar("T")
>>> class FirstBase(typing.Generic[T]):
.... pass
....
>>> class SecondBase(typing.Generic[T]):
.... pass
....
>>> class First(FirstBase[int]):
.... pass
....
>>> class Second(SecondBase[int]):
.... pass
....
>>> class Example(First, Second):
.... pass
....
>>> types.get_original_bases(Example)
(FirstBase[int],)
>>> Example.__bases__
(First, Second)
>>> types.get_original_bases(First)
(FirstBase[int],)

In other words, types.get_original_bases(Example) is returning the original base
types for First, rather than its own.

I believe this happens because __orig_bases__ is only set when one or more bases
are not types. In this case both bases are types, so Example doesn't get its own
__orig_bases__. Then when types.get_original_bases() tries to get __orig_bases__
with getattr(cls, "__orig_bases__") or something morally equivalent, it searches
the MRO and finds __orig_bases__ on First.

The same thing also happens if all the bases are "bare" generic types.

>>> class First(typing.Generic[T]):
.... pass
....
>>> class Second(typing.Generic[T]):
.... pass
....
>>> class Example(First, Second):
.... pass
....
>>> types.get_original_bases(Example)
(typing.Generic[~T],)

As I said, I'm not clear if this is a bug, or just an unfortunate but intended
behavior. I would personally expect types.get_original_bases() to check if the
type has its *own* __orig_bases__ attribute, and to fall back to __bases__
otherwise. The way it works today makes it unnecessarily difficult to write a
function that, for example, recurses down a type's inheritance tree inspecting
the original bases—I currently have to work around this behavior via hacks like
checking "__orig_bases__" in cls.__dict__ or any(types.get_original_bases(cls)
== types.get_original_bases(base) for base in cls.__bases__). (Unless I'm
missing some simpler solution.)

Is this something that could (should?) be addressed before 3.12 lands?

Thanks,
Chris Bouchard

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor