Skip to content

[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

Merged
merged 30 commits into from
Jun 22, 2021
Merged

[mypyc] Support various number-related dunders #10679

merged 30 commits into from
Jun 22, 2021

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 20, 2021

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 of
the method is never reached on incompatible argument type and there is no
way to explicitly return NotImplemented. However, it's still recommended that
the body returns NotImplemented as expected for Python compatibility.

If a dunder returns NotImplemented and has a type annotation, the return
type should be annotated as Union[T, Any], where T is the return value
when NotImplemented is not returned.

Work on mypyc/mypyc#839.

@JukkaL JukkaL requested a review from msullivan June 20, 2021 13:25
Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

wrapper_name = gen.wrapper_name()

gen.emit_header()
if fn.name not in reverse_op_methods and fn.name in reverse_op_methods.values():
Copy link
Collaborator

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),
Copy link
Collaborator

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this

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

Successfully merging this pull request may close these issues.

2 participants