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: Add types for URI::MailTo #1858

Merged
merged 1 commit into from
Jun 6, 2024
Merged

Conversation

tk0miya
Copy link
Contributor

@tk0miya tk0miya commented Jun 2, 2024

No description provided.

@@ -4,5 +4,89 @@ module URI
# RFC6068, the mailto URL scheme.
#
class MailTo < Generic
EMAIL_REGEXP: Regexp
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This constant is not documented in the official document.
https://docs.ruby-lang.org/en/3.3/URI/MailTo.html

But it's well used for verifying the format of email addresses.
For example, the Rails Guide mentions this constant in implementing validators on Rails.
https://guides.rubyonrails.org/active_record_validations.html#custom-validators

# m3 = URI::MailTo.build({:to => 'listman@example.com', :headers => [['subject', 'subscribe']]})
# m3.to_s # => "mailto:listman@example.com?subject=subscribe"
#
def self.build: (Array[String]) -> instance
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The official document says URI::MailTo.build takes a tuple of strings. So it would be correct and accurate to use ([String, String]) -> instance instead of this line. But it's not good for use because Steep recognizes the type of tuple literal of strings as an Array[String].

#
def self.build: (Array[String]) -> instance
| ([String, Array[Array[String]]]) -> instance
| (Hash[Symbol, String | Array[Array[String]]]) -> instance
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would also be correct and accurate to use a record type ({ to: String, headers: String | Array[Array[String]] }) -> instance instead. But it's not good for use too.

# <!-- rdoc-file=lib/uri/mailto.rb -->
# E-mail headers set by the URL, as an Array of Arrays.
#
def headers: () -> Array[[String, String]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found invalid pattern by raap.

$ bundle exec raap -r uri --library uri URI::MailTo#headers
[RaaP] INFO: # URI::MailTo
[RaaP] INFO: ## def headers: () -> ::Array[[ ::String, ::String ]]
EEF

() -> Array[[String, String]]

[RaaP] INFO: success: 0, skip: 0, exception: 2, time: 15ms
# 1) Failure:

def headers: () -> ::Array[[ ::String, ::String ]]
  in /Users/ksss/src/github.com/ksss/rbs/stdlib/uri/0/mailto.rbs:46:17...46:46

## Reason

Failed in case of `#<URI::MailTo>.headers() -> [["ez"]][Array[Array[String]]]`

### Repro

```rb
uri_mailto = URI::MailTo.new(nil, nil, 'lm', -4, nil, nil, nil, 'ez', nil)
uri_mailto.headers()
```

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 query (?ez) cannot be constructed with URI::MailTo.build.

irb(main):020> URI::MailTo.build(["hoge@huga", "foo"]).headers
/Users/soutaro/.rbenv/versions/3.3.2/lib/ruby/3.3.0/uri/mailto.rb:212:in `check_headers': bad component(expected opaque component): foo (URI::InvalidComponentError)

I think we can have Array[[String, String]] here, even raap detects an error.

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!

@soutaro soutaro added this pull request to the merge queue Jun 6, 2024
@soutaro soutaro added this to the RBS 3.5 milestone Jun 6, 2024
Merged via the queue into ruby:master with commit ca651ca Jun 6, 2024
18 checks passed
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
@tk0miya tk0miya deleted the uri_mailto branch June 6, 2024 10:37
@tk0miya
Copy link
Contributor Author

tk0miya commented Jun 6, 2024

Thank you for your review and merge!

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.

3 participants