Skip to content

Commit

Permalink
Support and deprecate empty args
Browse files Browse the repository at this point in the history
  • Loading branch information
odelalleau committed Feb 26, 2021
1 parent d46d058 commit e262502
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion omegaconf/grammar/OmegaConfGrammarParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ element:
listContainer: BRACKET_OPEN sequence? BRACKET_CLOSE; // [], [1,2,3], [a,b,[1,2]]
dictContainer: BRACE_OPEN (dictKeyValuePair (COMMA dictKeyValuePair)*)? BRACE_CLOSE; // {}, {a:10,b:20}
dictKeyValuePair: dictKey COLON element;
sequence: element (COMMA element)*;
sequence: (element (COMMA element?)*) | (COMMA element?)+;

// Interpolations.

Expand Down
28 changes: 25 additions & 3 deletions omegaconf/grammar_visitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import warnings
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -246,20 +247,41 @@ def visitSequence(
) -> Generator[Any, None, None]:
from ._utils import _get_value

assert ctx.getChildCount() >= 1 # element (COMMA element)*
# (element (COMMA element?)*) | (COMMA element?)+
assert ctx.getChildCount() >= 1

def empty_str_warning() -> None:
txt = ctx.getText()
warnings.warn(
f"In the sequence `{txt}` some elements are missing: please replace "
f"them with empty strings (\"\" or '') since this will not be supported "
f"anymore in the future.",
category=UserWarning,
)

is_previous_comma = True # whether previous child was a comma (init to True)
for i, child in enumerate(ctx.getChildren()):
if i % 2 == 0:
assert isinstance(child, OmegaConfGrammarParser.ElementContext)
if isinstance(child, OmegaConfGrammarParser.ElementContext):
# Also preserve the original text representation of `child` so
# as to allow backward compatibility with old resolvers (registered
# with `legacy_register_resolver()`). Note that we cannot just cast
# the value to string later as for instance `null` would become "None".
yield _get_value(self.visitElement(child)), child.getText()
is_previous_comma = False
else:
assert (
isinstance(child, TerminalNode)
and child.symbol.type == OmegaConfGrammarLexer.COMMA
)
if is_previous_comma:
empty_str_warning()
yield "", ""
else:
is_previous_comma = True
if is_previous_comma:
# Trailing comma.
empty_str_warning()
yield "", ""

def visitSingleElement(
self, ctx: OmegaConfGrammarParser.SingleElementContext
Expand Down

0 comments on commit e262502

Please sign in to comment.