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

Passing arguments to __attrs_init_subclass__ #1335

Open
tartopohm opened this issue Aug 13, 2024 · 2 comments
Open

Passing arguments to __attrs_init_subclass__ #1335

tartopohm opened this issue Aug 13, 2024 · 2 comments
Labels

Comments

@tartopohm
Copy link

Hello, thanks for the great work!

My question/issue is related to __attrs_init_subclass__ that was recently added.
PEP 487 allows for passing arguments (not sure whether that's the proper name) at class definition.
The use case, for me, would be to have some sort of abstract class attributes that should be defined by concrete classes.

To make it short, reusing the example from the docs, this works

class Base:
   @classmethod
   def __init_subclass__(cls, foo):
       cls.foo = foo
       print(f"Base has been subclassed by attrs {cls} and foo={cls.foo}.")


class Derived(Base, foo=42):
   pass
>>> Base has been subclassed by attrs <class '__main__.Derived'> and foo=42.

while

import attrs


class Base:
   @classmethod
   def __attrs_init_subclass__(cls, foo):
       cls.foo = foo
       print(f"Base has been subclassed by attrs {cls} and foo={cls.foo}.")


@attrs.define
class Derived(Base, foo=12):
   pass

does not, throwing TypeError: Derived.__init_subclass__() takes no keyword arguments.

Would there be any workaround?

@hynek
Copy link
Member

hynek commented Aug 13, 2024

Yeah I was considering to add it, but 24.1 was 8 months overdue so this fell off the wagon, since I wasn’t sure what’s involved.

@hynek hynek added the Feature label Aug 13, 2024
@tartopohm
Copy link
Author

Fair enough. I am completely swamped at the moment, so no promises, but I'll try to look into it if I can find the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants