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

Update type for CSV.foreach #1738

Merged
merged 3 commits into from
Jan 29, 2024
Merged

Update type for CSV.foreach #1738

merged 3 commits into from
Jan 29, 2024

Conversation

m11o
Copy link
Contributor

@m11o m11o commented Jan 28, 2024

This Pull Request updates the type signatures of CSV.foreach in RBS.
When CSV.foreach is called with the headers keyword argument, the block parameter or the elements of the returned Enumerator are of type CSV::Row. If the headers argument is not provided, the elements of the Enumerator will be arrays with elements of type String.

This change accurately reflects the behavior of CSV.foreach depending on the presence of the headers keyword argument.

This Pull Request does not include changes for other methods that accept the headers keyword argument and have behavior variations based on this argument. After this Pull Request is merged, I plan to address these methods in a separate Pull Request.

Thanks.

CSV.foreach: https://github.com/ruby/csv/blob/0cba3e766d34373043b4ca0bbae93c8de12014f0/lib/csv.rb#L1332

@@ -1722,7 +1722,10 @@ class CSV < Object
# would read `UTF-32BE` data from the file but transcode it to `UTF-8`
# before parsing.
#
def self.foreach: [U] (String | IO | StringIO path, ?::Hash[Symbol, U] options) { (::Array[String?] arg0) -> void } -> void
def self.foreach: (String | IO path, ?String mode, headers: true, **untyped options) { (::CSV::Row arg0) -> void } -> void
Copy link
Contributor Author

@m11o m11o Jan 28, 2024

Choose a reason for hiding this comment

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

Accurately, the headers keyword argument can't be only received true, but also truthy value, for example, String and Symbol, etc.

However, As headers argument is often provided true, I defined true only in this methods.
Please let me know if not good. Thanks.

Because csv gem promoted bundled gem from default gem.
@@ -18,6 +18,7 @@ GEM
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.6)
csv (3.2.8)
Copy link
Contributor Author

@m11o m11o Jan 28, 2024

Choose a reason for hiding this comment

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

Add csv gem to Gemfile because it is changed from default gem to bundled gem in ruby 3.4.
ref: #1734

@m11o m11o changed the title Update CSV.foreach signatures Update type for CSV.foreach Jan 28, 2024
Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

Thanks!

Test is failing but unrelated to this change.

@soutaro
Copy link
Member

soutaro commented Jan 29, 2024

Technically, the type definition will result in an incorrect result:

bool = true
CSV.foreach(..., headers: bool) do |row|
  # `bool` is `bool` type, not `true`, and `row` is `String`.
end

I think it's not too weird to assume we pass a true literal typically, as headers: true.

@soutaro soutaro added this pull request to the merge queue Jan 29, 2024
Merged via the queue into ruby:master with commit 8e6ccef Jan 29, 2024
15 checks passed
@soutaro soutaro added this to the RBS 3.5 milestone Jun 6, 2024
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

2 participants