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

Try using __init_subclass__ #2

Closed
simonw opened this issue Dec 3, 2021 · 1 comment
Closed

Try using __init_subclass__ #2

simonw opened this issue Dec 3, 2021 · 1 comment
Labels

Comments

@simonw
Copy link
Owner

simonw commented Dec 3, 2021

https://twitter.com/dabeaz/status/1466731368956809219 - David Beazley says:

I think 95% of the problems once solved by a metaclass can be solved by __init_subclass__ instead

@simonw simonw added the research label Dec 3, 2021
@simonw
Copy link
Owner Author

simonw commented Dec 3, 2021

Can I use it in place of this?

class AsyncInjectMeta(type):
def __new__(cls, name, bases, attrs):
# Decorate any items that are 'async def' methods
_registry = {}
new_attrs = {"_registry": _registry}
inject_all = "AsyncInjectAll" in (b.__name__ for b in bases)
for key, value in attrs.items():
if inspect.iscoroutinefunction(value) and (
inject_all or getattr(value, "_inject", None)
):
new_attrs[key] = _make_method(value)
_registry[key] = new_attrs[key]
else:
new_attrs[key] = value
# Gather graph for later dependency resolution
graph = {
key: {
p
for p in inspect.signature(method).parameters.keys()
if p != "self" and not p.startswith("_")
}
for key, method in _registry.items()
}
new_attrs["_graph"] = graph
return super().__new__(cls, name, bases, new_attrs)

@simonw simonw closed this as completed in 88b0106 Dec 3, 2021
simonw added a commit that referenced this issue Dec 3, 2021
simonw added a commit that referenced this issue Dec 21, 2021
Refs #1, #2, #3, #5
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

1 participant