Skip to content

Commit 1100800

Browse files
authored
[mypyc] Document free threading and other doc updates (#21494)
Free threading support and limitations weren't documented at all. Some features that we currently support were still documented as unsupported. This includes some dunders and numeric use cases. We now support simple numeric use cases via `vec[float]`.
1 parent 1bbd6da commit 1100800

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

mypyc/doc/differences_from_python.rst

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,45 @@ When run as interpreted, the first example will execute slower due to
260260
the extra namespace lookups. In interpreted code final attributes can
261261
also be modified.
262262

263+
.. _free-threading:
264+
265+
Free threading
266+
--------------
267+
268+
Mypyc has basic support for free threading, but it doesn't provide the
269+
same memory safety guarantees as Python in compiled modules, since
270+
in current Python versions this would cause an unacceptable performance
271+
impact.
272+
273+
The exact details of the memory safety in the presence of data races
274+
are likely to evolve in the future. Currently, compiled code must
275+
ensure that proper synchronization is used to prevent data races. In
276+
particular, these operations require explicit synchronization, such as
277+
via ``threading.Lock``, if there is a possibility of data races
278+
(the list is not exhaustive):
279+
280+
* Reads or writes of non-final instance data attributes of native
281+
classes.
282+
* List item access or iteration using a ``list`` static type (using
283+
``Sequence`` or ``MutableSequence`` as the type ensure correct
284+
implicit synchronization).
285+
* Dict item access using a static ``dict`` type (using ``Mapping``
286+
or ``MutableMapping`` ensures correct implicit synchronization).
287+
288+
As libraries often won't be able to control the concurrent access by
289+
user code, we recommend that modules document that multi-threaded
290+
access is only supported via public interfaces that ensure correct
291+
synchronization. Marking attributes as internal using an underscore
292+
attribute prefix is another possibility, but this is not enforced at
293+
runtime. Another option is to document that multithreaded access is
294+
not supported, or that particular objects should not be used from
295+
multiple threads concurrently.
296+
297+
It's always safe to perform read-only operations concurrently. Using
298+
objects with final attributes and tuple objects can help prevent
299+
race conditions without introducing extra overhead from explicit
300+
synchronization operations.
301+
263302
Unsupported features
264303
--------------------
265304

@@ -284,10 +323,8 @@ Dunder methods
284323
Native classes **cannot** use these dunders. If defined, they will not
285324
work as expected.
286325

287-
* ``__del__``
288326
* ``__index__``
289-
* ``__getattr__``, ``__getattribute__``
290-
* ``__setattr__``
327+
* ``__getattribute__``
291328
* ``__delattr__``
292329

293330
Generator expressions
@@ -318,7 +355,6 @@ non-exhaustive list of what won't work:
318355
- Compiled methods aren't considered methods by ``inspect.ismethod``
319356
- ``inspect.signature`` chokes on compiled functions with default arguments that
320357
are not simple literals
321-
- ``inspect.iscoroutinefunction`` and ``asyncio.iscoroutinefunction`` will always return False for compiled functions, even those defined with `async def`
322358

323359
Profiling hooks and tracing
324360
***************************

mypyc/doc/introduction.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ also as normal, interpreted Python modules.
2323
Existing code with type annotations is often **1.5x to 5x** faster
2424
when compiled. Code tuned for mypyc can be **5x to 10x** faster.
2525

26-
Mypyc currently aims to speed up non-numeric code, such as server
27-
applications. Mypyc is also used to compile itself (and mypy).
26+
Mypyc aims to be effective in many common use cases, including
27+
server applications. Mypyc is also used to compile itself (and mypy).
2828

2929
Why mypyc?
3030
----------
@@ -54,10 +54,11 @@ requires only minor changes to compile using mypyc.
5454
normal Python code. You can use interpreted Python during development,
5555
with familiar and fast workflows.
5656

57-
**Runtime type safety.** Mypyc protects you from segfaults and memory
58-
corruption. Any unexpected runtime type safety violation is a bug in
59-
mypyc. Runtime values are checked against type annotations. (Without
60-
mypyc, type annotations are ignored at runtime.)
57+
**Runtime type safety.** Mypyc partially protects you from segfaults and
58+
memory corruption (see :ref:`free-threading` for limitations). Any
59+
unexpected runtime type safety violation is a bug in mypyc. Runtime
60+
values are checked against type annotations. (Without mypyc, type
61+
annotations are ignored at runtime.)
6162

6263
**Find errors statically.** Mypyc uses mypy for static type checking
6364
that helps catch many bugs.
@@ -110,8 +111,7 @@ differently, however:
110111
* Mypyc performs strict enforcement of type annotations at runtime,
111112
resulting in better runtime type safety and easier debugging.
112113

113-
Unlike Cython, mypyc doesn't directly support interfacing with C libraries
114-
or speeding up numeric code.
114+
Unlike Cython, mypyc doesn't directly support interfacing with C libraries.
115115

116116
How does it work
117117
----------------

0 commit comments

Comments
 (0)