Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define validators/converters for attrs classes #554

Open
mikewilli opened this issue Aug 8, 2024 · 0 comments
Open

define validators/converters for attrs classes #554

mikewilli opened this issue Aug 8, 2024 · 0 comments

Comments

@mikewilli
Copy link
Collaborator

mikewilli commented Aug 8, 2024

See the thread at the link below, but here's the main comment:

+1 to "let's do it later, not necessary for this PR"

As I understand it, default defines the default argument value while converter applies a function to the argument. Converters allow you to differentiate between the types of the parameters and the types of the attributes

So

class MyClass:
 x = attr.field(default = None, type = str | None)

will define a class with a constructor like:

MyClass.__init__(self, x: str | None = None):
 self.x = x # type will be str | None

If you add a converter:

class MyClass: 
 x = attr.field(default = None, type = str, converter=attr.converter.default_if_none("default"))

you'll get a class with a constructor like:

MyClass.__init__(self, x: str | None = None):
  self.x = x if x is not None else "default" # type will be str, no longer optional

I think it comes down to a question of 1) what parameters do we want callers to be able to pass to the constructor and 2) what type do we want the class attributes to be.

Originally posted by @danielkberry in #553 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant