dry-python / dry-python/returns

Currying support for generic attrs classes

Đang mở
#1,100 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
4.4k
Fork
155
Merge trung bình
2 giờ 27 phút
Pull request đã merge (30 ngày)
22

Mô tả

# Bug report

I tried out returns, because I was looking for typesafe curry implementation, but I hit a problem with my Generic / Protocol implementing attrs classes.

## What's wrong
```python
from typing import TypeVar, Generic

from attr import frozen
from returns.curry import curry

T = TypeVar("T")

@curry
@frozen(kw_only=True)
class Pair(Generic[T]):
_a: T
_b: T

p = Pair(b=1)(a=2)
print(p)

```
```
TypeError: __init__() missing 1 required keyword-only argument: 'a'
```

## How is that should be

The above code should simply print

`Pair(_a=2, _b=1)`

This seems to be due to https://github.com/python-attrs/attrs/issues/374

A fix I hacked together could be something like this.

```python
def curry(function: Callable[..., _ReturnType]) -> Callable[..., _ReturnType]:
import inspect
if inspect.isclass(function):
init_signature = Signature.from_callable(function.__init__)
init_params_without_self = tuple(init_signature.parameters.values())[1:]
argspec = init_signature.replace(parameters=init_params_without_self).bind_partial()
else:
argspec = Signature.from_callable(function).bind_partial()

def decorator(*args, **kwargs):
return _eager_curry(function, argspec, args, kwargs)
return wraps(function)(decorator)
```

If you are interested in support this admittedly special use case, I could submit a PR.

## System information

- `python` version: 3.7
- `returns` version: 0.17.0
- `mypy` version: 0.910

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.