Skip to content

Commit d7cfa03

Browse files
dbrattliclaude
andcommitted
Add DecorateTemplate section for library authors
Document Py.DecorateTemplate for creating reusable custom decorator attributes, with a web framework route decorator as an example. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d3dba62 commit d7cfa03

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

docs/docs/python/features.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,31 @@ type MyClass() =
539539
member val Value: int = 0 with get, set
540540
```
541541

542+
### DecorateTemplate for Library Authors
543+
544+
When building a library, you can create custom decorator attributes that users
545+
can apply without knowing the underlying Python syntax. Use `Py.DecorateTemplate`:
546+
547+
```fs
548+
/// Custom route decorator for a web framework
549+
[<Erase>]
550+
[<Py.DecorateTemplate("app.get('{0}')", "fastapi")>]
551+
type GetRouteAttribute(path: string) =
552+
inherit System.Attribute()
553+
```
554+
555+
Now users of your library can simply write:
556+
557+
```fs
558+
[<GetRoute("/users")>]
559+
static member get_users() = ...
560+
// Generates: @app.get('/users')
561+
```
562+
563+
The template string uses `{0}`, `{1}`, etc. as placeholders for the attribute's
564+
constructor arguments. The `[<Erase>]` attribute prevents the attribute type
565+
from being emitted to Python.
566+
542567
## `[<Erase>]`
543568

544569
### Erased unions

0 commit comments

Comments
 (0)