Skip to content

Use props dict on components, rather than kwargs. #993

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

Closed
Archmonger opened this issue May 24, 2023 · 7 comments
Closed

Use props dict on components, rather than kwargs. #993

Archmonger opened this issue May 24, 2023 · 7 comments
Labels
priority-2-moderate Should be resolved on a reasonable timeline. release-major Warrents a major release type-revision About a change in functionality or behavior

Comments

@Archmonger
Copy link
Contributor

Archmonger commented May 24, 2023

Current Situation

There's been a lot of growing pains in the fact that components have a significantly different interface than reactpy.html elements.

Developing certain parts of reactpy core and/or reactpy related utilities has been significantly more difficult due to this interface mismatch.

Proposed Actions

The new component interface should match the VDOM interface, and will look like this:

# `key=...` should be declared within props, identical to the `reactpy.html` interface.
my_component({"prop_1": True, "key": 123}, child_1, child_2)

As a part of this change, we should rename VdomAttributes to Props.

Do we retain args/kwargs?

We can handle @component function definitions a few ways. Some of these ways feel more similar to ReactJS, but have problems with Python type hints.

  1. Force all users to only use kwargs
  2. Allow args/kwargs in function defs. Let Python unpacking operator handle the rest.
    • This would break type hints, since args can't be defined within a TypedDict
  3. Force all users to define a props: dict as their first parameter
    • However, forcing users to manually define a TypedDict for every component would be extremely inconvenient.

I'm personally leaning towards option 1.

@Archmonger Archmonger added priority-2-moderate Should be resolved on a reasonable timeline. type-revision About a change in functionality or behavior release-major Warrents a major release labels May 24, 2023
@rmorshea
Copy link
Collaborator

More precisely the interface should conform to this protocol.

@Archmonger
Copy link
Contributor Author

We can potentially retain type hints using this PEP https://peps.python.org/pep-0692/

@Archmonger Archmonger added this to the 2.0.0 milestone Jun 8, 2023
@Archmonger
Copy link
Contributor Author

I spent more thought on this issue while rewriting the docs.

@rmorshea We need to decide what we will do with the user's component function defs. See my updates above.

@rmorshea
Copy link
Collaborator

rmorshea commented Jun 9, 2023

Note: we'll have to figure out how this will play work if the component function is a method.

@Archmonger Archmonger removed this from the 2.0.0 milestone Feb 2, 2025
@Archmonger
Copy link
Contributor Author

Archmonger commented Feb 12, 2025

I did some prototyping, and learned it's currently not possible to use ParamSpec to provide type hints to a dictionary.

Given that components are functions, it feels natural for users to call them via the standard "python function interface" (AKA arguments within parenthesis)

Additionally, changing the reactpy.html interface to kwargs makes the HTML "read wrong", where all HTML properties are now declared at the bottom of the elements, making it almost unreadable to figure out what prop belongs to which element in deeply nested structures.

Unless the Python type hinting space makes some major changes, I think we're forced to continue down the road we've already paved. I've already raised the issue that the Python type hinting system has no way to "mutate" kwargs, so hopefully that gains some interest/traction.

@Archmonger Archmonger closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2025
@rmorshea
Copy link
Collaborator

rmorshea commented Feb 17, 2025

A draft of the template strings PEP is currently in review so optimizing the interface may not be necessary anyways if that lands.

@Archmonger
Copy link
Contributor Author

Agreed, I recently made that same conclusion while talking about the reactpy.html discussion on Discord.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-2-moderate Should be resolved on a reasonable timeline. release-major Warrents a major release type-revision About a change in functionality or behavior
Projects
None yet
Development

No branches or pull requests

2 participants