@@ -346,29 +346,22 @@ def test_strip_md_extension_from_internal_links(self):
346346 expected = "See [Local](./local) and [External](https://example.com/page.md)"
347347 self .assertEqual (strip_md_extension_from_internal_links (text ), expected )
348348
349- def test_clean_runnable_blocks_basic (self ):
350- text = """```py runnable:test_basic
351- from transformers import AutoTokenizer
352-
353- tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
354- assert tokenizer is not None
355- output = tokenizer("Hello world")
356- assert "input_ids" in output
357- print(output)
349+ def test_clean_runnable_blocks_strips_annotation (self ):
350+ text = """```py runnable:test_clean
351+ from transformers import pipeline
352+ pipe = pipeline("sentiment-analysis")
353+ print(pipe("I love this!"))
358354```"""
359355 expected = """```py
360- from transformers import AutoTokenizer
361-
362- tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
363- output = tokenizer("Hello world")
364- print(output)
356+ from transformers import pipeline
357+ pipe = pipeline("sentiment-analysis")
358+ print(pipe("I love this!"))
365359```"""
366360 self .assertEqual (clean_runnable_blocks (text ), expected )
367361
368362 def test_clean_runnable_blocks_python_fence (self ):
369363 text = """```python runnable:test_python
370364x = 1
371- assert x == 1
372365print(x)
373366```"""
374367 expected = """```python
@@ -377,24 +370,36 @@ def test_clean_runnable_blocks_python_fence(self):
377370```"""
378371 self .assertEqual (clean_runnable_blocks (text ), expected )
379372
380- def test_clean_runnable_blocks_multiline_assert (self ):
381- text = """```py runnable:test_multi
382- result = do_something()
383- assert (
384- result.shape == (1, 10)
385- )
386- print(result)
373+ def test_clean_runnable_blocks_leaves_normal_blocks (self ):
374+ text = """```py
375+ x = 1 # nodoc
376+ print(x)
387377```"""
388- expected = """```py
389- result = do_something()
390- print(result)
378+ # Normal blocks without runnable: should be untouched
379+ self .assertEqual (clean_runnable_blocks (text ), text )
380+
381+ def test_clean_runnable_blocks_backticks_in_string (self ):
382+ """Triple backticks inside a string literal should not close the block early."""
383+ text = '''```py runnable:test_backticks
384+ x = """```
385+ not a fence
391386```"""
387+ print(x)
388+ ```'''
389+ expected = '''```py
390+ x = """```
391+ not a fence
392+ ```"""
393+ print(x)
394+ ```'''
392395 self .assertEqual (clean_runnable_blocks (text ), expected )
393396
394- def test_clean_runnable_blocks_no_asserts (self ):
395- text = """```py runnable:test_clean
397+ def test_clean_runnable_blocks_nodoc_single_line (self ):
398+ """A line marked with # nodoc is removed."""
399+ text = """```py runnable:test_nodoc
396400from transformers import pipeline
397401pipe = pipeline("sentiment-analysis")
402+ result = pipe("test") # nodoc
398403print(pipe("I love this!"))
399404```"""
400405 expected = """```py
@@ -404,60 +409,76 @@ def test_clean_runnable_blocks_no_asserts(self):
404409```"""
405410 self .assertEqual (clean_runnable_blocks (text ), expected )
406411
407- def test_clean_runnable_blocks_leaves_normal_blocks (self ):
408- text = """```py
409- assert x == 1
410- print(x)
411- ```"""
412- # Normal blocks without runnable: should be untouched
413- self .assertEqual (clean_runnable_blocks (text ), text )
414-
415- def test_clean_runnable_blocks_collapses_blank_lines (self ):
416- text = """```py runnable:test_blanks
417- x = 1
412+ def test_clean_runnable_blocks_nodoc_multiline_parens (self ):
413+ """A multi-line statement marked with # nodoc is fully removed."""
414+ text = """```py runnable:test_nodoc_multi
415+ result = compute()
418416
419- assert x == 1
417+ EXPECTED_OUTPUT = ( # nodoc
418+ "first value"
419+ + "second value"
420+ )
420421
421- y = 2
422+ print(result)
422423```"""
423424 expected = """```py
424- x = 1
425+ result = compute()
425426
426- y = 2
427+ print(result)
427428```"""
428429 self .assertEqual (clean_runnable_blocks (text ), expected )
429430
430- def test_clean_runnable_blocks_assert_with_parens (self ):
431- text = """```py runnable:test_parens
432- x = compute()
433- assert(x > 0)
431+ def test_clean_runnable_blocks_nodoc_multiline_brackets (self ):
432+ """Multi-line list with # nodoc tracked via bracket depth."""
433+ text = """```py runnable:test_nodoc_brackets
434+ x = do_work()
435+ expected = [ # nodoc
436+ 1,
437+ 2,
438+ 3,
439+ ]
434440print(x)
435441```"""
436442 expected = """```py
437- x = compute ()
443+ x = do_work ()
438444print(x)
439445```"""
440446 self .assertEqual (clean_runnable_blocks (text ), expected )
441447
442- def test_clean_runnable_blocks_for_loop_with_assert_only (self ):
443- """A for-loop whose body is only an assert should be removed entirely ."""
444- text = """```py runnable:test_for_assert
448+ def test_clean_runnable_blocks_nodoc_for_loop (self ):
449+ """A for-loop marked with # nodoc is removed with its body ."""
450+ text = """```py runnable:test_nodoc_for
445451inputs = prepare()
446452
447- for key in inputs:
448- assert torch.equal (inputs[key], inputs_transcription [key])
453+ for key in inputs: # nodoc
454+ do_something (inputs[key])
449455
450456outputs = model.generate(**inputs)
451457```"""
452458 expected = """```py
453459inputs = prepare()
454460
455461outputs = model.generate(**inputs)
462+ ```"""
463+ self .assertEqual (clean_runnable_blocks (text ), expected )
464+
465+ def test_clean_runnable_blocks_nodoc_collapses_blank_lines (self ):
466+ text = """```py runnable:test_blanks
467+ x = 1
468+
469+ y = 2 # nodoc
470+
471+ z = 3
472+ ```"""
473+ expected = """```py
474+ x = 1
475+
476+ z = 3
456477```"""
457478 self .assertEqual (clean_runnable_blocks (text ), expected )
458479
459480 def test_clean_runnable_blocks_glmasr_batched (self ):
460- """Real-world test from huggingface/transformers PR #44277 — test_batched block."""
481+ """Real-world test from huggingface/transformers PR #44277 — test_batched block with # nodoc ."""
461482 text = '''```py runnable:test_batched
462483import torch
463484from transformers import AutoProcessor, GlmAsrForConditionalGeneration
@@ -498,14 +519,14 @@ def test_clean_runnable_blocks_glmasr_batched(self):
498519 conversation, tokenize=True, add_generation_prompt=True, return_dict=True
499520).to(model.device, dtype=model.dtype)
500521
501- inputs_transcription = processor.apply_transcription_request(
522+ inputs_transcription = processor.apply_transcription_request( # nodoc
502523 [
503524 "https://huggingface.co/datasets/eustlb/audio-samples/resolve/main/bcn_weather.mp3",
504525 "https://huggingface.co/datasets/eustlb/audio-samples/resolve/main/obama2.mp3",
505526 ],
506527).to(model.device, dtype=model.dtype)
507528
508- for key in inputs:
529+ for key in inputs: # nodoc
509530 assert torch.equal(inputs[key], inputs_transcription[key])
510531
511532outputs = model.generate(**inputs, do_sample=False, max_new_tokens=500)
@@ -514,11 +535,11 @@ def test_clean_runnable_blocks_glmasr_batched(self):
514535 outputs[:, inputs.input_ids.shape[1] :], skip_special_tokens=True
515536)
516537
517- EXPECTED_OUTPUT = [
538+ EXPECTED_OUTPUT = [ # nodoc
518539 "Yesterday it was thirty five degrees in Barcelona, but today the temperature will go down to minus twenty degrees.",
519540 "This week, I traveled to Chicago to deliver my final farewell address to the nation.",
520541]
521- assert decoded_outputs == EXPECTED_OUTPUT
542+ assert decoded_outputs == EXPECTED_OUTPUT # nodoc
522543```'''
523544 expected = '''```py
524545import torch
@@ -560,110 +581,12 @@ def test_clean_runnable_blocks_glmasr_batched(self):
560581 conversation, tokenize=True, add_generation_prompt=True, return_dict=True
561582).to(model.device, dtype=model.dtype)
562583
563- inputs_transcription = processor.apply_transcription_request(
564- [
565- "https://huggingface.co/datasets/eustlb/audio-samples/resolve/main/bcn_weather.mp3",
566- "https://huggingface.co/datasets/eustlb/audio-samples/resolve/main/obama2.mp3",
567- ],
568- ).to(model.device, dtype=model.dtype)
569-
570584outputs = model.generate(**inputs, do_sample=False, max_new_tokens=500)
571585
572586decoded_outputs = processor.batch_decode(
573587 outputs[:, inputs.input_ids.shape[1] :], skip_special_tokens=True
574588)
575-
576- EXPECTED_OUTPUT = [
577- "Yesterday it was thirty five degrees in Barcelona, but today the temperature will go down to minus twenty degrees.",
578- "This week, I traveled to Chicago to deliver my final farewell address to the nation.",
579- ]
580- ```'''
581- self .assertEqual (clean_runnable_blocks (text ), expected )
582-
583- def test_clean_runnable_blocks_backticks_in_string (self ):
584- """Triple backticks inside a string literal should not close the block early."""
585- text = '''```py runnable:test_backticks
586- x = """```
587- not a fence
588- ```"""
589- assert x is not None
590- print(x)
591589```'''
592- expected = '''```py
593- x = """```
594- not a fence
595- ```"""
596- print(x)
597- ```'''
598- self .assertEqual (clean_runnable_blocks (text ), expected )
599-
600- def test_clean_runnable_blocks_nodoc_single_line (self ):
601- """A line marked with # nodoc is removed."""
602- text = """```py runnable:test_nodoc
603- from transformers import pipeline
604- pipe = pipeline("sentiment-analysis")
605- result = pipe("test") # nodoc
606- print(pipe("I love this!"))
607- ```"""
608- expected = """```py
609- from transformers import pipeline
610- pipe = pipeline("sentiment-analysis")
611- print(pipe("I love this!"))
612- ```"""
613- self .assertEqual (clean_runnable_blocks (text ), expected )
614-
615- def test_clean_runnable_blocks_nodoc_multiline_parens (self ):
616- """A multi-line statement marked with # nodoc is fully removed."""
617- text = """```py runnable:test_nodoc_multi
618- result = compute()
619-
620- EXPECTED_OUTPUT = [ # nodoc
621- "first value",
622- "second value",
623- ]
624- assert result == EXPECTED_OUTPUT
625-
626- print(result)
627- ```"""
628- expected = """```py
629- result = compute()
630-
631- print(result)
632- ```"""
633- self .assertEqual (clean_runnable_blocks (text ), expected )
634-
635- def test_clean_runnable_blocks_nodoc_for_loop (self ):
636- """A for-loop marked with # nodoc is removed with its body."""
637- text = """```py runnable:test_nodoc_for
638- inputs = prepare()
639-
640- for key in inputs: # nodoc
641- assert torch.equal(inputs[key], other[key])
642-
643- outputs = model.generate(**inputs)
644- ```"""
645- expected = """```py
646- inputs = prepare()
647-
648- outputs = model.generate(**inputs)
649- ```"""
650- self .assertEqual (clean_runnable_blocks (text ), expected )
651-
652- def test_clean_runnable_blocks_nodoc_multiline_brackets (self ):
653- """Multi-line list with # nodoc tracked via bracket depth."""
654- text = """```py runnable:test_nodoc_brackets
655- x = do_work()
656- expected = [ # nodoc
657- 1,
658- 2,
659- 3,
660- ]
661- print(x)
662- ```"""
663- expected = """```py
664- x = do_work()
665- print(x)
666- ```"""
667590 self .assertEqual (clean_runnable_blocks (text ), expected )
668591
669592 def test_clean_runnable_blocks_glmasr_basic (self ):
0 commit comments