You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ReactPy can generate type hinting errors generated (from Pylance).
For example, the fact that reactpy.component can ingest a ComponentType, but can only return a Component. Or the fact that use_state can be set to None initially and changed later.
I think idom.component is typed correctly. It's input function is a render function which may return ComponentType | VdomDict | None and its output is a constructor for a Component object. The distinction between Component and ComponentType is that the latter is a protocol, an abstract interface. Component on the other hand is a concrete implementation of ComponentType. ContextProvider is another example of a ComponentType.
For use_state, if its value is initially None but may also be some other type (e.g. a str) then you must explicitly declare this upfront.
Admittedly, while these solutions work, I think they all feel a bit awkward. Here are two alternatives:
# Use a named tuplestate: State[str|None] =use_state(None)
value, set_value=statestate.valuestate.set_value# Or do some weird typing magic to make this possiblevalue, 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, use_state is complicated by the fact that Python 3.9 broke generic NamedTuples which we'd need to get this working. The best we'd be able to do is a generic tuple till this is fixed in Python 3.11.
Also, presently the @component hides the type annotations of the underlying rendering function. This is because @component adds the special key parameter that is not consumed by the rendering function. Typing this correctly is blocked on concatenating keyword-only arguments with ParamSpec
Currently getting type hint errors on the component decorator, if str() is directly returned within a component. Or if a component is directly returned within a component.
Argument of type "None" cannot be assigned to parameter "attributes_and_children" of type "VdomAttributesAndChildren" in function "__call__"
Type "None" cannot be assigned to type "VdomAttributesAndChildren"
Type "None" cannot be assigned to type "Mapping[str, Any]"
"__iter__" is not present
"key" is not present
"type" is not present
"render" is not present
"should_render" is not present
Type "None" cannot be assigned to type "VdomDict"
...Pylance[reportGeneralTypeIssues](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)
Argument of type "(*args: Unknown, **kwargs: Unknown) -> (Component | Unknown)" cannot be assigned to parameter "function" of type "(...) -> (ComponentType | VdomDict | None)" in function "component"
Type "(*args: Unknown, **kwargs: Unknown) -> (Component | Unknown)" cannot be assigned to type "(...) -> (ComponentType | VdomDict | None)"
Function return type "Component | Unknown" is incompatible with type "ComponentType | VdomDict | None"
Type "Component | Unknown" cannot be assigned to type "ComponentType | VdomDict | None"
Type "Component" cannot be assigned to type "ComponentType | VdomDict | None"
"Component" is incompatible with protocol "ComponentType"
"Component" is incompatible with "VdomDict"
Type cannot be assigned to type "None"Pylance[reportGeneralTypeIssues](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)
Seems like the main MyPy issue is _Type is not correctly inferred for state and set_state.
Basically every usage of state/set_state result errors similar to Argument 1 has incompatible type "bool"; expected "Union[_Type, Callable[[_Type], _Type]]"
or Argument 2 to "Mutation" has incompatible type "_Type"; expected "bool"
The type hints around reactpy.html elements is too strict. Frankly, we may as well just accept Any for children and have the end-user handle incompatibilities.
The Context type created by create_context() currently suggests that a list of elements can be provided. This is unfortunately false, and will result in rendering failures.
Either remove this type hint, or begin official support for lists in Context.
Activity
rmorshea commentedon Jul 30, 2022
I think
idom.componentis typed correctly. It's input function is a render function which may returnComponentType | VdomDict | Noneand its output is a constructor for aComponentobject. The distinction betweenComponentandComponentTypeis that the latter is a protocol, an abstract interface.Componenton the other hand is a concrete implementation ofComponentType.ContextProvideris another example of aComponentType.For
use_state, if its value is initiallyNonebut may also be some other type (e.g. astr) then you must explicitly declare this upfront.Admittedly, while these solutions work, I think they all feel a bit awkward. Here are two alternatives:
I haven't seen the latter pattern used very many places so it might surprise people, but it looks nice.
rmorshea commentedon Jul 30, 2022
On thinking further,
use_stateis complicated by the fact that Python 3.9 broke genericNamedTuples which we'd need to get this working. The best we'd be able to do is a generic tuple till this is fixed in Python 3.11.rmorshea commentedon Sep 27, 2022
Mypy 0981 includes support for generic NamedTuples.
rmorshea commentedon Sep 27, 2022
Also, presently the
@componenthides the type annotations of the underlying rendering function. This is because@componentadds the specialkeyparameter that is not consumed by the rendering function. Typing this correctly is blocked on concatenating keyword-only arguments withParamSpecArchmonger commentedon Oct 7, 2022
Currently getting type hint errors on the component decorator, if
str()is directly returned within a component. Or if a component is directly returned within a component.Archmonger commentedon Oct 19, 2022
VDOM constructor type hints currently don't like
None.rmorshea commentedon Oct 19, 2022
In the case a
Nonechild is passed?Archmonger commentedon Oct 19, 2022
Yeah, for example a conditional to return a component or
Nonewithin a div tag.Archmonger commentedon Oct 19, 2022
It's also still very annoying that I can't directly return a component within a component without type hint errors.
rmorshea commentedon Oct 19, 2022
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.
Archmonger commentedon Oct 19, 2022
16 remaining items
rmorshea commentedon Jan 27, 2023
Can you elaborate on what those issues are?
Archmonger commentedon Jan 27, 2023
Seems like the main MyPy issue is
_Typeis not correctly inferred forstateandset_state.Basically every usage of
state/set_stateresult errors similar toArgument 1 has incompatible type "bool"; expected "Union[_Type, Callable[[_Type], _Type]]"or
Argument 2 to "Mutation" has incompatible type "_Type"; expected "bool"You can see this in
django_idom.hooks.rmorshea commentedon Jan 27, 2023
I've been noticing some weird behavior with my MyPy cache. Does the error persist if you clear the cache first?
Archmonger commentedon Jan 27, 2023
Clearing cache fixed the issues.
How strange... Maybe we can see if sqlite_cache resolves this issue?
rmorshea commentedon Jan 27, 2023
Yeah, I have no idea what's going on. I've been meaning to try and write up a bug report.
Archmonger commentedon Jan 30, 2023
The issue kept returning in my environment.
Changing
sqlite_cacheandcache_fine_graineddid not resolve the issue.However, it seems like changing
incrementaltofalsedoes resolve the issue.rmorshea commentedon Jan 30, 2023
That's good to know, I've been manually deleting the cache on every run.
Archmonger commentedon Sep 25, 2023
Note to self:
The type hints around
reactpy.htmlelements is too strict. Frankly, we may as well just acceptAnyfor children and have the end-user handle incompatibilities.Archmonger commentedon Oct 12, 2024
VdomAttributesshould be rewritten todict[str, Any]since type hinters really don't giving any auto completion forMutableMapping.Archmonger commentedon Oct 13, 2024
The
Contexttype created bycreate_context()currently suggests that a list of elements can be provided. This is unfortunately false, and will result in rendering failures.Either remove this type hint, or begin official support for lists in
Context.Archmonger commentedon Oct 15, 2024
Nonetype is currently an accepted type forContext, but is rendered as a literal stringNonewhen used as a child element.Archmonger commentedon Dec 4, 2024
VdomDictandVdomChildare currently incompatible types, despite being the same thing. We should remove one of them.Archmonger commentedon Dec 11, 2024
Layoutcurrently only acceptsComponentTypeas an arg, butContextshould be allowed as well.