beeware / batavia

A JavaScript implementation of the Python virtual machine.
http://pybee.org/batavia
Other
1.39k stars 424 forks source link

Represent operators with a class. #779

Closed martica closed 5 years ago

martica commented 5 years ago

Each operator can have a fallback, inplace falling back to normal, normal falling back to right-hand with arguments swapped.

Magic Methods now return NotImplemented instead of throwing an unsupported arguments exception, leaving the error handling to the operator class. This mimics the behaviour of the CPython implementations and simplified fall back.

Future dreams here are the hope that this will unlock implementing a more general __dir__ that uses the methods defined directly instead of hard-coding the list.

PR Checklist:

martica commented 5 years ago

Adding tests for the magic methods directly to ensure that they are only defined when they should be and return NotImplemented when expected was a great idea. This has added a few more tests to the suite, but they should be useful. I have tried to get the majority of them to pass, but there are still about 4% to tidy up. Mostly in the right-hand operator implementations.

I do have one regression where the exception message has changed in some cases when multiplying a sequence by a non-int. It's the right exception type still, but the message is more generic than before. I've marked these test cases with a note about this. I'll probably have to dig into cpython to see how they accomplish the current state of those messages.

martica commented 5 years ago

As we discussed, I've updated the magic method tests to only run one tests for methods that shouldn't be defined, but run a full suite for the ones that should be defined to ensure that each type is handled appropriately. This adds a more modest 2512 tests instead of the previous 9792 tests.