Skip to content

Commit

Permalink
[Python] Format generated files using ruff (#3578)
Browse files Browse the repository at this point in the history
* Format generated files in Fable Library using ruff
* Install poetry before building fable-library
* Install Python tools from the repository root
* Invoke ruff from the repository root
* Don't install poetry tools directly from the CI invocation because some paths can be missing

---------

Co-authored-by: Maxime Mangel <mangel.maxime@protonmail.com>
  • Loading branch information
dbrattli and MangelMaxime authored Nov 1, 2023
1 parent 5d309a0 commit 9f309c7
Show file tree
Hide file tree
Showing 29 changed files with 467 additions and 752 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install poetry
run: |
pip install poetry
- name: Fable Library - Python (linux)
if: matrix.platform == 'ubuntu-latest'
run: ./build.sh fable-library --python
Expand All @@ -117,11 +121,6 @@ jobs:
if: matrix.platform == 'windows-latest'
run: .\build.bat fable-library --python

- name: Install dependencies
run: |
pip install poetry
poetry install
- name: Fable Tests - Python (linux)
if: matrix.platform == 'ubuntu-latest'
run: ./build.sh test python --skip-fable-library
Expand Down
141 changes: 27 additions & 114 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ python-dateutil = "^2.8.2"
[tool.poetry.dev-dependencies]
fable-library = {path = "./temp/fable-library-py", develop = true}
pytest = "^6.2.4"
black = {version = "^22.6.0", allow-prereleases = true}
isort = "^5.10.1"
ruff = "^0.1.3"

[tool.pyright]
reportMissingTypeStubs = false
Expand All @@ -37,15 +36,12 @@ reportImplicitStringConcatenation = true
pythonVersion = "3.10"
typeCheckingMode = "strict"

[tool.isort]

[tool.ruff]
# Keep in sync with .pre-commit-config.yaml
line-length = 120
ignore = []
target-version = "py310"
select = ["E", "W", "F", "I", "T", "RUF", "TID", "UP"]
exclude = ["tests", "build", "temp", "src/fable_library", "src/fable_library_rust", "src/fable_library_php"]
include =["*.py"]

[tool.ruff.pydocstyle]
Expand All @@ -54,7 +50,6 @@ convention = "google"
[tool.ruff.isort]
lines-after-imports = 2


[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
18 changes: 18 additions & 0 deletions src/Fable.Build/FableLibrary/Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Build.FableLibrary
open System.IO
open Fake.IO
open Build.Utils
open SimpleExec

type BuildFableLibraryPython() =
inherit
Expand Down Expand Up @@ -30,3 +31,20 @@ type BuildFableLibraryPython() =
Directory.GetFiles(linkedFileFolder, "*") |> Shell.copyFiles this.OutDir

Shell.deleteDir (this.BuildDir </> "fable_library/fable-library")

// Install the python dependencies at the root of the project
Command.Run(
"poetry",
"install"
)

// Run Ruff linter checking import sorting and fix any issues
Command.Run(
"poetry",
$"run ruff --select I --fix {this.BuildDir}"
)
// Run Ruff formatter on all generated files
Command.Run(
"poetry",
$"run ruff format {this.BuildDir}"
)
14 changes: 4 additions & 10 deletions src/fable-library-py/fable_library/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ def _arrow19(_arg_1: _T) -> Async[None]:
def _arrow21(__unit: Literal[None] = None) -> Async[list[_T]]:
return singleton.Return(results)

return singleton.Combine(
singleton.For(computations, _arrow20), singleton.Delay(_arrow21)
)
return singleton.Combine(singleton.For(computations, _arrow20), singleton.Delay(_arrow21))

return delay(delayed)

Expand All @@ -152,9 +150,7 @@ def on_success(x: _T):
def on_error(err: Exception):
ctx.on_success(Choice_makeChoice2Of2(err)) # type: ignore

ctx_ = IAsyncContext.create(
on_success, on_error, ctx.on_cancel, ctx.trampoline, ctx.cancel_token
)
ctx_ = IAsyncContext.create(on_success, on_error, ctx.on_cancel, ctx.trampoline, ctx.cancel_token)
work(ctx_)

return protected_cont(cont)
Expand All @@ -164,7 +160,7 @@ def from_continuations(
f: Callable[
[Continuations[_T]],
None,
]
],
) -> Callable[[IAsyncContext[_T]], None]:
def cont(ctx: IAsyncContext[_T]) -> None:
f((ctx.on_success, ctx.on_error, ctx.on_cancel))
Expand Down Expand Up @@ -247,9 +243,7 @@ def runner() -> None:
run_in_loop(runner)


def start_as_task(
computation: Async[_T], cancellation_token: CancellationToken | None = None
) -> Awaitable[_T]:
def start_as_task(computation: Async[_T], cancellation_token: CancellationToken | None = None) -> Awaitable[_T]:
"""Executes a computation in the thread pool.
If no cancellation token is provided then the default cancellation
Expand Down
32 changes: 8 additions & 24 deletions src/fable-library-py/fable_library/async_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def create(
trampoline: Trampoline | None,
cancel_token: CancellationToken | None,
) -> IAsyncContext[_T]:
return AnonymousAsyncContext(
on_success, on_error, on_cancel, trampoline, cancel_token
)
return AnonymousAsyncContext(on_success, on_error, on_cancel, trampoline, cancel_token)


""" FSharpAsync"""
Expand All @@ -158,9 +156,7 @@ def __init__(
):
self._on_success: Callable[[_T], None] = on_success or empty_continuation
self._on_error: Callable[[Exception], None] = on_error or empty_continuation
self._on_cancel: Callable[[OperationCanceledError], None] = (
on_cancel or empty_continuation
)
self._on_cancel: Callable[[OperationCanceledError], None] = on_cancel or empty_continuation

self._cancel_token = cancel_token
self._trampoline = trampoline
Expand Down Expand Up @@ -264,9 +260,7 @@ def on_success(x: _T) -> None:
# print("Exception: ", err)
ctx.on_error(err)

ctx_ = IAsyncContext.create(
on_success, ctx.on_error, ctx.on_cancel, ctx.trampoline, ctx.cancel_token
)
ctx_ = IAsyncContext.create(on_success, ctx.on_error, ctx.on_cancel, ctx.trampoline, ctx.cancel_token)
return computation(ctx_)

return protected_cont(cont)
Expand All @@ -282,9 +276,7 @@ def f(ctx: IAsyncContext[_T]) -> None:
class AsyncBuilder:
__slots__ = ()

def Bind(
self, computation: Async[_T], binder: Callable[[_T], Async[_U]]
) -> Async[_U]:
def Bind(self, computation: Async[_T], binder: Callable[[_T], Async[_U]]) -> Async[_U]:
return protected_bind(computation, binder)

def Combine(self, computation1: Async[Any], computation2: Async[_T]) -> Async[_T]:
Expand Down Expand Up @@ -329,9 +321,7 @@ def Return(self, value: Any = None) -> Async[Any]:
def ReturnFrom(self, computation: Async[_T]) -> Async[_T]:
return computation

def TryFinally(
self, computation: Async[_T], compensation: Callable[[], None]
) -> Async[_T]:
def TryFinally(self, computation: Async[_T], compensation: Callable[[], None]) -> Async[_T]:
def cont(ctx: IAsyncContext[_T]) -> None:
def on_success(x: _T) -> None:
compensation()
Expand All @@ -345,16 +335,12 @@ def on_cancel(x: OperationCanceledError) -> None:
compensation()
ctx.on_cancel(x)

ctx_ = IAsyncContext.create(
on_success, on_error, on_cancel, ctx.trampoline, ctx.cancel_token
)
ctx_ = IAsyncContext.create(on_success, on_error, on_cancel, ctx.trampoline, ctx.cancel_token)
computation(ctx_)

return protected_cont(cont)

def TryWith(
self, computation: Async[_T], catch_handler: Callable[[Exception], Async[_T]]
) -> Async[_T]:
def TryWith(self, computation: Async[_T], catch_handler: Callable[[Exception], Async[_T]]) -> Async[_T]:
def fn(ctx: IAsyncContext[_T]):
def on_error(err: Exception) -> None:
try:
Expand All @@ -381,9 +367,7 @@ def compensation() -> None:
return self.TryFinally(binder(resource), compensation)

@overload
def While(
self, guard: Callable[[], bool], computation: Async[Literal[None]]
) -> Async[None]:
def While(self, guard: Callable[[], bool], computation: Async[Literal[None]]) -> Async[None]:
...

@overload
Expand Down
Loading

0 comments on commit 9f309c7

Please sign in to comment.