Skip to content

Change defaults of an 3rd party attrs class #829

Closed
@tc8

Description

@tc8

I'm using the grafanalib library which uses attrs extensively and one of the class looks something like this

@attr.s
class Target(object):
    intervalFactor = attr.ib(default=2)
...

I want to change the default of intervalFactor to 1. I could specify 1 each time in the constructor but that's very tedious when I have to do it each time.

I tried subclassing it and playing with __attrs_pre_init__ , __attrs_post_init__ , and __attrs_init__ but could not find any combination that gave me the desired behavior.

I've also looked at using attr.evolve like attrs.evolve(default_target, myarg1, myarg2, ...) where default_target is a Target instance with the default values set. This works but witing attrs.evolve(default_target each time in lieu of the constructor is tedious.

I could wrap it in a helper function, like

def create_target(*args, **kwargs) -> Target: 
  return attrs.evolve(default_target, *args, **kwargs)

The problem with this is that I lose the IDE (PyCharm) autocompletion that I would get with writing attrs.evolve(...) manually.

I tried setting the __signature__ manually by doing create_target.__signature = inspect.signature(Target.__init__) but this didn't give me the correct tooltip in the editor model.

Any help would be greatly appreciated.

** On a related note**
How is attrs.evolve able to provide the right auto-complete suggestions in PyCharm? Is it because of the built-in integration with PyCharm?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions