Skip to content

Add duplicate keyword argument error #1407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum LexicalErrorType {
UnicodeError,
NestingError,
PositionalArgumentError,
DuplicateKeywordArgumentError,
UnrecognizedToken { tok: char },
FStringError(FStringErrorType),
OtherError(String),
Expand All @@ -33,6 +34,9 @@ impl fmt::Display for LexicalErrorType {
LexicalErrorType::FStringError(error) => write!(f, "Got error in f-string: {}", error),
LexicalErrorType::UnicodeError => write!(f, "Got unexpected unicode"),
LexicalErrorType::NestingError => write!(f, "Got unexpected nesting"),
LexicalErrorType::DuplicateKeywordArgumentError => {
write!(f, "keyword argument repeated")
}
LexicalErrorType::PositionalArgumentError => {
write!(f, "positional argument follows keyword argument")
}
Expand Down
15 changes: 15 additions & 0 deletions parser/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::ast;
use crate::error::{LexicalError, LexicalErrorType};

Expand All @@ -6,9 +8,22 @@ type FunctionArgument = (Option<Option<String>>, ast::Expression);
pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ast::ArgumentList, LexicalError> {
let mut args = vec![];
let mut keywords = vec![];

let mut keyword_names = HashSet::with_capacity(func_args.len());
for (name, value) in func_args {
match name {
Some(n) => {
if let Some(keyword_name) = n.clone() {
if keyword_names.contains(&keyword_name) {
return Err(LexicalError {
error: LexicalErrorType::DuplicateKeywordArgumentError,
location: value.location.clone(),
});
}

keyword_names.insert(keyword_name.clone());
}

keywords.push(ast::Keyword { name: n, value });
}
None => {
Expand Down
7 changes: 7 additions & 0 deletions tests/snippets/function_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ def func(**kwargs):

kwargs = func(a=1, b=2, c=3)
assert kwargs == [('a', 1), ('b', 2), ('c', 3)]


def inc(n):
return n + 1

with assert_raises(SyntaxError):
exec("inc(n=1, n=2)")