Skip to content

Use descriptors for Extensions? #367

@TomAugspurger

Description

@TomAugspurger

I'm writing up an extension, and my wrists hurt from all the typing :) I wonder if we could use descriptors to cut down on the boilerplate. For a typical property that comes from the properties dict, it'd be something like

class MyExtension:
    description = Property()

where Property is a descriptor like

# untested
class Property:
    def __set_name__(self, owner, name):
        self.name= name

    def __get__(self, obj, objtype=None):
        return getattr(obj, "properties")[self.name)
    def __set__(self, obj, value):
        getattr(obj, "properties")[self.name] = value

I haven't really looked to see if this works for many of the extensions. Some downsides:

  1. Unclear how this would interact with static typing. But properties are supported by mypy, and descriptors are prosperities, so it might be doable.
  2. Makes . lookup a bit slower, though on par with @property so maybe it's acceptable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions