
On 8/11/21 5:15 AM, Chris Angelico wrote:
On Wed, Aug 11, 2021 at 10:03 PM Larry Hastings <larry@hastings.org> wrote:
This approach shouldn't break reasonable existing code. That said, this change would be observable from Python, and pathological code could notice and break. For example:
def ensure_Foo_is_a_class(o): assert isinstance(Foo, type) return o
class Foo: ...
@ensure_Foo_is_a_class def Foo(): ...
This terrible code currently would not raise an assertion. But if we made the proposed change to the implementation of decorators, it would. I doubt anybody does this sort of nonsense, I just wanted to fully flesh out the topic.
You would be here declaring that a @monkeypatch decorator is terrible code. I'm not sure whether you're right or wrong. You may very well be right.
def monkeypatch(cls): basis = globals()[cls.__name__] for attr in dir(cls): setattr(basis, attr, getattr(cls, attr)) return basis
@monkeypatch class SomeClass: def new_method(self): ...
Currently this works, since SomeClass doesn't get assigned yet. This could be made to work across versions by writing it as @monkeypatch(SomeClass) instead (and then the actual class name would become immaterial).
Golly! I've never seen that. Is that a common technique? If we need to preserve that behavior, then this idea is probably a non-starter. //arry/