Class PythonParameterDefaultValueProvider
- All Implemented Interfaces:
Ordered, ParameterDefaultValueProvider
A Python default is evaluated by the interpreter, so in general it is applied by the stub
constructor rather than by the caller. A literal is the exception: host: str = "localhost"
carries its value into ArgumentDef#defaultValue() at compile time, so it can be passed
explicitly and is indistinguishable from letting Python apply it.
Anything else — a mutable literal, a default_factory (an
DefaultFactoryDef), a call, a name — yields
Optional.empty(), leaving those parameters to the stub's own defaulting. In particular a
fresh mutable default must not be materialised here, as Python evaluates a mutable default once
at definition time and shares it between calls.
- Since:
- 5.2.0
- Author:
- graemerocher
-
Field Summary
Fields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionOptional<io.micronaut.sourcegen.model.ExpressionDef> defaultValueExpression(ParameterElement parameter, @Nullable io.micronaut.sourcegen.model.ExpressionDef target) Supplies an expression evaluating to the parameter's declared default value.booleansupports(ParameterElement parameter) Whether this provider can supply default values for the given parameter, which is typically a test of the element's language specific implementation type.
-
Constructor Details
-
PythonParameterDefaultValueProvider
public PythonParameterDefaultValueProvider()
-
-
Method Details
-
supports
Description copied from interface:ParameterDefaultValueProviderWhether this provider can supply default values for the given parameter, which is typically a test of the element's language specific implementation type.- Specified by:
supportsin interfaceParameterDefaultValueProvider- Parameters:
parameter- The parameter- Returns:
- True if
ParameterDefaultValueProvider.defaultValueExpression(ParameterElement, ExpressionDef)should be consulted for this parameter
-
defaultValueExpression
public Optional<io.micronaut.sourcegen.model.ExpressionDef> defaultValueExpression(ParameterElement parameter, @Nullable io.micronaut.sourcegen.model.ExpressionDef target) Description copied from interface:ParameterDefaultValueProviderSupplies an expression evaluating to the parameter's declared default value.The returned expression must be assignable to the erasure of
ParameterElement.getType()and must be safe to evaluate at most once per invocation, in argument position. Implementations should be side effect free, as this method may be called more than once for the same parameter.- Specified by:
defaultValueExpressionin interfaceParameterDefaultValueProvider- Parameters:
parameter- The parameter, for whichParameterElement.hasDefault()is truetarget- The receiver of the call the parameter belongs to, ornullwhen the default is obtained without one — as for a constructor, or where the language emits a static accessor- Returns:
- The expression, or
Optional.empty()if the default cannot be materialised at the call site, in which case the caller falls back to the default value of the parameter's type
-