Skip to content
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

stdlib: Update signature of CSV.read #2002

Merged
merged 1 commit into from
Sep 13, 2024
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
3 changes: 2 additions & 1 deletion stdlib/csv/0/csv.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,8 @@ class CSV < Object
# File.write(path, string)
# CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
#
def self.read: (String path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]
def self.read: (String | IO path, headers: true | :first_row | Array[untyped] | String, **untyped options) -> ::CSV::Table[CSV::Row]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what is the best to type args for CSV::Table. Please let me know if there is a more appropriated one.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like the type parameter is not used...

| (String | IO path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]

# <!--
# rdoc-file=lib/csv.rb
Expand Down
28 changes: 28 additions & 0 deletions test/stdlib/CSV_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ def test_foreach
assert_send_type "(String path, headers: bool, **untyped) -> Enumerator[CSV::Row, void]",
CSV, :foreach, path, headers: true, encoding: 'UTF-8'
end

def test_read
tmpdir = Dir.mktmpdir
path = File.join(tmpdir, "example.csv")
File.write(path, "a,b,c\n1,2,3\n")

assert_send_type "(String path, headers: true) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: true
assert_send_type "(IO path, headers: true) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: true
assert_send_type "(String path, headers: :first_row) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: :first_row
assert_send_type "(IO path, headers: :first_row) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: :first_row
assert_send_type "(String path, headers: Array[String]) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: %w[foo bar baz]
assert_send_type "(IO path, headers: Array[String]) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: %w[foo bar baz]
assert_send_type "(String path, headers: String) -> CSV::Table[CSV::Row]",
CSV, :read, path, headers: "foo,bar,baz"
assert_send_type "(IO path, headers: String) -> CSV::Table[CSV::Row]",
CSV, :read, File.open(path), headers: "foo,bar,baz"

assert_send_type "(String path) -> Array[Array[String?]]",
CSV, :read, path
assert_send_type "(IO path) -> Array[Array[String?]]",
CSV, :read, File.open(path)
end
end