[Python] Changed Decorate attribute to take importFrom parameter instead of auto-importing #4290
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR improves the Py.Decorate attribute to emit decorator strings verbatim and adds explicit import control via the
importFromparameter. This fixes issues with decorator patterns since the previous auto-import logic could not be used for FastAPI endpoint decorators.Key changes:
New API:
This enables proper integration with FastAPI, Pydantic, and other Python frameworks that use local variable decorators or decorator factories.
Note: We need to use
importFromparameter instead of using[<Import(..)>]´ since theImport` attribute is not allowed to have multiple imports on the same element. We need to be able to add multiple decorators to the same elementAlternative would be to change the Fable AST. Named Attribute Parameters in Fable AST. Currently,
Fable.Attributeonly exposesConstructorArgs: obj list, which means we rely on constructor overloads and positional arguments. This works but can be awkward for attributes with multiple optional parameters.For example, we'd ideally write:
[<Py.Decorate("lru_cache", importFrom="functools", parameters="maxsize=128")>]
And have the compiler recognize
importFromandparametersas named arguments rather than relying on constructor position matching.Proposed change to Fable.Attribute:
This would require changes to: