Skip to content

Commit

Permalink
ruff-format: unify Black with Ruff v0.1 (#2837)
Browse files Browse the repository at this point in the history
* ruff-format: unify Black with Ruff

* ruff lint.

* v0.1.0

* run precommit

* hand fixing

* fix `not isinstance(...)`

Co-authored-by: Thomas Brandého <thomas.brandeho@gmail.com>

* rev isinstance(...) with noqa

* exclude = ["*.pyi"]

* apply precommit

* apply format

* revert pyi

* fixing

* Fix or

Co-authored-by: Thomas Brandého <thomas.brandeho@gmail.com>

* Update reflex/components/datadisplay/code.pyi

* Apply suggestions from code review

Co-authored-by: Thomas Brandého <thomas.brandeho@gmail.com>

* Update tests/components/core/test_colors.py

* Update reflex/app.py

* Update reflex/app.py

* Update benchmarks/test_benchmark_compile_pages.py

* Update benchmarks/test_benchmark_compile_pages.py

* Update benchmarks/test_benchmark_compile_pages.py

---------

Co-authored-by: Thomas Brandého <thomas.brandeho@gmail.com>
  • Loading branch information
Borda and Lendemor authored Apr 23, 2024
1 parent 8e4d6a4 commit 4d567b7
Show file tree
Hide file tree
Showing 34 changed files with 135 additions and 98 deletions.
11 changes: 4 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
fail_fast: true

repos:
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
args: [integration, reflex, tests]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.244
rev: v0.1.0
hooks:
- id: ruff-format
args: [integration, reflex, tests]
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
args: ["--fix", "--exit-non-zero-on-fix"]
exclude: '^integration/benchmarks/'

- repo: https://github.com/RobertCraigie/pyright-python
Expand Down
9 changes: 6 additions & 3 deletions benchmarks/test_benchmark_compile_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def app_with_10_components(
yield AppHarness.create(
root=root,
app_source=functools.partial(
AppWithTenComponentsOnePage, render_component=render_component # type: ignore
AppWithTenComponentsOnePage,
render_component=render_component, # type: ignore
),
) # type: ignore

Expand All @@ -184,7 +185,8 @@ def app_with_100_components(
yield AppHarness.create(
root=root,
app_source=functools.partial(
AppWithHundredComponentOnePage, render_component=render_component # type: ignore
AppWithHundredComponentOnePage,
render_component=render_component, # type: ignore
),
) # type: ignore

Expand All @@ -206,7 +208,8 @@ def app_with_1000_components(
yield AppHarness.create(
root=root,
app_source=functools.partial(
AppWithThousandComponentsOnePage, render_component=render_component # type: ignore
AppWithThousandComponentsOnePage,
render_component=render_component, # type: ignore
),
) # type: ignore

Expand Down
17 changes: 13 additions & 4 deletions benchmarks/test_benchmark_compile_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ def app_with_ten_pages(
an AppHarness instance
"""
root = tmp_path_factory.mktemp(f"app10")
yield AppHarness.create(root=root, app_source=functools.partial(AppWithTenPages, render_comp=render_multiple_pages)) # type: ignore
yield AppHarness.create(
root=root,
app_source=functools.partial(
AppWithTenPages,
render_comp=render_multiple_pages, # type: ignore
),
)


@pytest.fixture(scope="session")
Expand All @@ -245,7 +251,8 @@ def app_with_hundred_pages(
yield AppHarness.create(
root=root,
app_source=functools.partial(
AppWithHundredPages, render_comp=render_multiple_pages # type: ignore
AppWithHundredPages,
render_comp=render_multiple_pages, # type: ignore
),
) # type: ignore

Expand All @@ -267,7 +274,8 @@ def app_with_thousand_pages(
yield AppHarness.create(
root=root,
app_source=functools.partial( # type: ignore
AppWithThousandPages, render_comp=render_multiple_pages # type: ignore
AppWithThousandPages,
render_comp=render_multiple_pages, # type: ignore
),
) # type: ignore

Expand All @@ -289,7 +297,8 @@ def app_with_ten_thousand_pages(
yield AppHarness.create(
root=root,
app_source=functools.partial(
AppWithTenThousandPages, render_comp=render_multiple_pages # type: ignore
AppWithTenThousandPages,
render_comp=render_multiple_pages, # type: ignore
),
) # type: ignore

Expand Down
14 changes: 8 additions & 6 deletions integration/test_call_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,14 @@ def test_call_script(
yield_callback_button.click()
update_counter_button.click()
assert call_script.poll_for_value(counter, exp_not_equal="0") == "4"
assert call_script.poll_for_value(
results, exp_not_equal="[]"
) == '["%s1",null,{"%s3":42,"a":[1,2,3],"s":"js","o":{"a":1,"b":2}},"async %s4"]' % (
script,
script,
script,
assert (
call_script.poll_for_value(results, exp_not_equal="[]")
== '["%s1",null,{"%s3":42,"a":[1,2,3],"s":"js","o":{"a":1,"b":2}},"async %s4"]'
% (
script,
script,
script,
)
)
reset_button.click()
assert call_script.poll_for_value(counter, exp_not_equal="4") == "0"
Expand Down
9 changes: 7 additions & 2 deletions integration/test_dynamic_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ def index():
rx.link("index", href="/", id="link_index"),
rx.link("page_X", href="/static/x", id="link_page_x"),
rx.link(
"next", href="/page/" + DynamicState.next_page, id="link_page_next" # type: ignore
"next",
href="/page/" + DynamicState.next_page,
id="link_page_next", # type: ignore
),
rx.link("missing", href="/missing", id="link_missing"),
rx.chakra.list(
rx.foreach(DynamicState.order, lambda i: rx.chakra.list_item(rx.text(i))), # type: ignore
rx.foreach(
DynamicState.order, # type: ignore
lambda i: rx.chakra.list_item(rx.text(i)),
),
),
)

Expand Down
6 changes: 5 additions & 1 deletion integration/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def another():
),
),
rx.chakra.tfoot( # type: ignore
rx.chakra.tr(rx.chakra.td("footer1"), rx.chakra.td("footer2"), rx.chakra.td("footer3")) # type: ignore
rx.chakra.tr(
rx.chakra.td("footer1"),
rx.chakra.td("footer2"),
rx.chakra.td("footer3"),
) # type: ignore
),
rx.chakra.table_caption("random caption"),
variant="striped",
Expand Down
14 changes: 5 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,12 @@ build-backend = "poetry.core.masonry.api"
[tool.pyright]

[tool.ruff]

select = ["B", "D", "E", "F", "I", "SIM", "W"]

ignore = ["B008", "D203", "D205", "D213", "D401", "D406", "D407", "E501", "F403", "F405", "F541"]

exclude = ["*.pyi"]
target-version = "py37"

[tool.ruff.per-file-ignores]

lint.select = ["B", "D", "E", "F", "I", "SIM", "W"]
lint.ignore = ["B008", "D203", "D205", "D213", "D401", "D406", "D407", "E501", "F403", "F405", "F541"]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*.py" = ["D100", "D103", "D104"]
"tests/*.py" = ["D100", "D103", "D104", "B018"]
"reflex/.templates/*.py" = ["D100", "D103", "D104"]
"*.pyi" = ["ALL"]
8 changes: 4 additions & 4 deletions reflex/.templates/apps/demo/code/pages/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ class DataTableState(State):
data_show = """[
["1", "Harry James Potter", "31 July 1980", True, "Gryffindor", "11' Holly phoenix feather", "Stag", "Half-blood"],
["2", "Ronald Bilius Weasley", "1 March 1980", True,"Gryffindor", "12' Ash unicorn tail hair", "Jack Russell terrier", "Pure-blood"],
["3", "Hermione Jean Granger", "19 September, 1979", True, "Gryffindor", "10¾' vine wood dragon heartstring", "Otter", "Muggle-born"],
["4", "Albus Percival Wulfric Brian Dumbledore", "Late August 1881", True, "Gryffindor", "15' Elder Thestral tail hair core", "Phoenix", "Half-blood"],
["5", "Rubeus Hagrid", "6 December 1928", False, "Gryffindor", "16' Oak unknown core", "None", "Part-Human (Half-giant)"],
["6", "Fred Weasley", "1 April, 1978", True, "Gryffindor", "Unknown", "Unknown", "Pure-blood"],
["3", "Hermione Jean Granger", "19 September, 1979", True, "Gryffindor", "10¾' vine wood dragon heartstring", "Otter", "Muggle-born"],
["4", "Albus Percival Wulfric Brian Dumbledore", "Late August 1881", True, "Gryffindor", "15' Elder Thestral tail hair core", "Phoenix", "Half-blood"],
["5", "Rubeus Hagrid", "6 December 1928", False, "Gryffindor", "16' Oak unknown core", "None", "Part-Human (Half-giant)"],
["6", "Fred Weasley", "1 April, 1978", True, "Gryffindor", "Unknown", "Unknown", "Pure-blood"],
["7", "George Weasley", "1 April, 1978", True, "Gryffindor", "Unknown", "Unknown", "Pure-blood"],
]"""

Expand Down
4 changes: 2 additions & 2 deletions reflex/.templates/apps/demo/code/pages/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
rx.chakra.checkbox("Checked", id="check"),
rx.chakra.switch("Switched", id="switch"),
),
rx.chakra.button("Submit",
type_="submit",
rx.chakra.button("Submit",
type_="submit",
bg="#ecfdf5",
color="#047857",
border_radius="lg",
Expand Down
10 changes: 8 additions & 2 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,17 @@ async def postprocess(
for middleware in self.middleware:
if asyncio.iscoroutinefunction(middleware.postprocess):
out = await middleware.postprocess(
app=self, state=state, event=event, update=update # type: ignore
app=self, # type: ignore
state=state,
event=event,
update=update,
)
else:
out = middleware.postprocess(
app=self, state=state, event=event, update=update # type: ignore
app=self, # type: ignore
state=state,
event=event,
update=update,
)
if out is not None:
return out # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/chakra/disclosure/accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create(
icon_pos="right",
allow_multiple: Optional[Var[bool]] = None,
allow_toggle: Optional[Var[bool]] = None,
**props
**props,
) -> Component:
"""Create an accordion component.
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/chakra/media/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def create(cls, *children, **props):
raise AttributeError(
f"Passing children to Icon component is not allowed: remove positional arguments {children} to fix"
)
if "tag" not in props.keys():
if "tag" not in props:
raise AttributeError("Missing 'tag' keyword-argument for Icon")
if type(props["tag"]) != str or props["tag"].lower() not in ICON_LIST:
if not isinstance(props["tag"], str) or props["tag"].lower() not in ICON_LIST:
raise ValueError(
f"Invalid icon tag: {props['tag']}. Please use one of the following: {sorted(ICON_LIST)}"
)
Expand Down
6 changes: 2 additions & 4 deletions reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ def __init__(self, *args, **kwargs):
else:
value = base_value
else:
value = Var.create(value, _var_is_string=type(value) is str)
value = Var.create(value, _var_is_string=isinstance(value, str))

# Set the prop.
self.props[format.to_camel_case(key)] = value
Expand Down Expand Up @@ -1590,9 +1590,7 @@ def _get_dynamic_imports(self) -> str:
library_import = f"const {self.alias if self.alias else self.tag} = dynamic(() => import('{import_name}')"
mod_import = (
# https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#with-named-exports
f".then((mod) => mod.{self.tag})"
if not self.is_default
else ""
f".then((mod) => mod.{self.tag})" if not self.is_default else ""
)
return "".join((library_import, mod_import, opts_fragment))

Expand Down
2 changes: 1 addition & 1 deletion reflex/components/core/cond.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def cond(condition: Any, c1: Any, c2: Any = None):
def create_var(cond_part):
return Var.create_safe(
cond_part,
_var_is_string=type(cond_part) is str or isinstance(cond_part, Color),
_var_is_string=isinstance(cond_part, (str, Color)),
)

# convert the truth and false cond parts into vars so the _var_data can be obtained.
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/core/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _create_condition_var(cls, cond: Any) -> BaseVar:
Raises:
ValueError: If the condition is not provided.
"""
match_cond_var = Var.create(cond, _var_is_string=type(cond) is str)
match_cond_var = Var.create(cond, _var_is_string=isinstance(cond, str))

if match_cond_var is None:
raise ValueError("The condition must be set")
Expand Down Expand Up @@ -119,7 +119,7 @@ def _create_case_var_with_var_data(cls, case_element):
_var_data = case_element._var_data if isinstance(case_element, Style) else None # type: ignore
case_element = Var.create(
case_element,
_var_is_string=type(case_element) is str or isinstance(case_element, Color),
_var_is_string=isinstance(case_element, (str, Color)),
)
if _var_data is not None:
case_element._var_data = VarData.merge(case_element._var_data, _var_data) # type: ignore
Expand Down
6 changes: 3 additions & 3 deletions reflex/components/lucide/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ def map_deprecated_icon_names_05(tag: str) -> str:
return tag

if children:
if len(children) == 1 and type(children[0]) == str:
if len(children) == 1 and isinstance(children[0], str):
props["tag"] = children[0]
children = []
else:
raise AttributeError(
f"Passing multiple children to Icon component is not allowed: remove positional arguments {children[1:]} to fix"
)
if "tag" not in props.keys():
if "tag" not in props:
raise AttributeError("Missing 'tag' keyword-argument for Icon")

if (
type(props["tag"]) != str
not isinstance(props["tag"], str)
or map_deprecated_icon_names_05(format.to_snake_case(props["tag"]))
not in LUCIDE_ICON_LIST
):
Expand Down
12 changes: 4 additions & 8 deletions reflex/components/markdown/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def create(cls, *children, **props) -> Component:
Returns:
The markdown component.
"""
assert len(children) == 1 and types._isinstance(
children[0], Union[str, Var]
assert (
len(children) == 1 and types._isinstance(children[0], Union[str, Var])
), "Markdown component must have exactly one child containing the markdown source."

# Custom styles are deprecated.
Expand Down Expand Up @@ -257,9 +257,7 @@ def format_component_map(self) -> dict[str, str]:
}

# Separate out inline code and code blocks.
components[
"code"
] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{
components["code"] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{
const match = (className || '').match(/language-(?<lang>.*)/);
const language = match ? match[1] : '';
if (language) {{
Expand All @@ -277,9 +275,7 @@ def format_component_map(self) -> dict[str, str]:
) : (
{self.format_component("codeblock", language=Var.create_safe("language", _var_is_local=False))}
);
}}}}""".replace(
"\n", " "
)
}}}}""".replace("\n", " ")

return components

Expand Down
4 changes: 2 additions & 2 deletions reflex/components/radix/themes/components/icon_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create(cls, *children, **props) -> Component:
The IconButton component.
"""
if children:
if type(children[0]) == str:
if isinstance(children[0], str):
children = [
Icon.create(
children[0],
Expand All @@ -68,7 +68,7 @@ def create(cls, *children, **props) -> Component:
"IconButton requires a child icon. Pass a string as the first child or a rx.icon."
)
if "size" in props:
if type(props["size"]) == str:
if isinstance(props["size"], str):
RADIX_TO_LUCIDE_SIZE = {
"1": "12px",
"2": "24px",
Expand Down
6 changes: 2 additions & 4 deletions reflex/components/radix/themes/components/radio_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ def create(

# convert only non-strings to json(JSON.stringify) so quotes are not rendered
# for string literal types.
if (
type(default_value) is str
or isinstance(default_value, Var)
and default_value._var_type is str
if isinstance(default_value, str) or (
isinstance(default_value, Var) and default_value._var_type is str
):
default_value = Var.create(default_value, _var_is_string=True) # type: ignore
else:
Expand Down
1 change: 0 additions & 1 deletion reflex/custom_components/custom_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ def _ensure_dist_dir(version_to_publish: str, build: bool):
if build:
# Need to check if the files here are for the version to be published.
if dist_dir.exists():

# Check if the distribution files are for the version to be published.
needs_rebuild = False
for suffix in CustomComponents.DISTRIBUTION_FILE_SUFFIXES:
Expand Down
Loading

0 comments on commit 4d567b7

Please sign in to comment.