-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[mypyc] Dict true test #10333
[mypyc] Dict true test #10333
Conversation
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.
Hmm the original implementation may actually be buggy. We should allow subclasses to override __len__
, for consistency with other dict operations, and this means that we should have a type guard. This probably makes it too complex to be done directly in IR, and instead we could use a C helper that uses PyDict_GET_SIZE
behind a type guard. Sorry about that!
This should work similar to CPython and print 'here':
class D(dict):
def __len__(self) -> int:
return 1
if D():
print('here')
Got it. I will start a new branch from beginning later. |
I tried adding this: Py_ssize_t CPyDict_Size(PyObject *dict) {
if (PyDict_CheckExact(dict)) {
return PyDict_Size(dict);
}
return PyObject_Size(dict);
} class MyDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
def __len__(self):
return 1
def test_dict_subclass_to_bool() -> None:
d = MyDict()
if d:
print("true") However
|
Hmm I wonder if this is a bug in But can you add a new issue to track the issue of overriding
No, the cast does not modify the operand. |
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.
Thanks for the updates! This looks good, and it made a quick benchmark I wrote about 50% faster, which is a nice speedup.
Description
Closes mypyc/mypyc#768
builtin_len(dict)
.cpython/dictobject.h
call_c
withLoadMem
and addPyDictObject
. Related issue: Add specialization for strings in builtin_len mypyc/mypyc#835Test Plan
An IR test and a functional test.
Similar to 1e96f1d