[Python-3000] Type parameterization (was: Re: Type annotations: annotating generators)
Jim Jewett
jimjjewett at gmail.com
Fri May 19 22:46:31 CEST 2006
On 5/19/06, Collin Winter <collinw at gmail.com> wrote:
> On 5/19/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> > On 5/19/06, Collin Winter <collinw at gmail.com> wrote:
> > > ... what do you think of ... having bracket-based parameterization be
> > > redirected a call to some double-underscore method
> > It already is -- to __getitem__.
> >
> > Having it go to something else just because you're in a function
> > definition is asking for trouble. What should happen in the following
> > case?
> > b=dict(strict=check1, lenient=check2, normal=check3)
> > def foo(a:b["normal"]): pass
> > Should it really look for some special method on b (or b's type) just
> > because it is in a signature context? Today, the annotation
> > expression would evaluate to check2, and I'm not looking forward to
> > figuring out all the corner cases on when that wouldn't happen.
> dict(normal=check3)["normal"] is not the same as dict["normal"] --
Yes, they are. They both call __getitem__.
__getitem__ on a dictionary instance happens to be defined.
__getitem__ on the dict class happens not to be defined.
> Guido and I have been discussing the latter. When I said "redirected",
> I meant that dict's metaclass would catch the __getitem__ call and
> then invoke dict.__parameterize__() appropriately.
I interpreted your suggestion as saying type annotations should be
treated that way.
Now it sounds like you are saying that a few specific classes (such as
dict) should be given a custom metaclass, and start to behave this way
in all situations, not just signature context. User defined classes
would need to explicitly request the same behavior.
I still think it has too many corner cases, both mentally and from an
implementation standpoint.
class P:
def __parameterize__(self, T):
Is P()[x] the same as P().__parameterize__(x)? Then why not just call
it __getitem__?
class B:
def __getitem__(self, k): return 42
def __parameterize__(self, T): pass
Since __getitem__ exists and does not delegate, is there any way that
B.__parameterize__ will ever be called just by using brackets? If so,
then when? If not, what good is it?
-jJ
More information about the Python-3000
mailing list