python-attrs / python-attrs/attrs

Conditional initialization of attributes

Open
#354 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Description

While trying to use attrs with sqlalchemy, I came across an issue that in some cases it's needed to initialize an attribute only if it was passed to constructor.

Basically, this code

@attr.attrs
class A:
   a = attr.ib(initialize_if_given=True)

should translate to this

CONDITIONAL_INIT = object()

class A:
    def __init__(a=CONDITIONAL_INIT):
        if a is not CONDITIONAL_INIT:
            self.a = a

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named in the issue. Start from the two constructor examples and review the project's existing initialization behavior; done means supporting conditional attribute assignment when the argument is provided while preserving current constructor behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.