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

Operator based BaseMetadata creation #28

Closed
hanjinliu opened this issue Nov 13, 2022 · 4 comments
Closed

Operator based BaseMetadata creation #28

hanjinliu opened this issue Nov 13, 2022 · 4 comments

Comments

@hanjinliu
Copy link

Hi all,
I really like the idea of BaseMetadata to formulate Annotated types.

However, configuration using functions such as Gt(10) is not as easy as x > 10 to read. It would be nice to provide a non-constraint class All and support methods such as __gt__ like below.

class All(BaseMetadata):
    def __gt__(self, other):
        return Gt(other)

X = All()  # could be better to keep `All` private and provide `X` publically.
Annotated[int, X > 5]  # equivalent to Annotated[int, Gt(5)]

Similar logic can by applied to construct several objects.

  • Interval from 2 < X < 5
  • MultipleOf from X % 3 == 0
  • MaxLen from X.len() <= 10
@Zac-HD
Copy link
Member

Zac-HD commented Nov 14, 2022

Ooh, I really like this idea! It's going to be a little tricky to implement support for chained comparisons and len(X), at least if we want to give good error messages when misused, but worth it for how sweet the syntax will be 🤩

@samuelcolvin
Copy link
Contributor

Awesome idea, thanks @hanjinliu. I'll review the PR now.

@hanjinliu
Copy link
Author

Thank you all for the consideration! Looking forward to seeing it.

@Zac-HD
Copy link
Member

Zac-HD commented Nov 15, 2022

Per #30, this sadly seems too complex and edge-case-prone to support for now.

Nonetheless, we really appreciate your contribution of design feedback 🙏- thanks!

@Zac-HD Zac-HD closed this as completed Nov 15, 2022
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

Successfully merging a pull request may close this issue.

3 participants