Repository navigation
See the documentation for more information on all of the features listed below.
Security
GHSA-85q3-whq8-63qq: Coconut's Jupyter kernels and language server no longer run code from the directory of the notebook or workspace being opened.
- Jupyter kernels: they were launched with
python -m coconut.icoconut, and Jupyter starts kernels in the notebook's directory. Sincepython -mputs the current directory first onsys.path, acoconutpackage next to a notebook that used a Coconut kernel ran as soon as the notebook was opened, before any cell was run. - Language server:
coconut --lsplikewise started its compile worker withpython -min the workspace. It also started Pyright and mypy withpython -min its mirror of the workspace. So acoconutpackage in the workspace, or apyright.pyormypy.pynext to an open file, ran as soon as a Coconut file was opened.
All of these processes now start without the current directory on sys.path. IPython still adds the notebook's directory back for your code, so cells can import modules next to the notebook. After upgrading, run coconut --jupyter install (or any coconut --jupyter command) to update Coconut kernels installed by older versions, wherever Jupyter finds them. The VS Code extension and Sublime Text package have the same fix in v2.6.0. Thanks to Dilanka Kaushal Hewage for reporting this!
Type Checking
Coconut now recommends Pyright as its type checker. coconut --pyright (and the coconut[pyright] extra) is the suggested way to type check Coconut code, and Coconut's own test suite is now type checked with Pyright (as well as MyPy, which is still fully supported through --mypy and the coconut[mypy] extra). Along the way, a lot of compiled code now type checks more precisely:
- Type parameters on targets below 3.12 now compile to
TypeVar(..., infer_variance=True), so type checkers infer their variance just as they do for Python 3.12's type parameter syntax, rather than treating every type parameter as invariant. MyPy doesn't supportinfer_varianceyet (python/mypy#17811), so the new--no-infer-varianceflag leaves it out, and--mypyimplies it. - Generic
datatypes (including genericcase datavariants) now specialize their fields, so e.g.Box[int](1).xis anintto type checkers rather than an unsolvedT. - Fully support PEP 695 #757:
typestatements are now lazy on every target, as on Python 3.12+, so type aliases can refer to themselves or to names defined later (e.g.type Tree = int | list[Tree]). On targets below 3.12, type checkers see a plainTypeAlias, while at runtime the alias is atyping.TypeAliasTypeon Python 3.12+ and an equivalent object before that (with a lazily evaluated.__value__,.__type_params__, subscripting,|, and pickling), rather than a string. - Set letter literals (
s{...},m{...}), async generators on--target 3.5, tail-recursive functions, and the compiled header now type check correctly with Pyright, and--pyrightadds each checked package's root to Pyright's import paths (as MyPy does). - Type errors on the last line of a tail-recursive function are now reported on that line rather than the line after the function.
- Add automatic type ignore comments #520: the code Coconut generates for its own test suite, which covers every feature, now type checks with no errors under both Pyright and MyPy. Where generated code can't be followed by a type checker, Coconut adds a
type: ignorecomment itself.
Language Features
Allow operator definition syntax to be used for Python operators #675: In a class body, def self + other: now defines __add__, and def -self: defines __neg__, for all of Python's binary, comparison, and unary operators, with every form of method definition (assignment functions, type annotations, match def, and addpattern def). See Defining Python Operators.
Allow custom in-place-operators #729: Custom operators can now be used in augmented assignments, so after operator ~~, x ~~= y means x = x ~~ y. (For a custom operator that's a word, like operator plus, x plus= y used to be a destructuring assignment; write x plus = y, with a space, for that.) See Custom Operators.
Update sections now work on any object, not just data types, namedtuples, dataclasses, objects that define __replace__, and lists, dicts, and tuples: any other object is copied with copy.copy and the new value set on the copy, without mutating the original. Each object along the path is copied the way its own type is, so if obj is a data type and obj.a is an instance of a normal class, obj |> (.a.x = 1) uses ._replace for obj and copy.copy for obj.a.
Compiler and Tooling Features
Coconut doesn't convert doctest docstrings #321: Doctests in Coconut code can now be written in Coconut and run with the new coconut.doctest module, which works like doctest (testmod, testfile, DocTestSuite, DocFileSuite, and python -m coconut.doctest) but compiles each example as Coconut. See coconut.doctest.
Add compiler directives for things like --no-tco and --no-wrap #614: A # coconut: --strict --pure comment at the top of a file turns those options on for just that file, however it's compiled (command line, coconut.api, or the language server). The supported flags are --strict, --pure, --no-tco, --no-wrap-types, and --no-infer-variance, and like [tool.coconut], directives can only turn options on. See File Directives.
Warn when an empty data pattern like Const() can never match a data type with fields #913: Since data patterns match a data type's fields, Const() never matches a Const data type with required fields; Coconut now warns about such patterns (an error with --strict) and suggests class Const() or Const(*_), which check only the type.
Known unreachable code in a function after a return should throw an error on --strict #599: --strict now raises an error on code after a return, raise, break, or continue statement in the same block (which # NOQA suppresses), other than a bare yield after a return, the standard way to make a function a generator. Thanks to @nae3x for their work on this in #904!
The language server (coconut --lsp) can now type check with Pyright: its pyright setting can be true, false, or "auto" (type check whenever Pyright is installed). The coconut_check MCP tool type checks with Pyright by default when it's installed, the CLI's check takes --pyright, and the Claude Code plugin turns Pyright on automatically. The VS Code extension and Sublime Text package (v2.6.0, which also has the security fix above) support it too, through a pyright setting that defaults to "auto". The VS Code extension can also now run cells (marked with # %%), selections, and statements in the Coconut interpreter (#668).
Does the target code of Coconut run under GraalPY? #870: Compiled Coconut code now runs on GraalPy (previously, a GraalPy difference in functools.partial subclasses made every compiled file fail on import), and Coconut's CI now runs its test suite, compiled with CPython, on GraalPy. The Coconut compiler itself also runs on GraalPy.
Xontrib: Allow using cocounut in rc files and @(...) syntax #883: In xonsh, Coconut code now works inside the @(...) Python substitutions of subprocess-mode commands, e.g. ls @(Path(".").glob("*.py") |> map$(.name) |> sorted).
Feat: Marimo support #875: The documentation now covers using Coconut from marimo notebooks, by importing .coco modules or evaluating Coconut expressions with coconut_eval.
Add coconut.builtins as an alias for coconut.__coconut__ #779: Python code can now import Coconut's built-ins from coconut.builtins (e.g. from coconut.builtins import fmap), with full types for type checkers. coconut.__coconut__ still works, and is the same module.
Coconut's website, coconut-lang.org, has been rebuilt, with a playground that compiles and runs Coconut entirely in your browser.
Coconut should compile to a temporary directory when --no-write is passed #314: Tracebacks from code that Coconut runs without writing it to a file (a file run with --run --no-write, -c code, piped-in code, and interpreter input) now show each line of compiled Python that raised, with its #N (line in Coconut source) comment, as they would for a compiled file. Previously, they only showed File "<string>" and a line number. Also, an interpreter input that raises a SyntaxError while running (e.g. from eval) no longer runs twice, and, as with python -c, an uncaught exception in -c or piped-in code now makes Coconut's exit status 1 (previously 0).
The interpreter now ignores brackets and # inside strings when deciding whether an input continues onto more lines (previously, e.g. x = "(" waited for more input), and, as in Python's interpreter, a multi-line statement that doesn't open a block, like one continued by an unclosed string or parenthesis, now ends as soon as it's complete rather than waiting for a blank line. The same string handling applies to the Coconut Jupyter kernel's line continuation in Jupyter console.
Universalize all new exception subclasses #584: Exception classes added in later Python versions, like FileNotFoundError, ModuleNotFoundError, and RecursionError, now work on every Python version: on Pythons that don't have them, they're aliases of the classes they subclass (see Compatible Python Versions). Previously, compiled code that used them failed with a NameError on older Pythons.
Dynamically compile implicit lambdas to the right arity #416: Implicit lambdas now have their own section of the documentation, which explains that (=> expr) takes one optional argument named _, which shadows any _ from an enclosing scope (write (() => _) to use an outer _).
Bugfixes
Fix line number handling by ensuring no suppressed newlines #645: The compiled code for each case of a case block, match expression, and case def, the main statement of a where statement, and the first statement of a data body now get the line numbers of their own source lines (which tracebacks, --line-numbers, and comments like # type: ignore use), rather than those of a nearby line.
Changes introduced in 1.3.1.post0.dev21 break build #402: Built-ins that Coconut doesn't replace, like open, print, and input, are no longer copied into every compiled module on Python 3, so patching them in builtins (e.g. mock.patch("builtins.open")) now affects Coconut code like any other code.
Make addpattern def pickleable #510: Pattern-matching functions with several patterns (match def plus addpattern def) can now be pickled, by reference to their name, like normal functions (previously, pickling them failed with "it's not the same object as ...").
Nested where blocks miscompile with COCONUT_USE_COMPUTATION_GRAPH=FALSE #916: A where block nested inside another where block can now use the outer block's names (previously, b = c where: c = a + 1 with a from the outer block raised a NameError).
process_map (and starmap.using_processes) can now use functions and classes defined in a Coconut file run with --run --no-write (previously, pickling them failed, as did pickling anything else defined in such a file).
Contributors
Thanks to @nae3x for their work on unreachable code detection in #904, and to Dilanka Kaushal Hewage for reporting GHSA-85q3-whq8-63qq.
Generated by Claude Code