-
Notifications
You must be signed in to change notification settings - Fork 829
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
Define abstract Mutation.mutate() method [type-hints] #1393
base: master
Are you sure you want to change the base?
Conversation
I will be glad to perform more similar refactoring in the project if current change will be considered reasonable. |
mutate = getattr(cls, "mutate", None) | ||
assert mutate, "All mutations must define a mutate method in it" | ||
resolver = get_unbound_function(mutate) | ||
resolver = get_unbound_function(cls.mutate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that with current implementation it's really needed to call get_unbound_function()
instead of just referring cls.mutate
. Should I check it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your approach is really interesting due to IDE warning. The only difference between your implementations and previous one is the fact that your code only raises exception in runtime when the function mutate
is called.
The older one would raise with class __new__
and the error is raises after the module is loaded.
For example, the following test fails with your code:
def test_mutation_raises_exception_if_no_mutate():
with raises(AssertionError) as excinfo:
class MyMutation(Mutation):
pass
assert "All mutations must define a mutate method in it" == str(excinfo.value)
I think it's a break change if your code is merged. So, maybe there is another path to follow in order to validate if it's overwritten and also raises the the same exception.
Maybe a workaround would be to check if the method is implemented:
def add_implemented_flag(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper._is_implemented = False
return wrapper
class Mutation(ObjectType):
@classmethod
@add_implemented_flag
def mutate(cls, parent, info, **kwargs):
raise NotImplementedError("All mutations must define a mutate method in it")
@classmethod
def __init_subclass_with_meta__(
cls,
interfaces=(),
resolver=None,
output=None,
arguments=None,
_meta=None,
**options,
):
...
if not resolver:
is_defined = getattr(cls.mutate, '_is_implemented', True)
assert is_defined, "All mutations must define a mutate method in it"
resolver = get_unbound_function(cls.mutate)
(but I also didn't like my solution lol)
Sorry for the late reply. I have a lot of thoughts on the topic... (Click on every section to expand)
.mutate() method. |
Add default implementation of Mutation.mutate() that raises NotImplementedError. This makes code more clear and also improves work of auto-completion tools (e. g. in IDE). They usually guess if user is overriding a class method and suggest to complete method name/arguments.
Add default implementation of Mutation.mutate() that raises NotImplementedError.
This makes code more clear and also improves work of auto-completion tools (e. g. in IDE). They usually guess if user is overriding a class method and suggest to complete method name/arguments.
Not using python's abc.ABC because of possible metaclass conflicts.