Skip to content

Commit

Permalink
Make __() optional for standard constructors (#7)
Browse files Browse the repository at this point in the history
fixes #6

* [late] allow omitting `__()` for basic constructors
* [readme] mention Optional[]
* [late] fix list of immutable types
* [latebinding] wrap structured types because members may be mutable
  • Loading branch information
apalala authored Nov 17, 2023
1 parent 0eedd18 commit 1fa9678
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[
![lincense](https://img.shields.io/github/license/neogeny/Late)
![license](https://img.shields.io/github/license/neogeny/Late)
](https://www.gnu.org/licenses/lgpl-3.0.html)
[
![version](https://img.shields.io/pypi/pyversions/late.svg)
](https://www.python.org/downloads/)
[
![fury](https://badge.fury.io/py/Late.svg)
](https://pypi.org/project/Late/)
![downloada](https://img.shields.io/pypi/dm/Late.svg)
![downloads](https://img.shields.io/pypi/dm/Late.svg)
[
![tests](https://github.com/neogeny/late/actions/workflows/default.yml/badge.svg)
](https://github.com/neogeny/late/actions/workflows/default.yml)

# 包 Late
# 包 Late 1.3.0b1
Late binding for Python default arguments


Expand Down Expand Up @@ -66,7 +66,13 @@ in a way so that type checkers do not complain about using ``None`` as the defau
def f(x: list[Any] | None = None) -> list[Any]:
```

Another problem with the above declaration is that calling ``f(None)`` passes type checking,
or:

```python
def f(x: Optional[list[Any]] = None) -> list[Any]:
```

Another problem with the above declarations is that calling ``f(None)`` passes type checking,
when that's probably not the preferred situation.


Expand All @@ -90,10 +96,16 @@ assert f() == [1]

```

For constructors for basic structured types, the ``__()`` call may be omitted:

```python
@latebinding
def f(x: list[Any] = []) -> list[Any]:
```

### Working with classes

**包 Late** also works with classes and `dataclass`. The ``@latebinding`` decorator
**包 Late** also works with classes and ``dataclass``. The ``@latebinding`` decorator
must be the outer one:

```python
Expand Down
27 changes: 16 additions & 11 deletions late/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Callable, Iterator
from typing import Any, NamedTuple, TypeVar

__all__ = ['latebinding', 'late', '__']
__all__ = ['latebinding']


_R = TypeVar('_R')
Expand All @@ -17,7 +17,7 @@ class _LateBound(NamedTuple):


def late(o: _T | Iterator[_V] | Callable[[], _R]) -> _T | _V | _R:
if isinstance(o, int | float | str | bytes | bool | tuple | bytearray | frozenset):
if isinstance(o, int | float | bool | str | bytes):
return o # type: ignore

return _LateBound(actual=o) # type: ignore
Expand All @@ -27,19 +27,24 @@ def late(o: _T | Iterator[_V] | Callable[[], _R]) -> _T | _V | _R:
= late


def _lateargs(func: Callable, **kwargs) -> dict[str, Any]:
def _resolve_value(value):
if isinstance(value, _LateBound):
value = value.actual
if isinstance(value, int | float | bool | str | bytes):
return value
if isinstance(value, Iterator):
return next(value)
if inspect.isfunction(value):
return value()
return copy.deepcopy(value)


def resolve_default(value):
if isinstance(value, Iterator):
return next(value)
if inspect.isfunction(value):
return value()
return copy.deepcopy(value)
def _lateargs(func: Callable, **kwargs) -> dict[str, Any]:

lateargs = {
name: resolve_default(param.default.actual)
name: _resolve_value(param.default)
for name, param in inspect.signature(func).parameters.items()
if name not in kwargs and isinstance(param.default, _LateBound)
if name not in kwargs and param.default != inspect._empty
}
return {**kwargs, **lateargs}

Expand Down
2 changes: 1 addition & 1 deletion late/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.3.0b1'
__version__ = '1.2.1'
4 changes: 2 additions & 2 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ select = [
# 'ERA', # commented out code
]
ignore = [
'B008',
'B006', 'B008',
'E501', 'E741', 'E402',
'S101', 'S102', 'S105', 'S301', 'S311',
'C408',
Expand All @@ -23,7 +23,7 @@ ignore = [
'PLR0913', 'PLR2004', 'PLR0904', 'PLR0915',
'PLW0603', 'PLW2901', 'PLW3201',
'RET505',
'RUF009',
'RUF008', 'RUF009',
]
exclude = []

Expand Down
2 changes: 1 addition & 1 deletion test/complex_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def test_recursion():
@latebinding
def f(x: list[list[Any]] = __([[]])) -> list[list[Any]]:
def f(x: list[list[Any]] = [[]]) -> list[list[Any]]:
x[0].append(1)
return x

Expand Down
8 changes: 4 additions & 4 deletions test/simple_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import inspect
from typing import Any

from late import __, _LateBound, latebinding,
from late import latebinding,


def test_with_list():

@latebinding
def f(x: list[Any] = __([])) -> list[Any]:
def f(x: list[Any] = []) -> list[Any]:
x.append(1)
return x

Expand All @@ -18,13 +18,13 @@ def f(x: list[Any] = __([])) -> list[Any]:

def test_immutable():
@latebinding
def f(x: frozenset[Any] = __(frozenset()), y: set = __(set())):
def f(x: frozenset[Any] = frozenset(), y: set = set()):
return

param = inspect.signature(f).parameters['x']
assert type(param.default) is frozenset
param = inspect.signature(f).parameters['y']
assert type(param.default) is _LateBound
assert type(param.default) is set


def test_kanji():
Expand Down

0 comments on commit 1fa9678

Please sign in to comment.