-
-
Notifications
You must be signed in to change notification settings - Fork 319
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
Review User API Type Hints #794
Comments
I think For initial_value: str | None = None
value, set_value = use_state(initial_value)
# Or cast initial value
from typing import cast
value, set_value = use_state(cast(str | None, None))
# Or declare state, set_state upfront
value: str | None
set_value: Callable[[str | None], None]
value, set_value = use_state(None) Admittedly, while these solutions work, I think they all feel a bit awkward. Here are two alternatives: # Use a named tuple
state: State[str | None] = use_state(None)
value, set_value = state
state.value
state.set_value
# Or do some weird typing magic to make this possible
value, set_value = use_state[str | None](None) I haven't seen the latter pattern used very many places so it might surprise people, but it looks nice. |
On thinking further, |
Mypy 0981 includes support for generic NamedTuples. |
Also, presently the |
Currently getting type hint errors on the component decorator, if |
VDOM constructor type hints currently don't like |
In the case a |
Yeah, for example a conditional to return a component or |
It's also still very annoying that I can't directly return a component within a component without type hint errors. |
Can you share an example. I'm not quite sure I understand what you're describing.
Can you share the error you're getting? The type hint for the allowable return type for component functions should allow for this. |
@component
def component_1():
return html.div(
None
)
@component
def component_2():
return html.div()
@component
def component_1():
return component_2() |
For the first case, it looks like the behavior of React is to simply exclude children which are
For the second, I don't observe any problems when I lint with MyPy. What does your VSCode configuration look like? I can't seem to figure out how to reproduce the errors your seeing. |
Yes those errors are pylance specific, using "basic" type checking. I'll post the required vscode settings after work. |
In the vscode {
"python.analysis.typeCheckingMode": "basic",
"python.languageServer": "Pylance",
} |
Did you ever replicate the issue with embedded component type hints? |
Whoops I linked to a comment here thinking it wouldn't close the issue. |
For example @dataclass
class Example():
my_component: ComponentType
...
Example.my_component(params) |
Technically this is true. The component object itself is not actually callable. You should annotate this as |
Actually there's already an annotation for this called |
There is a type hint mismatch causing some annoyance on my side The |
|
In order for PyRight to handle this, |
Traditionally, subtyping is based on inheritance hierarchies, but |
I think the generic tuple on |
Yeah... Far too many annoying to fix type hint errors with the new |
Can you elaborate on what those issues are? |
Seems like the main MyPy issue is Basically every usage of You can see this in |
I've been noticing some weird behavior with my MyPy cache. Does the error persist if you clear the cache first? |
Clearing cache fixed the issues. How strange... Maybe we can see if sqlite_cache resolves this issue? |
Yeah, I have no idea what's going on. I've been meaning to try and write up a bug report. |
The issue kept returning in my environment. Changing However, it seems like changing |
That's good to know, I've been manually deleting the cache on every run. |
Note to self: The type hints around |
|
The Either remove this type hint, or begin official support for lists in |
|
|
|
Current Situation
ReactPy can generate type hinting errors generated (from Pylance).
For example, the fact that
reactpy.component
can ingest aComponentType
, but can only return aComponent
. Or the fact thatuse_state
can be set to None initially and changed later.Proposed Actions
Review user API type hints and fix where needed.
The text was updated successfully, but these errors were encountered: