Skip to content

Also type-check optional arguments - #16194

Merged
WindSoilder merged 1 commit into
nushell:mainfrom
marienz:typecheck-optional
Jul 17, 2025
Merged

WindSoilder merged 1 commit into
nushell:mainfrom
marienz:typecheck-optional

Conversation

@marienz

@marienz marienz commented Jul 17, 2025

Copy link
Copy Markdown
Contributor

Description

Type-check all closure arguments, not just required arguments.

Not doing so looks like an oversight.

User-Facing Changes

Previously, passing an argument of the wrong type to a closure would fail if the argument is required, but be accepted (ignoring the type annotation) if the argument is optional:

> do {|x: string| $x} 4
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry #13:1:21]
 1 │ do {|x: string| $x} 4
   ·                     ┬
   ·                     ╰── can't convert int to string
   ╰────
> do {|x?: string| $x} 4
4
> do {|x?: string| $x} 4 | describe
int

It now fails the same way in both cases.

Tests + Formatting

Added tests, the existing tests still pass.

Please let me know if I added the wrong type of test or added them in the wrong place (I didn't spot similar tests in the nu-cmd-lang crate, so I put them next to the most-related existing tests I could find...

After Submitting

I think this is minor enough it doesn't need a doc update, but please point me in the right direction if not.

Not doing so looks like an oversight.

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you! It looks good to me

@WindSoilder
WindSoilder merged commit 7ce66a9 into nushell:main Jul 17, 2025
@github-actions github-actions Bot added this to the v0.106.0 milestone Jul 17, 2025
@marienz
marienz deleted the typecheck-optional branch July 17, 2025 13:49
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Aug 25, 2025
# Description
Type-check all closure arguments, not just required arguments.

Not doing so looks like an oversight.

# User-Facing Changes

Previously, passing an argument of the wrong type to a closure would
fail if the argument is required, but be accepted (ignoring the type
annotation) if the argument is optional:

```
> do {|x: string| $x} 4
Error: nu::shell::cant_convert

  × Can't convert to string.
   ╭─[entry nushell#13:1:21]
 1 │ do {|x: string| $x} 4
   ·                     ┬
   ·                     ╰── can't convert int to string
   ╰────
> do {|x?: string| $x} 4
4
> do {|x?: string| $x} 4 | describe
int
```

It now fails the same way in both cases.

# Tests + Formatting
Added tests, the existing tests still pass.

Please let me know if I added the wrong type of test or added them in
the wrong place (I didn't spot similar tests in the nu-cmd-lang crate,
so I put them next to the most-related existing tests I could find...

# After Submitting
I think this is minor enough it doesn't need a doc update, but please
point me in the right direction if not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants