Skip to content

nushell functions see tables as non-table and non-tables as table #7699

Closed

Description

@amtoine

Describe the bug

we've noticed, with @Eldyj and @fdncred over on the discord server a kinda strange behaviour when annotating an argument with the table type 😮

in the example below, i've tried to simplify the original lesscase function into what's causing the issue only

  • the : table in the signature

original discussion: https://discord.com/channels/601130461678272522/615253963645911060/1060542132848431144

How to reproduce

define a foo command with

def foo [tab: table] {
  $tab | describe
  $tab
}

run the following tests

1. expected to fail
> foo 10
Error: nu::parser::parse_mismatch (link)

  × Parse mismatch during operation.
   ╭─[entry #9:1:1]
 1 │ foo 10
   ·     ─┬
   ·      ╰── expected table
   ╰────
> foo "foo"
Error: nu::parser::parse_mismatch (link)

  × Parse mismatch during operation.
   ╭─[entry #10:1:1]
 1 │ foo "foo"
   ·     ──┬──
   ·       ╰── expected table
   ╰────
2. should fail because they violate the signature
> foo []
list<any>
> foo [1, 2, 3]
list<int>
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
╰───┴───╯
> foo [foo]
list<string>
╭───┬─────╮
│ 0 │ foo │
╰───┴─────╯
> foo {a: "A", b: "B"}
record<a: string, b: string>
╭───┬───╮
│ a │ A │
│ b │ B │
╰───┴───╯
3. should not fail because it respects the signature but still fails
> foo [[a b]; [1 2] [3, 4]]
Error: nu::parser::type_mismatch (link)

  × Type mismatch.
   ╭─[entry #15:1:1]
 1 │ foo [[a b]; [1 2] [3, 4]]
   ·     ──────────┬──────────
   ·               ╰── expected List(Any), found Table([])
   ╰────

note that

> [[a b]; [1 2] [3, 4]] | describe
table<a: int, b: int>

Expected behavior

i expected the following to work, and they did

> foo 10
> foo "foo"

i kinda expected the following to fail

> foo []
> foo [1, 2, 3]
> foo [foo]
> foo {a: "A", b: "B"}

in the end, the four above are some particular views of table, imo everything apart from basic types such as int can be seen as a table, right?

i definitely expected the last one to work, because describe says it's a table

> foo [[a b]; [1 2] [3, 4]]

Screenshots

No response

Configuration

key value
version 0.73.1
branch main
commit_hash a9bdc65
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)
cargo_version cargo 1.66.0
pkg_version 0.73.1
build_time 2023-01-06 19:10:15 +01:00
build_rust_channel debug
features database, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. amtoine commented on Jan 11, 2023

    @amtoine
    MemberAuthor

    and there is definitely something going on with the signature parser 🤔

    i was writing some functions to wrap the go implementation of IPFS, called kubo, but i get the following

    # ipfs.nu
    
    # TODO: docstring
    #
    # example usage on the module itself
    # ```bash
    # > ipfs test chunk ipfs.nu [size-256 size-512 size-256144]
    # ╭───┬────────────────────────────────────────────────┬─────────────┬───────┬──────╮
    # │ # │                      hash                      │   chunker   │ links │ data │
    # ├───┼────────────────────────────────────────────────┼─────────────┼───────┼──────┤
    # │ 0 │ QmR6mnWt9knDXZ7SWudC1kaCoGdw6NFG6pDhPwuVttF68s │ size-256    │     3 │   18 │
    # │ 1 │ QmXmrC1j55i3GTYC7MfUZUHwQbU2j81Bna7GRxHwrGcXMt │ size-512    │     2 │   14 │
    # │ 2 │ QmPnMhGNKdRMUPckZG9td94x5CzMwdA6YhTNtbu4bpGgyL │ size-256144 │     0 │  855 │
    # ╰───┴────────────────────────────────────────────────┴─────────────┴───────┴──────╯
    # ```
    #
    export def "test chunk" [
      file: string  # TODO: arg
      chunkers: list = [size-256144]
    ] {
      let results = (
        $chunkers
        | each {|chunker|
          let hash = (ipfs add --quieter $file --chunker $chunker)
          {
            hash: $hash
            chunker: $chunker
            links: (ipfs dag get $hash | from json | get Links | length)
            data: (ipfs dag get $hash | from json | get Data./.bytes | split chars | length)
          }
        }
      )
      
      $results
    }

    but this gives me

    > use ipfs.nu
    Error: nu::parser::assignment_mismatch (link)
    
      × Default value wrong type
        ╭─[ipfs.nu:13:1]
     13 │   file: string  # TODO: arg
     14 │   chunkers: list = [size-256144]
        ·                    ──────┬──────
        ·                          ╰── default value not list<any>
     15 │ ] {
        ╰────

    i have to remove the : list on the chunker argument to mitigate the error 👍

    also...

    it's quite strange to get the following error when annotation with : list<any>, isn't it? 😕

    > use ipfs.nu
    Error: nu::parser::unknown_type (link)
    
      × Unknown type.
        ╭─[ipfs.nu:13:1]
     13 │   file: string  # TODO: arg
     14 │   chunkers: list<any> = [size-256144]
        ·             ────┬────
        ·                 ╰── unknown type
     15 │ ] {
        ╰────
  2. added
    category:bug
    A:parser
    on Jan 11, 2023
  3. added
    A:type-system
    on Jan 12, 2023
  4. fdncred commented on Feb 16, 2023

    @fdncred
    Contributor

    Hey @amtoine!

    I think these are two separate issues.
    The stuff in the first post is fixed for now. The only questionable thing is that records and tables work the same. The rest fail. Records and Tables work the same because a table is a list of Records.

    The second issue is separate where you can't do chunkers: list = [size-256144] as a parameter with a default. Can you please open that as a new issue? Then we can close this one.

    Also, I think the list<any> thing is just saying that parameters of list type can't have defaults. I wonder if that's even in the source code. I'm betting we left it out. I'm really not sure what's going on there but that looks like a bug to me.

  5. amtoine commented on Feb 18, 2023

    @amtoine
    MemberAuthor

    Hey @amtoine!

    hey there 👋 😋

    I think these are two separate issues. The stuff in the first post is fixed for now. The only questionable thing is that records and tables work the same. The rest fail. Records and Tables work the same because a table is a list of Records.

    yep i see that, thanks for the ping 😌

    you're right, the last one that "does not behave as expected" is

    foo {a: "A", b: "B"}

    but yeah, records and tables looks really the same!

    however theses are not the same types 😕
    should {a: "A", b: "B"} and [[a b]; [1 2] [3, 4]] behave the same?

    The second issue is separate where you can't do chunkers: list = [size-256144] as a parameter with a default. Can you please open that as a new issue? Then we can close this one.

    Also, I think the list<any> thing is just saying that parameters of list type can't have defaults. I wonder if that's even in the source code. I'm betting we left it out. I'm really not sure what's going on there but that looks like a bug to me.

    okey, let me file another issue 😉

  6. amtoine commented on Feb 18, 2023

    @amtoine
    MemberAuthor

    maybe we close this one?
    even though the record/table "equivalence" still looks a bit strange 😕
    'cause if record ~ list<table> then the types really are different, right? 🤔

  7. fdncred commented on Feb 18, 2023

    @fdncred
    Contributor

    I tend to think <a record> | describe should show a record and <a table> | describe should show a table.

  8. amtoine commented on Feb 18, 2023

    @amtoine
    MemberAuthor

    I tend to think <a record> | describe should show a record and <a table> | describe should show a table.

    agree 👌

    but then what about a function that takes a table as above, should it give an error given a record? 😏
    because a table is a list<record<_>> i would say it should give an error 🤔

  9. amtoine commented on Feb 22, 2023

    @amtoine
    Author
  10. WindSoilder commented on May 19, 2023

    @WindSoilder
    Contributor

    I think this issue is already resolved, I can't reproduce it on 0.80;

    Will close it for now, fell free to re-open it if you still get the issue

  11. amtoine commented on May 19, 2023

    @amtoine
    MemberAuthor

    good catch @WindSoilder, this looks solved 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:parserA:type-systemcategory:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions