Skip to content

Commit 2120324

Browse files
committed
Use #: for RBS annotations
I have annotated signatures since the first version of Zeitwerk. For me, they are an essential aid to understand and use the code, both public and private. In the case of Zeitwerk, the API is described in English in the README. These signatures are mostly internal documentation for readers of the code. You need to know what a method accepts, returns, yields, and what may raise. And that is the case regardless of the dynamic or static nature of your type system. At the beginning I used my own notation, then adopted RBS, and now a consensus towards using #: as a marker seems to be taking shape. This pull request to Sorbet sorbet/sorbet#8470 inspired me to join this trend.
1 parent a1d4f54 commit 2120324

23 files changed

+177
-171
lines changed

lib/zeitwerk.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Zeitwerk
1919
# This is a dangerous method.
2020
#
2121
# @experimental
22-
# @sig () -> void
22+
#: () -> void
2323
def self.with_loader
2424
loader = Zeitwerk::Loader.new
2525
yield loader

lib/zeitwerk/core_ext/kernel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class << self
1919
alias_method :zeitwerk_original_require, :require
2020
end
2121

22-
# @sig (String) -> true | false
22+
#: (String) -> bool
2323
def require(path)
2424
if loader = Zeitwerk::Registry.autoloads.registered?(path)
2525
if path.end_with?(".rb")

lib/zeitwerk/core_ext/module.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module Zeitwerk::ConstAdded # :nodoc:
4-
# @sig (Symbol) -> void
4+
#: (Symbol) -> void
55
def const_added(cname)
66
if loader = Zeitwerk::Registry.explicit_namespaces.loader_for(self, cname)
77
namespace = const_get(cname, false)

lib/zeitwerk/cref.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,69 @@
22

33
# This private class encapsulates pairs (mod, cname).
44
#
5-
# Objects represent the constant cname in the class or module object mod, and
6-
# have API to manage them. Examples:
5+
# Objects represent the constant `cname` in the class or module object `mod`,
6+
# and have API to manage them. Examples:
77
#
88
# cref.path
99
# cref.set(value)
1010
# cref.get
1111
#
12-
# The constant may or may not exist in mod.
12+
# The constant may or may not exist in `mod`.
1313
class Zeitwerk::Cref
1414
require_relative "cref/map"
1515

1616
include Zeitwerk::RealModName
1717

18-
# @sig Module
18+
#: Module
1919
attr_reader :mod
2020

21-
# @sig Symbol
21+
#: Symbol
2222
attr_reader :cname
2323

2424
# The type of the first argument is Module because Class < Module, class
2525
# objects are also valid.
2626
#
27-
# @sig (Module, Symbol) -> void
27+
#: (Module, Symbol) -> void
2828
def initialize(mod, cname)
2929
@mod = mod
3030
@cname = cname
3131
@path = nil
3232
end
3333

34-
# @sig () -> String
34+
#: () -> String
3535
def path
3636
@path ||= Object.equal?(@mod) ? @cname.name : "#{real_mod_name(@mod)}::#{@cname.name}".freeze
3737
end
3838
alias to_s path
3939

40-
# @sig () -> String?
40+
#: () -> String?
4141
def autoload?
4242
@mod.autoload?(@cname, false)
4343
end
4444

45-
# @sig (String) -> bool
45+
#: (String) -> nil
4646
def autoload(abspath)
4747
@mod.autoload(@cname, abspath)
4848
end
4949

50-
# @sig () -> bool
50+
#: () -> bool
5151
def defined?
5252
@mod.const_defined?(@cname, false)
5353
end
5454

55-
# @sig (top) -> top
55+
#: (top) -> top
5656
def set(value)
5757
@mod.const_set(@cname, value)
5858
end
5959

6060
# @raise [NameError]
61-
# @sig () -> top
61+
#: () -> top
6262
def get
6363
@mod.const_get(@cname, false)
6464
end
6565

6666
# @raise [NameError]
67-
# @sig () -> void
67+
#: () -> void
6868
def remove
6969
@mod.__send__(:remove_const, @cname)
7070
end

lib/zeitwerk/cref/map.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,44 +69,45 @@
6969
# unnecessary cref objects for constants we do not manage (but we do not know in
7070
# advance there).
7171
class Zeitwerk::Cref::Map # :nodoc: all
72+
#: () -> void
7273
def initialize
7374
@map = {}
7475
@map.compare_by_identity
7576
@mutex = Mutex.new
7677
end
7778

78-
# @sig (Zeitwerk::Cref, V) -> V
79+
#: (Zeitwerk::Cref, top) -> top
7980
def []=(cref, value)
8081
@mutex.synchronize do
8182
cnames = (@map[cref.mod] ||= {})
8283
cnames[cref.cname] = value
8384
end
8485
end
8586

86-
# @sig (Zeitwerk::Cref) -> top?
87+
#: (Zeitwerk::Cref) -> top
8788
def [](cref)
8889
@mutex.synchronize do
8990
@map[cref.mod]&.[](cref.cname)
9091
end
9192
end
9293

93-
# @sig (Zeitwerk::Cref, { () -> V }) -> V
94+
#: (Zeitwerk::Cref, { () -> top }) -> top
9495
def get_or_set(cref, &block)
9596
@mutex.synchronize do
9697
cnames = (@map[cref.mod] ||= {})
9798
cnames.fetch(cref.cname) { cnames[cref.cname] = block.call }
9899
end
99100
end
100101

101-
# @sig (Zeitwerk::Cref) -> top?
102+
#: (Zeitwerk::Cref) -> top
102103
def delete(cref)
103104
delete_mod_cname(cref.mod, cref.cname)
104105
end
105106

106107
# Ad-hoc for loader_for, called from const_added. That is a hot path, I prefer
107108
# to not create a cref in every call, since that is global.
108109
#
109-
# @sig (Module, Symbol) -> top?
110+
#: (Module, Symbol) -> top
110111
def delete_mod_cname(mod, cname)
111112
@mutex.synchronize do
112113
if cnames = @map[mod]
@@ -117,7 +118,7 @@ def delete_mod_cname(mod, cname)
117118
end
118119
end
119120

120-
# @sig (top) -> void
121+
#: (top) -> void
121122
def delete_by_value(value)
122123
@mutex.synchronize do
123124
@map.delete_if do |mod, cnames|
@@ -129,7 +130,7 @@ def delete_by_value(value)
129130

130131
# Order of yielded crefs is undefined.
131132
#
132-
# @sig () { (Zeitwerk::Cref) -> void } -> void
133+
#: () { (Zeitwerk::Cref) -> void } -> void
133134
def each_key
134135
@mutex.synchronize do
135136
@map.each do |mod, cnames|
@@ -140,14 +141,14 @@ def each_key
140141
end
141142
end
142143

143-
# @sig () -> void
144+
#: () -> void
144145
def clear
145146
@mutex.synchronize do
146147
@map.clear
147148
end
148149
end
149150

150-
# @sig () -> bool
151+
#: () -> bool
151152
def empty? # for tests
152153
@mutex.synchronize do
153154
@map.empty?

lib/zeitwerk/error.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Error < StandardError
55
end
66

77
class ReloadingDisabledError < Error
8+
#: () -> void
89
def initialize
910
super("can't reload, please call loader.enable_reloading before setup")
1011
end
@@ -14,6 +15,7 @@ class NameError < ::NameError
1415
end
1516

1617
class SetupRequired < Error
18+
#: () -> void
1719
def initialize
1820
super("please, finish your configuration and call Zeitwerk::Loader#setup once all is ready")
1921
end

lib/zeitwerk/gem_inflector.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
module Zeitwerk
44
class GemInflector < Inflector
5-
# @sig (String) -> void
5+
#: (String) -> void
66
def initialize(root_file)
77
namespace = File.basename(root_file, ".rb")
88
root_dir = File.dirname(root_file)
99
@version_file = File.join(root_dir, namespace, "version.rb")
1010
end
1111

12-
# @sig (String, String) -> String
12+
#: (String, String) -> String
1313
def camelize(basename, abspath)
1414
abspath == @version_file ? "VERSION" : super
1515
end

lib/zeitwerk/gem_loader.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class GemLoader < Loader
1010
private_class_method :new
1111

1212
# @private
13-
# @sig (String, bool) -> Zeitwerk::GemLoader
13+
#: (String, namespace: Module, warn_on_extra_files: boolish) -> Zeitwerk::GemLoader
1414
def self.__new(root_file, namespace:, warn_on_extra_files:)
1515
new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
1616
end
1717

18-
# @sig (String, bool) -> void
18+
#: (String, namespace: Module, warn_on_extra_files: boolish) -> void
1919
def initialize(root_file, namespace:, warn_on_extra_files:)
2020
super()
2121

@@ -30,15 +30,15 @@ def initialize(root_file, namespace:, warn_on_extra_files:)
3030
push_dir(@root_dir, namespace: namespace)
3131
end
3232

33-
# @sig () -> void
33+
#: () -> void
3434
def setup
3535
warn_on_extra_files if @warn_on_extra_files
3636
super
3737
end
3838

3939
private
4040

41-
# @sig () -> void
41+
#: () -> void
4242
def warn_on_extra_files
4343
expected_namespace_dir = @root_file.delete_suffix(".rb")
4444

lib/zeitwerk/inflector.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Inflector
1111
#
1212
# Takes into account hard-coded mappings configured with `inflect`.
1313
#
14-
# @sig (String, String) -> String
14+
#: (String, String) -> String
1515
def camelize(basename, _abspath)
1616
overrides[basename] || basename.split('_').each(&:capitalize!).join
1717
end
@@ -28,7 +28,7 @@ def camelize(basename, _abspath)
2828
# inflector.camelize("mysql_adapter", abspath) # => "MySQLAdapter"
2929
# inflector.camelize("users_controller", abspath) # => "UsersController"
3030
#
31-
# @sig (Hash[String, String]) -> void
31+
#: (Hash[String, String]) -> void
3232
def inflect(inflections)
3333
overrides.merge!(inflections)
3434
end
@@ -38,7 +38,7 @@ def inflect(inflections)
3838
# Hard-coded basename to constant name user maps that override the default
3939
# inflection logic.
4040
#
41-
# @sig () -> Hash[String, String]
41+
#: () -> Hash[String, String]
4242
def overrides
4343
@overrides ||= {}
4444
end

lib/zeitwerk/internal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# This is a private module.
44
module Zeitwerk::Internal
5-
# @sig (Symbol) -> void
5+
#: (Symbol) -> void
66
def internal(method_name)
77
private method_name
88

0 commit comments

Comments
 (0)