diff options
author | laurentlb <laurentlb@google.com> | 2017-05-04 15:06:14 +0200 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2017-05-04 17:01:00 +0200 |
commit | 52637bedbd0ca9157d39c012f027449d19d4e012 (patch) | |
tree | 6a890de83fb96ec960cb2928f1c3f23f516fa329 /src/main/java/com/google/devtools/build/lib/syntax | |
parent | 19a3363786f8b46534d1c86bceba0a63fd0dbf36 (diff) |
Add the flag --incompatible_keyword_only_syntax to forbid keyword-only syntax.
Keyword-only syntax is when a parameter happens after the `*` paramter, e.g.
def foo(a, *, b): pass
or:
def foo(a, *b, c): pass
The syntax is not compatible with Python 2 (it's supported only in Python 3),
and breaking tooling based on Python2.
RELNOTES:
Keyword-only syntax in a function definition is deprecated
(e.g. `def foo(a, *, b)` or `def foo(a, *b, c)`) and will be removed in the future.
PiperOrigin-RevId: 155071704
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/FunctionDefStatement.java | 13 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemanticsOptions.java | 8 |
2 files changed, 19 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/FunctionDefStatement.java b/src/main/java/com/google/devtools/build/lib/syntax/FunctionDefStatement.java index 70c4c95943..cde876e58a 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/FunctionDefStatement.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/FunctionDefStatement.java @@ -49,12 +49,21 @@ public class FunctionDefStatement extends Statement { defaultValues.add(expr.eval(env)); } } + + FunctionSignature sig = signature.getSignature(); + if (env.getSemantics().incompatibleKeywordOnlySyntax + && sig.getShape().getMandatoryNamedOnly() > 0) { + throw new EvalException( + getLocation(), + "Keyword-only argument is forbidden. You can temporarily disable this " + + "error using the flag --incompatible_keyword_only_syntax=false"); + } + env.update( ident.getName(), new UserDefinedFunction( ident, - FunctionSignature.WithValues.<Object, SkylarkType>create( - signature.getSignature(), defaultValues, types), + FunctionSignature.WithValues.<Object, SkylarkType>create(sig, defaultValues, types), statements, env.getGlobals())); } diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemanticsOptions.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemanticsOptions.java index 5ed2643e90..0c83c25760 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemanticsOptions.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemanticsOptions.java @@ -50,4 +50,12 @@ public class SkylarkSemanticsOptions extends OptionsBase implements Serializable help = "If set to true, disables the deprecated `set` constructor for depsets." ) public boolean incompatibleDepsetConstructor; + + @Option( + name = "incompatible_keyword_only_syntax", + defaultValue = "false", + category = "incompatible changes", + help = "If set to true, disables the keyword-only argument syntax in function definition." + ) + public boolean incompatibleKeywordOnlySyntax; } |