Skip to content

Make length only operate on supported input types#14475

Merged
sholderbach merged 2 commits intonushell:mainfrom
132ikl:length-input-output
Nov 29, 2024
Merged

Make length only operate on supported input types#14475
sholderbach merged 2 commits intonushell:mainfrom
132ikl:length-input-output

Conversation

@132ikl
Copy link
Copy Markdown
Member

@132ikl 132ikl commented Nov 29, 2024

Description

Before this PR, length did not check its input type at run-time, so it would attempt to calculate a length for any input with indeterminate type (e.g., echo which has an any output type). This PR makes length only work on the types specifically supported in its input/output types (list/table, binary, and nothing), making the behavior the same at parse-time and at run-time.

Fixes #14462

User-Facing Changes

Length will error if passed an unsupported type:

Before (only caught at parse-time):

"hello" | length
Error: nu::parser::input_type_mismatch

  × Command does not support string input.
   ╭─[entry #2:1:11]
 1  "hello" | length
   ·           ───┬──
   ·              ╰── command doesn't support string input
   ╰────

echo "hello" | length
# => 1

After (caught at parse-time and run-time):

"hello" | length
Error: nu::parser::input_type_mismatch

  × Command does not support string input.
   ╭─[entry #22:1:11]
 1  "hello" | length
   ·           ───┬──
   ·              ╰── command doesn't support string input
   ╰────

echo "hello" | length
Error: nu::shell::only_supports_this_input_type

  × Input type not supported.
   ╭─[entry #23:1:6]
 1 │ echo "hello" | length
   ·      ───┬───   ───┬──
   ·         │         ╰── only list, table, binary, and nothing input data is supported
   ·         ╰── input type: string
   ╰────

Tests + Formatting

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

Copy link
Copy Markdown
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call around Nothing and Empty! Thanks for spotting and fixing this

@sholderbach sholderbach merged commit 5f04bbb into nushell:main Nov 29, 2024
@github-actions github-actions bot added this to the v0.101.0 milestone Nov 29, 2024
@fdncred fdncred added the deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way label Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sort-by {length} on list of strings doesn't result in error

3 participants