attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Add positional-only parameters

Open Dr-ZeeD opened this issue 5 years ago • 3 comments

With PEP 570, positional-only parameters were introduced. Similarly to keyword-only parameters, I would like to see these as an option for attrs. One thing that makes them "special" is that the __repr__ method would behave differently for positional-only parameters than it would for regular positional parameters.

An example I would use in my libraries:

from attr import attrib, attrs


@attrs(auto_attribs=True)
class Point:
    """A three-dimensional point."""
    _data: Tuple[float, float, float] = attrib(positional_only=True)


print(Point((0, 1, 2)))

The output would then be

Point((0, 1, 2))

instead of

Point(_data=(0, 1, 2))

If this were a desirable feature, would this be a good "first pull request" I could follow up on?

Dr-ZeeD avatar Apr 26 '20 09:04 Dr-ZeeD

attrs respects attribute definition order. what would the behaviour of something like this be?

@attr.define
class X:
    a = attr.field(kw_only=True)
    b = attr.field(positional_only=True)  # or pos_only

transparently reshuffled __init__() args? rejected upfront as an invalid definition?

wbolster avatar Mar 21 '22 17:03 wbolster

@wbolster Since the ordinary behaviour with positional arguments is silent reshuffling, then this would the same I think.

NeilGirdhar avatar Mar 22 '22 03:03 NeilGirdhar

I was also just searching if the possibility of positional-only arguments exists. Has there been any progress on this topic?

AdrianSosic avatar Jan 16 '24 09:01 AdrianSosic