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

feat(python): show properties and values in TypeBuilder string representation #1260

Open
wants to merge 6 commits into
base: canary
Choose a base branch
from

Conversation

revidious
Copy link
Contributor

@revidious revidious commented Dec 18, 2024

This PR enhances the TypeBuilder's string representation to show all properties of a class and values of an enum, including their aliases and descriptions.

Changes:

  • Show all properties in class representation:

    # Before:
    TypeBuilder(Classes: ['User'])
    
    # After:
    TypeBuilder(Classes: ['User { name (alias='username', desc='The user\'s full name'), age (desc='User\'s age in years'), email }'])
  • Show all values in enum representation:

    # Before:
    TypeBuilder(Enums: ['Status'])
    
    # After:
    TypeBuilder(Enums: ['Status { ACTIVE (alias='active', desc='User is active'), INACTIVE (alias='inactive'), PENDING }'])
  • Store and track metadata:

    • Cache aliases and descriptions in builders
    • Sort properties and values for consistent output
    • Properly escape quotes in string values

Fixes #1253

Copy link

vercel bot commented Dec 18, 2024

@revidious is attempting to deploy a commit to the Gloo Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Reviewed everything up to 7311baa in 46 seconds

More details
  • Looked at 362 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 7 drafted comments based on config settings.
1. engine/language_client_python/python_src/baml_py/type_builder.py:27
  • Draft comment:
    The docstring should start with a capital letter for consistency and readability.
    """A builder for creating and modifying types at runtime.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the TypeBuilder class is inconsistent in its use of capitalization. It starts with a lowercase letter, which is not standard for Python docstrings. This should be corrected for consistency and readability.
2. engine/language_client_python/python_src/baml_py/type_builder.py:46
  • Draft comment:
    Remove the extra space between 'the' and 'TypeBuilder'.
        """Initialize the TypeBuilder with optional predefined classes and enums.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the __init__ method in the TypeBuilder class has an extra space between 'the' and 'TypeBuilder'. This is a minor formatting issue but should be corrected for consistency.
3. engine/language_client_python/python_src/baml_py/type_builder.py:64
  • Draft comment:
    The docstring should start with a capital letter and maintain consistent formatting.
        """A human-readable string representation of the TypeBuilder.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the __str__ method in the TypeBuilder class has inconsistent capitalization and formatting. The first letter should be capitalized, and the formatting should be consistent with other docstrings.
4. engine/language_client_python/python_src/baml_py/type_builder.py:206
  • Draft comment:
    The docstring should start with a capital letter for consistency.
    """Builder for creating and modifying BAML classes.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the NewClassBuilder class should start with a capital letter for consistency with Python docstring conventions.
5. engine/language_client_python/python_src/baml_py/type_builder.py:248
  • Draft comment:
    The docstring should start with a capital letter for consistency.
    """Builder for configuring class properties.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the ClassPropertyBuilder class should start with a capital letter for consistency with Python docstring conventions.
6. engine/language_client_python/python_src/baml_py/type_builder.py:296
  • Draft comment:
    The docstring should start with a capital letter for consistency.
    """Builder for creating and modifying BAML enums.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the NewEnumBuilder class should start with a capital letter for consistency with Python docstring conventions.
7. engine/language_client_python/python_src/baml_py/type_builder.py:339
  • Draft comment:
    The docstring should start with a capital letter for consistency.
    """Builder for configuring enum values.
  • Reason this comment was not posted:
    Confidence changes required: 10%
    The docstring for the EnumValueBuilderWrapper class should start with a capital letter for consistency with Python docstring conventions.

Workflow ID: wflow_RO6UccrIdJ6lPCDD


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@aaronvg
Copy link
Contributor

aaronvg commented Dec 18, 2024

nice work! We'll have someone in the team review soon

Copy link
Contributor

@hellovai hellovai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@revidious this is really great work, but if possible, would you be ok implenting this in rust instead?

otherwise we'll have different printers in python/ts/ruby/etc and will make it harder to catch bugs later on (and small details will easily be missed)

@revidious revidious closed this Dec 24, 2024
@revidious revidious force-pushed the feature/typebuilder-repr branch from 7311baa to 2974458 Compare December 24, 2024 07:31
…mplementation for TypeBuilder and related structs - Add Python bindings for string representation - Add comprehensive test cases for both Rust and Python - Add detailed documentation for string formatting
@revidious revidious reopened this Dec 24, 2024
@revidious
Copy link
Contributor Author

@hellovai Done! I've moved the implementation to Rust with the Display trait, and Python now just exposes it via str.

@revidious revidious requested a review from hellovai December 24, 2024 07:40
Copy link
Contributor

@hellovai hellovai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great! :) ready to merge. small request, could you add this for the language_client_{ts / ruby} as well. If not, let us know and i'll follow up on it instead. This is almost perfect btw 👍

Some nits things I would do (but not mandatory):

  • add whitespace / newlines in the rendered string
  • instead of BamlValue::to_string(...) I would just implement the display for all baml values
  • Instead of hard-coding which attributes i print out for meta, i would inject all metas in the print out, not just alias / description

@revidious
Copy link
Contributor Author

Thanks for the feedback! I've implemented all three suggestions:

  • Added proper whitespace/newlines in the rendered string
  • Using BamlValue's Display trait directly instead of to_string()
  • Now showing all metadata fields without specific ones

Changes are implemented in both TypeScript and Ruby clients as well (what it seems like).

@revidious revidious requested a review from hellovai December 25, 2024 10:56
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 this pull request may close these issues.

TypeBuilder _repr_ should be implemented in python for better debugging
3 participants