Skip to content

Commit

Permalink
chore: add format to pre-commit (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Mar 18, 2024
1 parent aaa43cb commit ed516f4
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 45 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repos:
hooks:
- id: ruff
args: ["--fix", "--unsafe-fixes"]
- id: ruff-format

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.16
Expand Down
9 changes: 3 additions & 6 deletions src/app_model/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ def dispose(self) -> None:
self._disposers.pop()[1]()

@overload
def register_action(self, action: Action) -> DisposeCallable:
...
def register_action(self, action: Action) -> DisposeCallable: ...

@overload
def register_action(
Expand All @@ -227,8 +226,7 @@ def register_action(
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> CommandDecorator:
...
) -> CommandDecorator: ...

@overload
def register_action(
Expand All @@ -244,8 +242,7 @@ def register_action(
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> DisposeCallable:
...
) -> DisposeCallable: ...

def register_action(
self,
Expand Down
1 change: 1 addition & 0 deletions src/app_model/backends/qt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Qt objects for app_model."""

from ._qaction import QCommandAction, QCommandRuleAction, QMenuItemAction
from ._qkeybindingedit import QModelKeyBindingEdit
from ._qkeymap import (
Expand Down
1 change: 1 addition & 0 deletions src/app_model/expressions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Abstraction on expressions, and contexts in which to evaluate them."""

from ._context import Context, app_model_context, create_context, get_context
from ._context_keys import ContextKey, ContextKeyInfo, ContextNamespace
from ._expressions import (
Expand Down
6 changes: 3 additions & 3 deletions src/app_model/expressions/_context_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ class ContextKey(Name, Generic[A, T]):
Examples
--------
>>> class MyNames(ContextNamespace):
... some_key = ContextKey(0, 'some description', lambda x: sum(x))
... some_key = ContextKey(0, "some description", lambda x: sum(x))
>>> expr = MyNames.some_key > 5 # create an expression using this key
these expressions can be later evaluated with some concrete context.
>>> expr.eval({'some_key': 3}) # False
>>> expr.eval({'some_key': 6}) # True
>>> expr.eval({"some_key": 3}) # False
>>> expr.eval({"some_key": 6}) # True
"""

# This will catalog all ContextKeys that get instantiated, which provides
Expand Down
22 changes: 11 additions & 11 deletions src/app_model/expressions/_expressions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Provides `Expr` and its subclasses."""

from __future__ import annotations

import ast
Expand Down Expand Up @@ -128,10 +129,10 @@ class Expr(ast.AST, Generic[T]):
Examples
--------
>>> expr = parse_expression('myvar > 5')
>>> expr = parse_expression("myvar > 5")
combine expressions with operators
>>> new_expr = expr & parse_expression('v2')
>>> new_expr = expr & parse_expression("v2")
nice `repr`
>>> new_expr
Expand All @@ -147,7 +148,7 @@ class Expr(ast.AST, Generic[T]):
Name(id='v2', ctx=Load())])
evaluate in some context
>>> new_expr.eval(dict(v2='hello!', myvar=8))
>>> new_expr.eval(dict(v2="hello!", myvar=8))
'hello!'
serialize
Expand All @@ -158,13 +159,13 @@ class Expr(ast.AST, Generic[T]):
that can be evaluated repeatedly as some underlying context changes.
```python
light_is_green = Name[bool]('light_is_green')
count = Name[int]('count')
light_is_green = Name[bool]("light_is_green")
count = Name[int]("count")
is_ready = light_is_green & count > 5
assert is_ready.eval({'count': 4, 'light_is_green': True}) == False
assert is_ready.eval({'count': 7, 'light_is_green': False}) == False
assert is_ready.eval({'count': 7, 'light_is_green': True}) == True
assert is_ready.eval({"count": 4, "light_is_green": True}) == False
assert is_ready.eval({"count": 7, "light_is_green": False}) == False
assert is_ready.eval({"count": 7, "light_is_green": True}) == True
```
this will also preserve type information:
Expand Down Expand Up @@ -466,7 +467,7 @@ class ExprTransformer(ast.NodeTransformer):
Examples
--------
>>> tree = ast.parse('my_var > 11', mode='eval')
>>> tree = ast.parse("my_var > 11", mode="eval")
>>> tree = ExprTransformer().visit(tree) # transformed
"""

Expand All @@ -480,7 +481,6 @@ def visit(self, node: ast.expr) -> Expr: ...
@overload
def visit(self, node: PassedType) -> PassedType: ...
# fmt: on

def visit(self, node: ast.AST) -> ast.AST | None:
"""Visit a node in the tree, transforming into Expr."""
if isinstance(
Expand Down Expand Up @@ -523,7 +523,7 @@ class _ExprSerializer(ast.NodeVisitor):
Examples
--------
>>> expr = Expr.parse('a + b == c')
>>> expr = Expr.parse("a + b == c")
>>> print(expr)
'a + b == c'
Expand Down
1 change: 1 addition & 0 deletions src/app_model/registries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""App-model registries, such as menus, keybindings, commands."""

from ._commands_reg import CommandsRegistry
from ._keybindings_reg import KeyBindingsRegistry
from ._menus_reg import MenusRegistry
Expand Down
32 changes: 16 additions & 16 deletions src/app_model/registries/_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@


@overload
def register_action(app: Application | str, id_or_action: Action) -> DisposeCallable:
...
def register_action(
app: Application | str, id_or_action: Action
) -> DisposeCallable: ...


@overload
Expand All @@ -39,8 +40,7 @@ def register_action(
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> CommandDecorator:
...
) -> CommandDecorator: ...


@overload
Expand All @@ -57,8 +57,7 @@ def register_action(
menus: list[MenuRuleOrDict] | None = ...,
keybindings: list[KeyBindingRuleOrDict] | None = ...,
palette: bool = True,
) -> DisposeCallable:
...
) -> DisposeCallable: ...


def register_action(
Expand Down Expand Up @@ -159,10 +158,10 @@ def register_action(
from app_model import Application, Action, register_action
app = Application.get_or_create("myapp")
action = Action('my_action', title='My Action', callback=lambda: print("hi"))
action = Action("my_action", title="My Action", callback=lambda: print("hi"))
register_action(app, action)
app.commands.execute_command('my_action') # prints "hi"
app.commands.execute_command("my_action") # prints "hi"
```
## Creating a new Action
Expand All @@ -175,12 +174,12 @@ def register_action(
```python
register_action(
app,
'my_action2',
title='My Action2',
"my_action2",
title="My Action2",
callback=lambda: print("hello again!"),
)
app.commands.execute_command('my_action2') # prints "hello again!"
app.commands.execute_command("my_action2") # prints "hello again!"
```
## Usage as a decorator
Expand All @@ -189,11 +188,12 @@ def register_action(
decorate a function as the executor of the command:
```python
@register_action(app, 'my_action3', title='My Action3')
@register_action(app, "my_action3", title="My Action3")
def my_action3():
print("hello again, again!")
app.commands.execute_command('my_action3') # prints "hello again, again!"
app.commands.execute_command("my_action3") # prints "hello again, again!"
```
## Passing app as a string
Expand All @@ -206,9 +206,9 @@ def my_action3():
```python
register_action(
'myapp', # app name instead of Application instance
'my_action4',
title='My Action4',
"myapp", # app name instead of Application instance
"my_action4",
title="My Action4",
callback=lambda: print("hello again, again, again!"),
)
```
Expand Down
1 change: 1 addition & 0 deletions src/app_model/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""App-model types."""

from typing import TYPE_CHECKING

from ._action import Action
Expand Down
9 changes: 3 additions & 6 deletions src/app_model/types/_keys/_key_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,13 @@ class KeyMod(IntFlag):
WinCtrl = 1 << 8 # meta key on windows, ctrl key on mac

@overload # type: ignore
def __or__(self, other: "KeyMod") -> "KeyMod":
...
def __or__(self, other: "KeyMod") -> "KeyMod": ...

@overload
def __or__(self, other: KeyCode) -> "KeyCombo":
...
def __or__(self, other: KeyCode) -> "KeyCombo": ...

@overload
def __or__(self, other: int) -> int:
...
def __or__(self, other: int) -> int: ...

def __or__(
self, other: Union["KeyMod", KeyCode, int]
Expand Down
2 changes: 1 addition & 1 deletion src/app_model/types/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def import_python_name(python_name: str) -> Any:
--------
>>> import_python_name("my_package.a_module:some_function")
<function some_function at 0x...>
>>> import_python_name('pydantic:BaseModel')
>>> import_python_name("pydantic:BaseModel")
<class 'pydantic.main.BaseModel'>
"""
_validate_python_name(python_name) # shows the best error message
Expand Down
3 changes: 1 addition & 2 deletions tests/test_context/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
def test_create_context():
"""You can create a context for any object"""

class T:
...
class T: ...

t = T()
tid = id(t)
Expand Down

0 comments on commit ed516f4

Please sign in to comment.