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

Unify vdom interface #841

Merged
merged 24 commits into from
Jan 28, 2023
Merged

Unify vdom interface #841

merged 24 commits into from
Jan 28, 2023

Conversation

rmorshea
Copy link
Collaborator

closes: #755

Checklist

Please update this checklist as you complete each item:

  • Tests have been included for all bug fixes or added functionality.
  • The changelog.rst has been updated with any significant changes.
  • GitHub Issues which may be closed by this Pull Request have been linked.

@rmorshea rmorshea force-pushed the unify-vdom-interface branch from bb1038c to 5cfed3e Compare November 29, 2022 06:12
@Archmonger
Copy link
Contributor

This should probably be the final PR in the 1.0 release, since it's a fairly significant overhaul.

@Archmonger
Copy link
Contributor

Archmonger commented Jan 3, 2023

I did some more thinking, id would be the the single property that makes this implementation messy. We don't want to override the built-in python id function, so do we sufix it with a _?

@Archmonger
Copy link
Contributor

Discussed this via call with Ryan.

All suffixed underscores should be stripped out.

@Archmonger
Copy link
Contributor

Archmonger commented Jan 21, 2023

We might want to have another discussion as to whether we should move forward with this.

Given both options have positives/negatives, I'm still undecided.


Using Dict

Pros

  • Feels more like ReactJS props (since in React/JSX props are "at the top" within each HTML tag)
  • Does not have an issue with Python keywords`

Cons

  • Might feel weird to some
  • Currently requires using camelCase within the dict, which is not pythonic.

Using Kwargs

Pros

  • Feels more pythonic
  • Can do long-form type hints for all available properties

Cons

  • Does not have good visual similarity to ReactJS
  • Switching to this will require developer migration efforts
  • Will feel weird to use for data-* and aria-* attributes
  • Makes writing conditional parameters more awkward, since they'll need to be an unpacked dict.

@rmorshea
Copy link
Collaborator Author

rmorshea commented Jan 22, 2023

To clarify, only the following are keywords that would require you to add a trailing _:

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

Functions like id, map, or type which are available in the global namespace, but are not keywords do not conflict with kwargs passed to VDOM constructors.

Personally I don't think this is a large enough issue to block these changes.

@Archmonger
Copy link
Contributor

Do we want the conversion script to be able to handle props in a dict variable?

MODAL_CONTAINER = {
    "id": "modal-container",
    "className": "modal fade",
    "tabIndex": "-1",
    "aria-hidden": "true",
}

@component
def modal():
    return div(
        MODAL_CONTAINER,
        ....
    )

@rmorshea
Copy link
Collaborator Author

rmorshea commented Jan 23, 2023

Unfortunately, handling that is complicated. Looking at the div call in isolation, there's no way to tell if it's a child element or an attribute dictionary. You'd have to work backwards and determine the type of the variable. It's probably possible, but a lot more challenging than just converting dict literals to kwargs.

@rmorshea rmorshea force-pushed the unify-vdom-interface branch 4 times, most recently from 3fc050f to e8231ff Compare January 25, 2023 06:35
@rmorshea
Copy link
Collaborator Author

rmorshea commented Jan 25, 2023

@Archmonger can you try running the script against some of your code? It fairs pretty well against IDOM's source but that's a fairly small sample size. You should be able to copy and paste in an environment where IDOM is installed and do the following:

python fix_vdom_constructor_usage.py [patterns]

Where [patterns] is any number of glob pattern for the files you want to modify. For example:

python fix_vdom_constructor_usage.py tests/**/*.py tests/*.py

@Archmonger
Copy link
Contributor

Archmonger commented Jan 25, 2023

Looks like there was a incomplete merge commit at this LOC

EDIT: Perhaps not, maybe that's an actual docstring? The indentation makes it look fairly comical though.

Probably better represented as this:

dedent(
   f"{actual}\n"
   "▲   actual   ▲\n"
   "▼  expected  ▼\n"
   f"{expected}\n"
)

@Archmonger
Copy link
Contributor

Archmonger commented Jan 25, 2023

Running the output on conreq@app_store then did a visual inspection of all the deltas.

It only messed in in one situation, where it forgot to leave a preceding space on an inline ... if .... else ... statement on this LOC.

It essentially resulted in x if y elsehtml.button(...)


I think this interface looks way more pythonic, so I dig it.

@rmorshea rmorshea force-pushed the unify-vdom-interface branch 2 times, most recently from b037870 to 34d4a87 Compare January 26, 2023 02:39
@rmorshea
Copy link
Collaborator Author

Outside of the updating narrative docs this change is pretty much complete. Usages of vdom() where a child is a dictionary without a tagName key produce a warning explaining the API change. IDOM will also distribute a CLI utility for upgrading to the new API (e.x. idom update-html-usages [PATTERNS]).

@Archmonger
Copy link
Contributor

We probably want that CLI to be a bit more user friendly.

idom update-html-usages [DIR_OR_FILE]

If we inspect the path to be a dir, then we should automatically create the glob **/*.py

Otherwise, only format one file.

@rmorshea rmorshea force-pushed the unify-vdom-interface branch from 34d4a87 to 79bd0ff Compare January 26, 2023 03:09
@rmorshea
Copy link
Collaborator Author

Yup. Just made that change.

@rmorshea rmorshea marked this pull request as ready for review January 26, 2023 07:50
@Archmonger
Copy link
Contributor

Will review when tests pass

@rmorshea rmorshea force-pushed the unify-vdom-interface branch 3 times, most recently from 0e528a0 to 3693009 Compare January 26, 2023 19:20
@rmorshea
Copy link
Collaborator Author

There's some minor kinks in the tests to work out, but this is ready for review.

Copy link
Contributor

@Archmonger Archmonger left a comment

Choose a reason for hiding this comment

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

Not many nitpicks on this one.

Can we potentially sneak in a revert to the use_state type hints on this PR? It's fairly painful to to use_state right now.

docs/examples.py Outdated Show resolved Hide resolved
src/idom/utils.py Show resolved Hide resolved
@Archmonger
Copy link
Contributor

When we cut a 1.0.0 release of IDOM, I will sneak it into the 3.0.0 release of Django IDOM

@Archmonger
Copy link
Contributor

On a different note, in the future releases of IDOM, I recommend we switch to semantic versioning rather than feature-set versioning.

We will need to rename our GitHub Projects milestones as such.

@rmorshea rmorshea merged commit 764a5a8 into main Jan 28, 2023
@rmorshea rmorshea deleted the unify-vdom-interface branch January 28, 2023 01:17
@rmorshea
Copy link
Collaborator Author

Completely forgot to update docs/changelog will create a followup PR

@rmorshea rmorshea mentioned this pull request Feb 10, 2023
3 tasks
rmorshea added a commit that referenced this pull request Feb 21, 2023
* revert 764a5a8

- but preserve ability to declare attributes with snake_case
- move 'key' to be an attribute rather than keyword argument
  in constructor + make auto converter for this change based
  on the one used for the "new" `*args` and `**kwargs`
  syntax we are reverting.

* add key rewrite script

* changelog

* fix types

* apply rewrites

* add custom vdom constructor decorator

* change to snake

* fix err msg

* cast keys to strings

* make pub

* fix dashed html attrs

* rename to rewrite-keys

* fix types

* allow ints in vdom spec for keys

* fix types

* camelCase to snake_case + rewrite util

* fix types

* get cov

* fix style
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.

Unify Component and VDOM Element Constructor Interface
2 participants