-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Support various number-related dunders #10679
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
Conversation
Unify the interfaces of emitting unbox and cast operations. Also increase abstraction level by adding classes that describe failure handling, instead of passing C fragments used to handle errors.
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.
Great!
mypyc/codegen/emitwrapper.py
Outdated
wrapper_name = gen.wrapper_name() | ||
|
||
gen.emit_header() | ||
if fn.name not in reverse_op_methods and fn.name in reverse_op_methods.values(): |
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.
It doesn't really matter but let's make a set in operators with the results of reverse_op_methods.values()
so we don't need to scan
if typ.is_unboxed: | ||
# Borrow when unboxing to avoid reference count manipulation. | ||
emitter.emit_unbox('obj_{}'.format(name), 'arg_{}'.format(name), typ, | ||
error_code, declare_dest=True, borrow=True, optional=optional) | ||
emitter.emit_unbox('obj_{}'.format(name), |
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 am not asking for a change, but just making a remark: I've really come around to "black-style" for multi-line function calls, where you put the first argument on a new line, indented four spaces
optional=optional) | ||
|
||
|
||
class WrapperGenerator: |
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 like this
This adds support for these unary dunders:
__neg__
__invert__
__int__
__float__
Also add support for binary, reversible dunders, such as
__add__
and__radd__
.Finally, add support for in-place operator dunders such as
__iadd__
.The semantics of the binary dunders don't always match Python semantics, but
many common use cases should work.
There is one significant difference from Python that is not easy to remove: if a
forward dunder method is called with an incompatible argument, it's treated the
same as if it returned
NotImplemented
. This is necessary since the body ofthe method is never reached on incompatible argument type and there is no
way to explicitly return
NotImplemented
. However, it's still recommended thatthe body returns
NotImplemented
as expected for Python compatibility.If a dunder returns
NotImplemented
and has a type annotation, the returntype should be annotated as
Union[T, Any]
, whereT
is the return valuewhen
NotImplemented
is not returned.Work on mypyc/mypyc#839.