blhsing
(Ben Hsing)
March 15, 2024, 4:09pm
16
With all due respect, you appear to have a tinted filter that is hard-wired to ignore all my recent responses to feedbacks from other users, all positive ones:
object was actually the first thing I tried too, but it would produce:
TypeError: duplicate base class object
And if I did:
SupportsRead = SupportsWrite = object()
I’d get:
TypeError: object() takes no arguments
So I figured Mock() was the most convenient object because it can be called with any arguments, although one can also do:
class M:
def __new__(*args):
return object.__new__(M)
try:
from _typeshed import SupportsRead, SupportsWrite
except ModuleNotFoundError:
…
Yes absolutely. More reason to free developers from fear of using metaclasses.
By using the metaclass wrapper I posted in the Help forum, it is able to pass your test case since it calls __init_subclass__ to register the class object only after the dataclass decorator returns the new class.
As for passing on kwargs it’s certainly a problem as well, and I have to work around it by storing it in a _dataclass_kwargs attribute to pass it on:
_dataclass_keywords = set((code := dataclass.__code__).…
Yes, that’s what my second thought was alluding to, that a late evaluation of a string-based expression is useful only as a late-bound argument, since the names of the parameters are known to the caller.
Thanks. Should’ve thought of that instead of redoing what MRO already does. It does solve the problem elegantly.
class Base:
def __init_subclass__(cls, **kwargs):
kwargs.pop('debug', None)
super().__init_subclass__(**kwargs)
class Foo(Base):
def __init_subclass__(cls, **kwargs):
if kwargs.get('debug'):
print(f'initialized from {Foo.__init_subclass__}')
super().__init_subclass__(**kwargs)
class Bar(Base):
def __init_subclass__(cls, **kwa…
Yeah, I’m rooting for @ruro ’s idea also because it looks the most likely to be accepted despite it being a solution to only a specific (but again, the biggest) use case of multiple metaclasses.
Would be nice if my other, more general solution gets accepted but the sheer simplicity and elegance of @ruro ’s idea makes it more likely to succeed.
Why not judge a proposal by its merit instead?