Skip to content

Commit c1b8735

Browse files
authored
Fix off-by-one error on safety condition in while loop (#454)
* Fix off-by-one error on safety condition in while loop * Add test case Although it seems that the core issue may be elsewhere, as "transformers.training_args.__create_fn__.<locals>.__init__" is a bit unexpected.
1 parent 7a5bbe2 commit c1b8735

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/doc_builder/autodoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_shortest_path(obj, package):
7878
path_splits = long_path.split(".")
7979
module = package
8080
idx = module.__name__.count(".")
81-
while idx < len(path_splits) and not hasattr(module, short_name):
81+
while idx + 1 < len(path_splits) and not hasattr(module, short_name):
8282
idx += 1
8383
module = getattr(module, path_splits[idx])
8484
return ".".join(path_splits[: idx + 1]) + "." + long_name

tests/test_autodoc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
remove_example_tags,
3737
resolve_links_in_text,
3838
)
39-
from transformers import BertModel, BertTokenizer, BertTokenizerFast
39+
from transformers import BertModel, BertTokenizer, BertTokenizerFast, TrainingArguments
4040
from transformers.utils import PushToHubMixin
4141

4242

@@ -170,6 +170,10 @@ def test_get_shortest_path(self):
170170
self.assertEqual(get_shortest_path(BertModel, transformers), "transformers.BertModel")
171171
self.assertEqual(get_shortest_path(BertModel.forward, transformers), "transformers.BertModel.forward")
172172
self.assertEqual(get_shortest_path(PushToHubMixin, transformers), "transformers.utils.PushToHubMixin")
173+
self.assertEqual(
174+
get_shortest_path(TrainingArguments.__init__, transformers),
175+
"transformers.training_args.__create_fn__.<locals>.__init__",
176+
)
173177

174178
def test_get_type_name(self):
175179
self.assertEqual(get_type_name(str), "str")

0 commit comments

Comments
 (0)