From e92218da942c468bf2afb8c9bd9e7fae9e92daf4 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 14 Aug 2024 15:00:47 -0700 Subject: [PATCH] fix pyright issues --- reflex/ivars/base.py | 9 +++++---- reflex/ivars/sequence.py | 12 +++++++++--- reflex/style.py | 8 +++++--- tests/components/media/test_image.py | 4 ++-- tests/test_event.py | 2 +- tests/test_state.py | 18 +++++++++--------- tests/test_style.py | 2 +- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/reflex/ivars/base.py b/reflex/ivars/base.py index 9b41c7dc6..88e8d0706 100644 --- a/reflex/ivars/base.py +++ b/reflex/ivars/base.py @@ -851,18 +851,19 @@ class LiteralVar(ImmutableVar): if isinstance(value, Img): with io.BytesIO() as buffer: - value.save(buffer, format=getattr(value, "format", None) or "PNG") + image_format = getattr(value, "format", None) or "PNG" + value.save(buffer, format=image_format) try: # Newer method to get the mime type, but does not always work. - mimetype = value.get_format_mimetype() + mimetype = value.get_format_mimetype() # type: ignore except AttributeError: try: # Fallback method - mimetype = MIME[value.format] + mimetype = MIME[image_format] except KeyError: # Unknown mime_type: warn and return image/png and hope the browser can sort it out. warnings.warn( # noqa: B028 - f"Unknown mime type for {value} {value.format}. Defaulting to image/png" + f"Unknown mime type for {value} {image_format}. Defaulting to image/png" ) mimetype = "image/png" return LiteralStringVar.create( diff --git a/reflex/ivars/sequence.py b/reflex/ivars/sequence.py index 486c0c96f..6cec4681b 100644 --- a/reflex/ivars/sequence.py +++ b/reflex/ivars/sequence.py @@ -860,13 +860,19 @@ class ConcatVarOperation(StringVar): Returns: The name of the var. """ - list_of_strs = [""] + list_of_strs: List[Union[str, Var]] = [] + last_string = "" for var in self._var_value: if isinstance(var, LiteralStringVar): - list_of_strs[-1] += var._var_value + last_string += var._var_value else: + if last_string: + list_of_strs.append(last_string) + last_string = "" list_of_strs.append(var) - list_of_strs.append("") + + if last_string: + list_of_strs.append(last_string) list_of_strs_filtered = [ str(LiteralVar.create(s)) for s in list_of_strs if isinstance(s, Var) or s diff --git a/reflex/style.py b/reflex/style.py index 0591ccebb..a844764e9 100644 --- a/reflex/style.py +++ b/reflex/style.py @@ -116,7 +116,7 @@ def media_query(breakpoint_expr: str): def convert_item( style_item: str | Var, -) -> tuple[str, VarData | ImmutableVarData | None]: +) -> tuple[Var, VarData | ImmutableVarData | None]: """Format a single value in a style dictionary. Args: @@ -136,7 +136,7 @@ def convert_item( def convert_list( responsive_list: list[str | dict | Var], -) -> tuple[list[str | dict], VarData | None]: +) -> tuple[list[Var | dict[str, Var | list | dict]], VarData | None]: """Format a responsive value list. Args: @@ -158,7 +158,9 @@ def convert_list( return converted_value, VarData.merge(*item_var_datas) -def convert(style_dict): +def convert( + style_dict: dict[str, Var | dict | list | str], +) -> tuple[dict[str, Var | list | dict], VarData | None]: """Format a style dictionary. Args: diff --git a/tests/components/media/test_image.py b/tests/components/media/test_image.py index 198fbc844..ba8353260 100644 --- a/tests/components/media/test_image.py +++ b/tests/components/media/test_image.py @@ -37,7 +37,7 @@ def test_set_src_str(): """Test that setting the src works.""" image = rx.image(src="pic2.jpeg") # when using next/image, we explicitly create a _var_is_str Var - assert str(image.src) in ( + assert str(image.src) in ( # type: ignore '"pic2.jpeg"', "'pic2.jpeg'", "`pic2.jpeg`", @@ -63,4 +63,4 @@ def test_render(pil_image: Img): pil_image: The image to serialize. """ image = Image.create(src=pil_image) - assert isinstance(image.src, StringVar) + assert isinstance(image.src, StringVar) # type: ignore diff --git a/tests/test_event.py b/tests/test_event.py index 5e1537224..a484e0a3c 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -216,7 +216,7 @@ def test_event_console_log(): assert spec.args[0][0].equals(ImmutableVar(_var_name="message", _var_type=str)) assert spec.args[0][1].equals(LiteralVar.create("message")) assert format.format_event(spec) == 'Event("_console", {message:"message"})' - spec = event.console_log(ImmutableVar.create("message")) + spec = event.console_log(ImmutableVar.create_safe("message")) assert format.format_event(spec) == 'Event("_console", {message:message})' diff --git a/tests/test_state.py b/tests/test_state.py index 11cab8df8..053ebd30a 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -2602,23 +2602,23 @@ def test_state_union_optional(): c3r: Custom3 = Custom3(c2r=Custom2(c1r=Custom1(foo=""))) custom_union: Union[Custom1, Custom2, Custom3] = Custom1(foo="") - assert str(UnionState.c3.c2) == f'{str(UnionState.c3)}?.["c2"]' - assert str(UnionState.c3.c2.c1) == f'{str(UnionState.c3)}?.["c2"]?.["c1"]' + assert str(UnionState.c3.c2) == f'{str(UnionState.c3)}?.["c2"]' # type: ignore + assert str(UnionState.c3.c2.c1) == f'{str(UnionState.c3)}?.["c2"]?.["c1"]' # type: ignore assert ( - str(UnionState.c3.c2.c1.foo) == f'{str(UnionState.c3)}?.["c2"]?.["c1"]?.["foo"]' + str(UnionState.c3.c2.c1.foo) == f'{str(UnionState.c3)}?.["c2"]?.["c1"]?.["foo"]' # type: ignore ) assert ( - str(UnionState.c3.c2.c1r.foo) == f'{str(UnionState.c3)}?.["c2"]?.["c1r"]["foo"]' + str(UnionState.c3.c2.c1r.foo) == f'{str(UnionState.c3)}?.["c2"]?.["c1r"]["foo"]' # type: ignore ) - assert str(UnionState.c3.c2r.c1) == f'{str(UnionState.c3)}?.["c2r"]["c1"]' + assert str(UnionState.c3.c2r.c1) == f'{str(UnionState.c3)}?.["c2r"]["c1"]' # type: ignore assert ( - str(UnionState.c3.c2r.c1.foo) == f'{str(UnionState.c3)}?.["c2r"]["c1"]?.["foo"]' + str(UnionState.c3.c2r.c1.foo) == f'{str(UnionState.c3)}?.["c2r"]["c1"]?.["foo"]' # type: ignore ) assert ( - str(UnionState.c3.c2r.c1r.foo) == f'{str(UnionState.c3)}?.["c2r"]["c1r"]["foo"]' + str(UnionState.c3.c2r.c1r.foo) == f'{str(UnionState.c3)}?.["c2r"]["c1r"]["foo"]' # type: ignore ) - assert str(UnionState.c3i.c2) == f'{str(UnionState.c3i)}["c2"]' - assert str(UnionState.c3r.c2) == f'{str(UnionState.c3r)}["c2"]' + assert str(UnionState.c3i.c2) == f'{str(UnionState.c3i)}["c2"]' # type: ignore + assert str(UnionState.c3r.c2) == f'{str(UnionState.c3r)}["c2"]' # type: ignore assert UnionState.custom_union.foo is not None # type: ignore assert UnionState.custom_union.c1 is not None # type: ignore assert UnionState.custom_union.c1r is not None # type: ignore diff --git a/tests/test_style.py b/tests/test_style.py index 64c7fc9e7..8fa443cdf 100644 --- a/tests/test_style.py +++ b/tests/test_style.py @@ -377,7 +377,7 @@ class StyleState(rx.State): ( {"color": f"dark{StyleState.color}"}, { - "css": ImmutableVar.create( + "css": ImmutableVar.create_safe( f'({{ ["color"] : ("dark"+{StyleState.color}) }})' ).to(Dict[str, str]) },