[ruby-core:117564] [Ruby master Bug#20433] Hash.inspect for some hash returns syntax invalid representation

Issue #20433 has been reported by tompng (tomoya ishida). ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by byroot (Jean Boussier). An easy fix would be for `Hash#inspect` to include spaces around `=>`. It would also read much nicer in my opinion. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-107967 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by nobu (Nobuyoshi Nakada). Or use `{a!: 1}` for symbol keys? ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-107985 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by nobu (Nobuyoshi Nakada). Ditto for pp.rb. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-107987 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by jeremyevans0 (Jeremy Evans). I submitted a pull request for the most backwards compatible change, which only uses spaces around `=>` for symbols ending in `[!?*=<]`: https://github.com/ruby/ruby/pull/10737 ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-108201 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by Eregon (Benoit Daloze). I think always having spaces would help readability. If we don't want to change that, then how about simply quoting these symbols that need it? ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-108203 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by Dan0042 (Daniel DeLorme).
If we don't want to change that, then how about simply quoting these symbols that need it?
+1 ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-108220 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by mame (Yusuke Endoh). Discussed at the dev meeting. In conclusion, @matz wanted to change the return value of `Hash#inspect` significantly and estimate its compatibility impact: ```ruby { :key => 42 } #=> {key: 42} # if the key is a symbol { :== => 42 } #=> {"==": 42} # if the key is a symbol and quotes are needed { "str" => 42 } #=> {"str" => 42} # otherwise (note that `=>` is surrounded by spaces) # when keys are mixed { :== => 1, :key => 2, "str" => 3 } # {"==": 1, key: 2, "str" => 3} ``` Actually, some solutions were discussed. Solution 1: insert a space before `=>` only when needed. ```ruby { :key => 1 } # {:key=>1} { :== => 1 } # {:== =>1} { :a! => 1 } # {:a! =>1} ``` Solution 2: insert spaces before and after `=>` consistently ```ruby { :key => 1 } # {:key => 1} { :== => 1 } # {:== => 1} { :a! => 1 } # {:a! => 1} ``` Solution 3: quote keys only when needed ```ruby { :== => 1 } # {:"=="=>1} { :a! => 1 } # {:"a!"=>1} { :key => 1 } # {:key=>1} ``` Matz said he likes Solution 2. However, this has a compatibility issue. It may break some existing test assertions. Matz had had the idea of denoting symbol keys by a colon in `Hash#inspect`. And the incompatibility impacts of Solution 2 and of symbol keys by colons are expected to be about the same. Rather than introducing the incompatibilities in two steps, Matz said he wants to change it at once. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-108688 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by tompng (tomoya ishida). I created a proof of concept patch for colon style inspect https://github.com/ruby/ruby/pull/10924 ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-108707 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by tompng (tomoya ishida). I investigated the impact to ruby ci. Need to update these tests. The required change was within the expected range. ~~~ bootstraptest test/coverage test/error_highlight test/rdoc test/reline test/ruby test/rubygems spec/bundler spec/ruby ~~~ Two bundled gem tests failed. ~~~ minitest (4 failure) debug (2 failure) ~~~ All of them passes by only changing the expected string/regexp from `key=>value` to `key => value` or `:key=>value` to `key: value`. ## Test against several ruby versions Test of mspec, default gems and bundled gems needs to pass in several ruby versions. In most case, changing `expected = "message {1=>2}"` to `expected = "message #{{1=>2}.inspect}"` was enough. There are some case that needs a little more effort like this. ~~~ruby # spec/ruby/core/string/modulo_spec.rb { a: obj }.send(@method).should =~ /^\{:a=>#<MockObject:0x[0-9a-f]+>\}$/ # spec/ruby/library/net-http/http/send_request_spec.rb response.body.should include('"Referer"=>"' + referer + '"') # minitst/minitest test/minitest/test_minitest_mock.rb exp = "expected foo(:kw=>false) => nil, got [foo(:kw=>true) => nil]" ~~~ @matz For your information ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-108827 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by matz (Yukihiro Matsumoto). IMO, this is a good chance to upgrade Hash#inspect format to adopt current Hash usage. * symbol keys should be represented by "`key:`" * ambiguous keys should be wrapped by (double) quotation marks * spaces should be placed after "`:`" and around "`=>`". I'd propose that we try new format for release candidates. If serious compatibility issues happen (although I don't believe we will see), we re-consider this change again. Matz. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-109200 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by zverok (Victor Shepelev).
ambiguous keys should be wrapped by (double) quotation marks
BTW, if the change is so radical, can we also (start to) adopt Python’s flexible quotation marks? (For now, only for Hash keys, but once, maybe, for strings): ```python print("test 'quotes'".__repr__()) #=> "test 'quotes'" print('test "quotes"'.__repr__()) #=> 'test "quotes"' ``` I.e. make it so for Hash keys: ```ruby {"'": true, '"': false}.inspect #=> {"'": true, '"': false} ``` ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-109202 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by vo.x (Vit Ondruch). matz (Yukihiro Matsumoto) wrote in #note-10:
IMO, this is a good chance to upgrade Hash#inspect format to adopt current Hash usage.
* symbol keys should be represented by "`key:`"
I wish we stayed with the hash rockets, sigh ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-109216 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by byroot (Jean Boussier). @tompng @mame what is the status on this? I see https://github.com/ruby/ruby/pull/10924 has been merged, should this ticket be closed? I personally love this change, but I have a lot of tests to update as a result, so I'd like to know if this is certain this change will be part of 3.4.0 before I start the work of fixing it all. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-110054 * Author: tompng (tomoya ishida) * Status: Open * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by mame (Yusuke Endoh). Status changed from Open to Closed Yes, I have merged it since @matz said at the dev meeting today that he still wants to try this change in the next preview release. So closing. However, it may be reverted if the impact of the incompatibility is too large. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-110055 * Author: tompng (tomoya ishida) * Status: Closed * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/

Issue #20433 has been updated by byroot (Jean Boussier). To provide some datapoint, here's the impact on Rails test suite: https://github.com/rails/rails/pull/53202 I still love the change, and it really wasn't much work to fix, but I fear we may hear some complaints. ---------------------------------------- Bug #20433: Hash.inspect for some hash returns syntax invalid representation https://bugs.ruby-lang.org/issues/20433#change-110083 * Author: tompng (tomoya ishida) * Status: Closed * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- For these hashes, Hash.inspect returns a syntax invalid representation: ~~~ruby { :a! => 1 } # {:a!=>1} { :a? => 1 } # {:a?=>1} { :* => 1 } # {:*=>1} { :== => 1 } # {:===>1} { :< => 1 } # {:<=>1} ~~~ `eval(hash.inspect)` will raise SyntaxError. Although inspect does not guarantee that the result can be eval-ed, it'd be confusing for these few cases. Maybe related to https://bugs.ruby-lang.org/issues/20235 -- https://bugs.ruby-lang.org/
participants (10)
-
byroot (Jean Boussier)
-
Dan0042 (Daniel DeLorme)
-
Eregon (Benoit Daloze)
-
jeremyevans0 (Jeremy Evans)
-
mame (Yusuke Endoh)
-
matz (Yukihiro Matsumoto)
-
nobu (Nobuyoshi Nakada)
-
tompng (tomoya ishida)
-
vo.x (Vit Ondruch)
-
zverok (Victor Shepelev)