nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.52k stars 130 forks source link

Add initial enamldef const storage #546

Closed frmdstryr closed 3 months ago

frmdstryr commented 3 months ago

This makes the parser allow a "const" in an enamldef or child def that uses atom's ReadOnly member so it cannot be changed after the initial value is set. The only other difference from an attr is that the parser requires a default expression.

The existing parser (pegen not the ply one) actually allows "const" in an enamldef but not in a child def but throws an error because the visitor is not implemented in the block compiler.

I copied a lot of code in the gen_const_expr, idk if that can/should be cleaned up. It's mainly an inlined gen_storage_expr and gen_operator_expr.

An example use case...

from enaml.widgets.api import Window, Container, Label, PushButton

enamldef Main(Window): window:
    const locked: bool = True
    Container:
        Label:
            text = "Locked" if locked else "Unlocked"
        PushButton:
            text = "Try to unlock"
            clicked ::
                try:
                    window.locked = False
                except TypeError:
                    print("Nope!")

Any feedback is welcome.

codecov[bot] commented 3 months ago

Codecov Report

Merging #546 (a537128) into main (bf0ea6f) will increase coverage by 0.05%. The diff coverage is 97.50%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #546 +/- ## ========================================== + Coverage 70.74% 70.79% +0.05% ========================================== Files 287 287 Lines 25634 25673 +39 Branches 4676 4683 +7 ========================================== + Hits 18135 18176 +41 + Misses 6386 6385 -1 + Partials 1113 1112 -1 ```
sccolbert commented 3 months ago

Does this really gain us anything in practice?

I am generally against adding to the syntax unless it introduces an entirely new concept to the language.

On Sat, Mar 23, 2024 at 8:56 AM codecov[bot] @.***> wrote:

Codecov https://app.codecov.io/gh/nucleic/enaml/pull/546?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nucleic Report

Merging #546 https://app.codecov.io/gh/nucleic/enaml/pull/546?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nucleic (b56ea86 https://github.com/nucleic/enaml/commit/b56ea869525f704d96c0bc3426c64d66ea4056e5) into main https://app.codecov.io/gh/nucleic/enaml/commit/bf0ea6fde7b2c2ed72121fb3af8d0def6690449f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nucleic (bf0ea6f https://github.com/nucleic/enaml/commit/bf0ea6fde7b2c2ed72121fb3af8d0def6690449f) will increase coverage by 0.05%. The diff coverage is 97.50%.

Additional details and impacted files

@@ Coverage Diff @@## main #546 +/- ## ==========================================+ Coverage 70.74% 70.79% +0.05%

Files 287 287 Lines 25634 25673 +39 Branches 4676 4683 +7 ==========================================+ Hits 18135 18176 +41 + Misses 6386 6385 -1 + Partials 1113 1112 -1

— Reply to this email directly, view it on GitHub https://github.com/nucleic/enaml/pull/546#issuecomment-2016502179, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABBQSM52IN54EELXEKSYTLYZWCXRAVCNFSM6AAAAABFEVMADWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMJWGUYDEMJXHE . You are receiving this because you are subscribed to this thread.Message ID: @.***>