Skip to content

Commit

Permalink
Input output checking (#9680)
Browse files Browse the repository at this point in the history
# Description

This PR tights input/output type-checking a bit more. There are a lot of
commands that don't have correct input/output types, so part of the
effort is updating them.

This PR now contains updates to commands that had wrong input/output
signatures. It doesn't add examples for these new signatures, but that
can be follow-up work.

# User-Facing Changes

BREAKING CHANGE BREAKING CHANGE

This work enforces many more checks on pipeline type correctness than
previous nushell versions. This strictness may uncover incompatibilities
in existing scripts or shortcomings in the type information for internal
commands.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
  • Loading branch information
sophiajt committed Jul 14, 2023
1 parent e66139e commit 786ba3b
Show file tree
Hide file tree
Showing 89 changed files with 480 additions and 106 deletions.
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ impl Command for BitsAnd {

fn signature(&self) -> Signature {
Signature::build("bits and")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required(
"target",
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ impl Command for BitsNot {

fn signature(&self) -> Signature {
Signature::build("bits not")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.switch(
"signed",
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ impl Command for BitsOr {

fn signature(&self) -> Signature {
Signature::build("bits or")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required(
"target",
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/rotate_left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ impl Command for BitsRol {

fn signature(&self) -> Signature {
Signature::build("bits rol")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required("bits", SyntaxShape::Int, "number of bits to rotate left")
.switch(
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/rotate_right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ impl Command for BitsRor {

fn signature(&self) -> Signature {
Signature::build("bits ror")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required("bits", SyntaxShape::Int, "number of bits to rotate right")
.switch(
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/shift_left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ impl Command for BitsShl {

fn signature(&self) -> Signature {
Signature::build("bits shl")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required("bits", SyntaxShape::Int, "number of bits to shift left")
.switch(
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/shift_right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ impl Command for BitsShr {

fn signature(&self) -> Signature {
Signature::build("bits shr")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required("bits", SyntaxShape::Int, "number of bits to shift right")
.switch(
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bits/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ impl Command for BitsXor {

fn signature(&self) -> Signature {
Signature::build("bits xor")
.input_output_types(vec![(Type::Int, Type::Int)])
.input_output_types(vec![
(Type::Int, Type::Int),
(
Type::List(Box::new(Type::Int)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.required(
"target",
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/bytes/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ impl Command for BytesLen {

fn signature(&self) -> Signature {
Signature::build("bytes length")
.input_output_types(vec![(Type::Binary, Type::Int)])
.input_output_types(vec![
(Type::Binary, Type::Int),
(
Type::List(Box::new(Type::Binary)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.rest(
"rest",
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-cmd-extra/src/extra/bytes/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ impl Command for BytesRemove {

fn signature(&self) -> Signature {
Signature::build("bytes remove")
.input_output_types(vec![(Type::Binary, Type::Binary)])
.input_output_types(vec![
(Type::Binary, Type::Binary),
(Type::Table(vec![]), Type::Table(vec![])),
])
.required("pattern", SyntaxShape::Binary, "the pattern to find")
.rest(
"rest",
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-cmd-extra/src/extra/bytes/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ impl Command for BytesReplace {

fn signature(&self) -> Signature {
Signature::build("bytes replace")
.input_output_types(vec![(Type::Binary, Type::Binary)])
.input_output_types(vec![
(Type::Binary, Type::Binary),
(Type::Table(vec![]), Type::Table(vec![])),
])
.required("find", SyntaxShape::Binary, "the pattern to find")
.required("replace", SyntaxShape::Binary, "the replacement pattern")
.rest(
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/math/cos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("math cos")
.switch("degrees", "Use degrees instead of radians", Some('d'))
.input_output_types(vec![(Type::Number, Type::Float)])
.input_output_types(vec![
(Type::Number, Type::Float),
(
Type::List(Box::new(Type::Number)),
Type::List(Box::new(Type::Float)),
),
])
.vectorizes_over_list(true)
.category(Category::Math)
}
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/math/sin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("math sin")
.switch("degrees", "Use degrees instead of radians", Some('d'))
.input_output_types(vec![(Type::Number, Type::Float)])
.input_output_types(vec![
(Type::Number, Type::Float),
(
Type::List(Box::new(Type::Number)),
Type::List(Box::new(Type::Float)),
),
])
.vectorizes_over_list(true)
.category(Category::Math)
}
Expand Down
8 changes: 7 additions & 1 deletion crates/nu-cmd-extra/src/extra/math/tan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("math tan")
.switch("degrees", "Use degrees instead of radians", Some('d'))
.input_output_types(vec![(Type::Number, Type::Float)])
.input_output_types(vec![
(Type::Number, Type::Float),
(
Type::List(Box::new(Type::Number)),
Type::List(Box::new(Type::Float)),
),
])
.vectorizes_over_list(true)
.category(Category::Math)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-extra/tests/commands/bytes/starts_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn basic_string_fails() {
"#
);

assert!(actual.err.contains("Input type not supported"));
assert!(actual.err.contains("command doesn't support"));
assert_eq!(actual.out, "");
}

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-lang/src/core_commands/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Command for Collect {

fn signature(&self) -> Signature {
Signature::build("collect")
.input_output_types(vec![(Type::List(Box::new(Type::Any)), Type::Any)])
.input_output_types(vec![(Type::Any, Type::Any)])
.required(
"closure",
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
Expand Down
6 changes: 6 additions & 0 deletions crates/nu-command/src/conversions/into/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("into decimal")
.input_output_types(vec![
(Type::Int, Type::Number),
(Type::String, Type::Number),
(Type::Bool, Type::Number),
(Type::Table(vec![]), Type::Table(vec![])),
(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::Number)),
),
])
.rest(
"rest",
SyntaxShape::CellPath,
"for a data structure input, convert data at the given cell paths",
)
.allow_variants_without_examples(true)
.category(Category::Conversions)
}

Expand Down
7 changes: 7 additions & 0 deletions crates/nu-command/src/conversions/into/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ impl Command for SubCommand {
(Type::Bool, Type::Int),
// Unix timestamp in nanoseconds
(Type::Date, Type::Int),
(Type::Duration, Type::Int),
// TODO: Users should do this by dividing a Filesize by a Filesize explicitly
(Type::Filesize, Type::Int),
(Type::Table(vec![]), Type::Table(vec![])),
(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::Int)),
),
])
.vectorizes_over_list(true)
.allow_variants_without_examples(true)
.named("radix", SyntaxShape::Number, "radix of integer", Some('r'))
.switch("little-endian", "use little-endian byte decoding", None)
.rest(
Expand Down
4 changes: 4 additions & 0 deletions crates/nu-command/src/conversions/into/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ impl Command for SubCommand {
(Type::Bool, Type::String),
(Type::Filesize, Type::String),
(Type::Date, Type::String),
(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::String)),
),
])
.allow_variants_without_examples(true) // https://github.com/nushell/nushell/issues/7032
.rest(
Expand Down
5 changes: 4 additions & 1 deletion crates/nu-command/src/env/load_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ impl Command for LoadEnv {

fn signature(&self) -> nu_protocol::Signature {
Signature::build("load-env")
.input_output_types(vec![(Type::Record(vec![]), Type::Nothing)])
.input_output_types(vec![
(Type::Record(vec![]), Type::Nothing),
(Type::Nothing, Type::Nothing),
])
.allow_variants_without_examples(true)
.optional(
"update",
Expand Down
12 changes: 8 additions & 4 deletions crates/nu-command/src/filters/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ impl Command for Append {

fn signature(&self) -> nu_protocol::Signature {
Signature::build("append")
.input_output_types(vec![(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::Any)),
)])
.input_output_types(vec![
(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::Any)),
),
(Type::Record(vec![]), Type::Table(vec![])),
])
.required("row", SyntaxShape::Any, "the row, list, or table to append")
.allow_variants_without_examples(true)
.category(Category::Filters)
}

Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/filters/each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ with 'transpose' first."#
Type::List(Box::new(Type::Any)),
),
(Type::Table(vec![]), Type::List(Box::new(Type::Any))),
(Type::Any, Type::Any),
])
.required(
"closure",
SyntaxShape::Closure(Some(vec![SyntaxShape::Any, SyntaxShape::Int])),
"the closure to run",
)
.switch("keep-empty", "keep empty result cells", Some('k'))
.allow_variants_without_examples(true)
.category(Category::Filters)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/filters/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Command for Find {
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::Any)),
),
(Type::String, Type::String),
(Type::String, Type::Any),
(
// For find -p
Type::Table(vec![]),
Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/filters/first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ impl Command for First {
Type::Any,
),
(Type::Binary, Type::Binary),
(Type::Range, Type::Any),
])
.optional(
"rows",
SyntaxShape::Int,
"starting from the front, the number of rows to return",
)
.allow_variants_without_examples(true)
.category(Category::Filters)
}

Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/filters/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ If multiple cell paths are given, this will produce a list of values."#
Type::Any,
),
(Type::Table(vec![]), Type::Any),
(Type::Record(vec![]), Type::Any),
])
.required(
"cell_path",
Expand All @@ -51,6 +52,7 @@ If multiple cell paths are given, this will produce a list of values."#
"get path in a case sensitive manner",
Some('s'),
)
.allow_variants_without_examples(true)
.category(Category::Filters)
}

Expand Down
5 changes: 5 additions & 0 deletions crates/nu-command/src/filters/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ impl Command for Insert {
.input_output_types(vec![
(Type::Record(vec![]), Type::Record(vec![])),
(Type::Table(vec![]), Type::Table(vec![])),
(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::Any)),
),
])
.required(
"field",
Expand All @@ -30,6 +34,7 @@ impl Command for Insert {
SyntaxShape::Any,
"the new value to give the cell(s)",
)
.allow_variants_without_examples(true)
.category(Category::Filters)
}

Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/filters/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Command for Select {
.input_output_types(vec![
(Type::Record(vec![]), Type::Record(vec![])),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::List(Box::new(Type::Any)), Type::Any),
])
.switch(
"ignore-errors",
Expand All @@ -32,6 +33,7 @@ impl Command for Select {
SyntaxShape::CellPath,
"the columns to select from the table",
)
.allow_variants_without_examples(true)
.category(Category::Filters)
}

Expand Down

0 comments on commit 786ba3b

Please sign in to comment.