mypyc / mypyc

Compile type annotated Python to fast C extensions
1.76k stars 46 forks source link

Document which operations ignore overridden methods #722

Open JukkaL opened 4 years ago

JukkaL commented 4 years ago

Currently we don't honor overrides for certain operations on primitive types. If a subclass of list overrides __getitem__, for example, we still use list.__getitem__ for instances of the subclass if the static type is list. The motivation is improving performance of some of the most common operations.

We don't have a detailed policy about this, and it's often unclear when implementing new primitives what to do about subclasses. I propose that we should restrict ignoring overrides to only few specific operations. It would also be to good to document this clearly and have a term that we can use to refer to these operations.

We don't support overrides for any value types, including int and fixed-width tuples. This is something we can't change easily, since we actually convert values at runtime and lose the original type. I take this as a given.

I think that additionally at least these list operations can ignore overrides:

list.append is another candidate, though for many uses of append we could use static analysis to infer that the concrete type is always list (unlike the operations above).

In particular, I think that dict operations and variable-length tuple operations should always support overrides.

Discussion:

Thoughts?

ilevkivskyi commented 4 years ago

I am not sure we need any general rules here, and can rather decide on case by case basis (like e.g. for dictionary iterators we decided to handle subclasses safely). However, ideally all deviations from CPython behavior should be clearly documented.

JukkaL commented 4 years ago

I prefer having the general rule for several reasons:

The same arguments arguably apply to special casing builtin functions, to a certain extent. However, monkey patching builtin functions seem very rare and a questionable practice, whereas subclassing builtin classes happens a lot, and even the implementation of mypy does this.

JukkaL commented 4 years ago

However, monkey patching builtin functions seem very rare...

Besides, monkey patching already much restricted, so users need to learn about monkey patching in any case when starting to use mypyc.

JukkaL commented 4 years ago

@msullivan Do you have any thoughts about this proposal?