Skip to content

Commit 92602c5

Browse files
ichard26JukkaLAlexWaygood
authored
[mypyc] Document more unsupported features & update supported features (#15524)
Towards mypyc/mypyc#978. --------- Co-authored-by: Jukka Lehtosalo <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 21beadc commit 92602c5

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

mypyc/doc/differences_from_python.rst

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,27 @@ used in compiled code, or there are some limitations. You can
268268
partially work around some of these limitations by running your code
269269
in interpreted mode.
270270

271-
Operator overloading
272-
********************
271+
Nested classes
272+
**************
273273

274-
Native classes can only use these dunder methods to override operators:
274+
Nested classes are not supported.
275275

276-
* ``__eq__``
277-
* ``__ne__``
278-
* ``__getitem__``
279-
* ``__setitem__``
276+
Conditional functions or classes
277+
********************************
280278

281-
.. note::
279+
Function and class definitions guarded by an if-statement are not supported.
280+
281+
Dunder methods
282+
**************
282283

283-
This limitation will be lifted in the future.
284+
Native classes **cannot** use these dunders. If defined, they will not
285+
work as expected.
286+
287+
* ``__del__``
288+
* ``__index__``
289+
* ``__getattr__``, ``__getattribute__``
290+
* ``__setattr__``
291+
* ``__delattr__``
284292

285293
Generator expressions
286294
*********************
@@ -299,10 +307,16 @@ Descriptors
299307
Native classes can't contain arbitrary descriptors. Properties, static
300308
methods and class methods are supported.
301309

302-
Stack introspection
303-
*******************
310+
Introspection
311+
*************
312+
313+
Various methods of introspection may break by using mypyc. Here's an
314+
non-exhaustive list of what won't work:
304315

305-
Frames of compiled functions can't be inspected using ``inspect``.
316+
- Instance ``__annotations__`` is usually not kept
317+
- Frames of compiled functions can't be inspected using ``inspect``
318+
- Compiled methods aren't considered methods by ``inspect.ismethod``
319+
- ``inspect.signature`` chokes on compiled functions
306320

307321
Profiling hooks and tracing
308322
***************************

mypyc/doc/native_classes.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ classes:
6363
* ``IndexError``
6464
* ``LookupError``
6565
* ``UserWarning``
66+
* ``typing.NamedTuple``
67+
* ``enum.Enum``
6668

6769
By default, a non-native class can't inherit a native class, and you
6870
can't inherit from a native class outside the compilation unit that
@@ -104,6 +106,11 @@ through an instance. Example::
104106
print(o.cv) # OK (2)
105107
o.cv = 3 # Error!
106108

109+
.. tip::
110+
111+
Constant class variables can be declared using ``typing.Final`` or
112+
``typing.Final[<type>]``.
113+
107114
Generic native classes
108115
----------------------
109116

@@ -150,9 +157,10 @@ decorators can be used with native classes, however:
150157
* ``mypy_extensions.trait`` (for defining :ref:`trait types <trait-types>`)
151158
* ``mypy_extensions.mypyc_attr`` (see :ref:`above <inheritance>`)
152159
* ``dataclasses.dataclass``
160+
* ``@attr.s(auto_attribs=True)``
153161

154-
Dataclasses have partial native support, and they aren't as efficient
155-
as pure native classes.
162+
Dataclasses and attrs classes have partial native support, and they aren't as
163+
efficient as pure native classes.
156164

157165
.. note::
158166

mypyc/doc/using_type_annotations.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ Traits have some special properties:
194194
* You shouldn't create instances of traits (though mypyc does not
195195
prevent it yet).
196196

197-
* Traits can subclass other traits, but they can't subclass non-trait
198-
classes (other than ``object``).
197+
* Traits can subclass other traits or native classes, but the MRO must be
198+
linear (just like with native classes).
199199

200200
* Accessing methods or attributes through a trait type is somewhat
201201
less efficient than through a native class type, but this is much

0 commit comments

Comments
 (0)