-
-
Notifications
You must be signed in to change notification settings - Fork 324
Fix component decorator eating static type hints #1199
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
Conversation
It's been a while since these type hints have been broken, but if I recall they broke due to some type hinting restrictions that cropped up. The issue was that a component should contain all of the args and kwargs of their parent (as you mention) but they also need to contain one additional kwarg At the time there was no solution, but I think the concatenate function in the newer Python typing libraries might be able to be jerry-rigged to help here? |
No, |
Unfortunately |
Played around with this for a bit... I'm not seeing a way to fix this without changing our component interface. Kind of upsetting that there are so many Python type hinting limitations. There is a realistic future where pyright/mypy could infer types from an implicit return like this... def component(function: Callable[P, T]):
sig = inspect.signature(function)
if "key" in sig.parameters and sig.parameters["key"].kind in (
inspect.Parameter.KEYWORD_ONLY,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
):
msg = f"Component render function {function} uses reserved parameter 'key'"
raise TypeError(msg)
def constructor(
*args: P.args, key: Any | None = None, **kwargs: P.kwargs
) -> Component:
return Component(function, key, args, kwargs, sig)
return constructor But unfortunately, seems like type checkers just give up implicit parsing as soon as |
I've created a post over at the Python typing forums to see what the sentiment is around fixing this type hinting limitation. |
Marking this as a draft since it's broken with most linters. |
If we shove |
Closing in favor of #993 which will need to be drafted in the future. |
By submitting this pull request you agree that all contributions to this project are made under the MIT license.
Issues
The
@component
decorator eats type hints so you can't easily see the parameters to your component (just shows up as...
)Solution
This updates the type hinting so that component returns the same things its decorating
Checklist
changelog.rst
has been updated with any significant changes.The top screenshot is the current behavior. The bottom is the updated behavior. Note that the top
Component
is reactpy's while the bottomComponent
is my own.