fix pyright issues

This commit is contained in:
Khaleel Al-Adhami 2024-08-14 15:00:47 -07:00
parent d516b3bfc4
commit e92218da94
7 changed files with 32 additions and 23 deletions

View File

@ -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(

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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})'

View File

@ -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

View File

@ -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])
},