Douglas Raillard <douglas.raill...@arm.com> added the comment:

> Would bpo-47143 "Add functools.copy_class() which updates closures" solve 
> your use case?

This looks like a similar issue indeed. If I'm able to copy a class "cleanly" 
(as defined in this other thread), that may solve the problem (hard to tell 
precisely until trying it though, the devil is in the details).

> Would you elaborate your use case?

As you probably expect, it's a relatively tricky metaprogramming bit of code. 
It's implementing something akin to higher kinded types, similar in spirit to 
the following example where you can have e.g. containers that "know" the item 
type they contain:

        class ContainerBase:
                ITEM = None

                @classmethod
                def apply(cls, typ):
                        ...


                @classmethod
                def empty_list(cls):
                        ...
                  
        class List(ContainerBase):
                pass
        
        class ListOfInt(List.apply(int)):
                pass

The goal is to allow "ListOfInt" to define custom methods if we want in its 
body.

"ListOfInt" cannot return instances of itself [1] so I'm trying to "cherry 
pick" its method and graft them onto the type that is actually instantiated.

[1] We e.g. want to create an empty list of the right type. "List" already 
provides the method for such factory, but it will not return an actual instance 
of ListOfInt, since it does not even know this class exists. What it will use 
is an internal type that it created using the "ITEM" class attribute in e.g. 
List.__init_subclass__, and that is what will get instantiated. This "internal 
class" could still be customized by ListOfInt to have some custom methods 
though, which is what I'm trying to achieve.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue47144>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to