r/ProgrammerHumor • • 1d ago

Meme readTheDocumentation

Post image
8.5k Upvotes

129 comments sorted by

View all comments

Show parent comment

2

u/DezXerneas 14h ago

Wait, I don't get how it works, could you explain a bit?

2

u/Muhznit 10h ago

Actually that's my bad, python's documentation has a much better set of examples.

Basically the idea is that in whatever docstring that you usually add to a function for documentation, you include some special syntax to illustrate an example of how to use that function, you have some tool to extract and run that example. Thus, you become able to catch outdated documentation as a part of your build process.

It's easily one of my favorite things about Python. I can set up some quick-and-dirty tiny functional unit test and keep it in version control with the rest of the code. Personally I like writing my doctests similar to this:

#!/usr/bin/env python3                                                                                                                                                                                                                
import doctest                                                                                                                                                                                                                        
                                                                                                                                                                                                                                      
def is_this_good(thing: str) -> bool:                                                                                                                                                                                                 
    """                                                                                                                                                                                                                               
    Checks if the thing is a good thing.                                                                                                                                                                                              
    Doctests are good.                                                                                                                                                                                                                
    >>> assert is_this_good("doctests")                                                                                                                                                                                               
                                                                                                                                                                                                                                      
    Facism is not.                                                                                                                                                                                                                    
    >>> assert is_this_good("facism") == False                                                                                                                                                                                        
                                                                                                                                                                                                                                      
    Being able to drop into the python debugger when an assertion fails from a                                                                                                                                                        
    doctest is cool, but not something to include in CI/CD                                                                                                                                                                            
    >>> some_bullshit_condition = False                                                                                                                                                                                               
    >>> assert some_bullshit_condition, breakpoint()                                                                                                                                                                                  
    """                                                                                                                                                                                                                               
    good_things = {"doctests", "puppies", "boobs", "world peace"}                                                                                                                                                                     
    return thing in good_things                                                                                                                                                                                                       
                                                                                                                                                                                                                                      
def main():                                                                                                                                                                                                                           
    doctest.testmod()                                                                                                                                                                                                                 
                                                                                                                                                                                                                                      
if __name__ == "__main__":                                                                                                                                                                                                            
    main()

2

u/DezXerneas 6h ago

Oh, so functionally you're just writing your tests directly in your code without a tests.py for the basic stuff. That's really cool, I think I might start integrating it into my codebases too.