Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kw_only with default breaks __attrs_pre_init__ call #1284

Closed
zeevro opened this issue May 15, 2024 · 0 comments · Fixed by #1319
Closed

kw_only with default breaks __attrs_pre_init__ call #1284

zeevro opened this issue May 15, 2024 · 0 comments · Fixed by #1319
Labels

Comments

@zeevro
Copy link

zeevro commented May 15, 2024

The following code raises SyntaxError

from attrs import define, field

@define
class A:
    a: int = field(kw_only=True, default=3)

    def __attrs_pre_init__(self, _):
        pass
Traceback (most recent call last):
  File "/home/user/attrs_pre_init.py", line 4, in <module>
    @define
     ^^^^^^
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_next_gen.py", line 153, in define
    return wrap(maybe_cls)
           ^^^^^^^^^^^^^^^
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_next_gen.py", line 144, in wrap
    return do_it(cls, True)
           ^^^^^^^^^^^^^^^^
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_next_gen.py", line 90, in do_it
    return attrs(
           ^^^^^^
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_make.py", line 1715, in attrs
    return wrap(maybe_cls)
           ^^^^^^^^^^^^^^^
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_make.py", line 1694, in wrap
    builder.add_init()
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_make.py", line 1090, in add_init
    _make_init(
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_make.py", line 2181, in _make_init
    init = _make_method(
           ^^^^^^^^^^^^^
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_make.py", line 345, in _make_method
    _compile_and_eval(script, globs, locs, filename)
  File "/home/user/.venv/lib/python3.12/site-packages/attr/_make.py", line 317, in _compile_and_eval
    bytecode = compile(script, filename, "exec")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<attrs generated init __main__.A>", line 2
    self.__attrs_pre_init__(a=attr_dict['a'].default=a=attr_dict['a'].default)
                                                    ^
SyntaxError: invalid syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants