Skip to content

Commit 2adef37

Browse files
dbrattliclaude
andcommitted
Fix Python docs: correct Py.Decorator to Py.Decorate and fix examples
- Rename Py.Decorator to Py.Decorate (the actual API name) - Replace record example with class example (records already compile to @DataClass automatically, and Py.Decorate cannot be used on records) - Remove function decorator example (Fable doesn't support attributes on functions, only on types) - Add single-argument form example for local decorators - Move Class Attributes section before Python Decorators since the decorator example references Py.ClassAttributes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5d38a6d commit 2adef37

1 file changed

Lines changed: 28 additions & 34 deletions

File tree

docs/docs/python/features.md

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -458,73 +458,67 @@ def add(a: int32, b: int32) -> Any:
458458

459459
`Py.python` executes as statements, so use `return` keyword to return values.
460460

461-
## Python Decorators
461+
## Class Attributes
462462

463463
<p class="tag is-info is-medium">
464464
Added in v5.0.0-alpha
465465
</p>
466466

467-
`Py.Decorator` allows you to apply Python decorators to classes and functions.
467+
`Py.ClassAttributes` controls how class members are generated in Python.
468468

469469
```fs
470470
open Fable.Core
471471
472-
[<Py.Decorator("dataclasses.dataclass")>]
473-
type User =
474-
{ Name: string
475-
Age: int }
472+
[<Py.ClassAttributes(Py.ClassAttributeStyle.Attributes)>]
473+
type Config() =
474+
member val Name = "default" with get, set
475+
member val Port = 8080 with get, set
476476
```
477477

478478
generates:
479479

480480
```py
481-
@dataclasses.dataclass
482-
class User:
483-
name: str
484-
age: int32
485-
```
486-
487-
You can also pass parameters to decorators:
488-
489-
```fs
490-
[<Py.Decorator("functools.lru_cache", "maxsize=128")>]
491-
let expensiveFunction x = x * 2
481+
class Config:
482+
name: str = "default"
483+
port: int = 8080
492484
```
493485

494-
generates:
495-
496-
```py
497-
@functools.lru_cache(maxsize=128)
498-
def expensive_function(x):
499-
return x * 2
500-
```
486+
Without `ClassAttributes`, members would be generated as properties with instance backing.
501487

502-
## Class Attributes
488+
## Python Decorators
503489

504490
<p class="tag is-info is-medium">
505491
Added in v5.0.0-alpha
506492
</p>
507493

508-
`Py.ClassAttributes` controls how class members are generated in Python.
494+
`Py.Decorate` allows you to apply Python decorators to types.
509495

510496
```fs
511497
open Fable.Core
512498
513-
[<Py.ClassAttributes(Py.ClassAttributeStyle.Attributes)>]
514-
type Config() =
515-
member val Name = "default" with get, set
516-
member val Port = 8080 with get, set
499+
[<Py.Decorate("dataclass", "dataclasses")>]
500+
[<Py.ClassAttributes(Py.ClassAttributeStyle.Attributes, false)>]
501+
type DecoratedUser() =
502+
member val Name: string = "" with get, set
503+
member val Age: int = 0 with get, set
517504
```
518505

519506
generates:
520507

521508
```py
522-
class Config:
523-
name: str = "default"
524-
port: int = 8080
509+
@dataclass
510+
class DecoratedUser:
511+
Age: int32 = int32.ZERO
512+
Name: str = ""
525513
```
526514

527-
Without `ClassAttributes`, members would be generated as properties with instance backing.
515+
The single argument form can be used for local decorators where you don't need to import anything:
516+
517+
```fs
518+
[<Py.Decorate("my_decorator")>]
519+
type MyClass() =
520+
member val Value: int = 0 with get, set
521+
```
528522

529523
## `[<Erase>]`
530524

0 commit comments

Comments
 (0)