doctest for methodfunction calls we re going the use these types for extra testing from collections import userlist from collections import userdict we re defining four helper functions from test import support def ea b printa b def fa k printa support sortdictk def gx y z printx y support sortdictz def hj1 a2 h3 printj a h argument list examples f f1 1 f1 2 1 2 f1 2 3 1 2 3 f1 2 3 4 5 1 2 3 4 5 f1 2 3 4 5 1 2 3 4 5 f1 2 3 4 5 1 2 3 4 5 f1 2 3 userlist4 5 1 2 3 4 5 f1 2 3 4 5 6 7 1 2 3 4 5 6 7 f1 2 3 4 5 6 7 1 2 3 4 5 6 7 fuserlist1 2 userlist3 4 5 userlist6 7 1 2 3 4 5 6 7 here we add keyword arguments f1 2 3 a 4 b 5 1 2 3 a 4 b 5 f1 2 a 1 b 5 a 4 c 6 traceback most recent call last typeerror test testextcall f got multiple values for keyword argument a f1 2 a 1 b 5 a4 c6 traceback most recent call last typeerror test testextcall f got multiple values for keyword argument a f1 2 a3 a 4 a 5 traceback most recent call last typeerror test testextcall f got multiple values for keyword argument a f1 2 3 4 5 a 6 b 7 1 2 3 4 5 a 6 b 7 f1 2 3 x4 y5 6 7 a 8 b 9 1 2 3 6 7 a 8 b 9 x 4 y 5 f1 2 3 4 5 c 8 a 6 b 7 1 2 3 4 5 a 6 b 7 c 8 f1 2 3 4 5 x6 y7 a 8 b 9 1 2 3 4 5 a 8 b 9 x 6 y 7 f1 2 3 userdicta4 b5 1 2 3 a 4 b 5 f1 2 3 4 5 userdicta6 b7 1 2 3 4 5 a 6 b 7 f1 2 3 x4 y5 6 7 userdicta8 b9 1 2 3 6 7 a 8 b 9 x 4 y 5 f1 2 3 4 5 x6 y7 userdicta8 b9 1 2 3 4 5 a 8 b 9 x 6 y 7 mix keyword arguments and dict unpacking d1 a 1 d2 c 3 fb2 d1 d2 a 1 b 2 c 3 fd1 b2 d2 a 1 b 2 c 3 fd1 d2 b2 a 1 b 2 c 3 fd1 b2 d2 d4 a 1 b 2 c 3 d 4 examples with invalid arguments typeerrors we re also testing the function names in the exception messages verify clearing of sf bug 733667 ec4 traceback most recent call last typeerror e got an unexpected keyword argument c g traceback most recent call last typeerror g missing 1 required positional argument x g traceback most recent call last typeerror g missing 1 required positional argument x g traceback most recent call last typeerror g missing 1 required positional argument x g1 1 g1 2 1 2 g1 2 3 1 2 3 g1 2 3 4 5 1 2 3 4 5 class nothing pass gnothing traceback most recent call last typeerror test testextcall g argument after must be an iterable not nothing class nothing def lenself return 5 gnothing traceback most recent call last typeerror test testextcall g argument after must be an iterable not nothing class nothing def lenself return 5 def getitemself i if i3 return i else raise indexerrori gnothing 0 1 2 class nothing def initself self c 0 def iterself return self def nextself if self c 4 raise stopiteration c self c self c 1 return c gnothing 0 1 2 3 check for issue 4806 does a typeerror in a generator get propagated with the right error message also check with other iterables def broken raise typeerrormyerror gbroken for i in range1 traceback most recent call last typeerror myerror grange1 broken for i in range1 traceback most recent call last typeerror myerror class brokeniterable1 def iterself raise typeerror myerror gbrokeniterable1 traceback most recent call last typeerror myerror grange1 brokeniterable1 traceback most recent call last typeerror myerror class brokeniterable2 def iterself yield 0 raise typeerror myerror gbrokeniterable2 traceback most recent call last typeerror myerror grange1 brokeniterable2 traceback most recent call last typeerror myerror class brokensequence def getitemself idx raise typeerror myerror gbrokensequence traceback most recent call last typeerror myerror grange1 brokensequence traceback most recent call last typeerror myerror make sure that the function doesn t stomp the dictionary d a 1 b 2 c 3 d2 d copy g1 d4 d 1 a 1 b 2 c 3 d 4 d d2 true what about willful misconduct def saboteurkw kw x m return kw d kw saboteura1 d d g1 2 3 x 4 y 5 traceback most recent call last typeerror g got multiple values for argument x f1 2 traceback most recent call last typeerror keywords must be strings h e 2 traceback most recent call last typeerror h got an unexpected keyword argument e hh traceback most recent call last typeerror test testextcall h argument after must be an iterable not function h1 h traceback most recent call last typeerror value after must be an iterable not function h1 h traceback most recent call last typeerror value after must be an iterable not function dirh traceback most recent call last typeerror dir argument after must be an iterable not function nothing none nothingh traceback most recent call last typeerror none argument after must be an iterable not function hh traceback most recent call last typeerror test testextcall h argument after must be a mapping not function h traceback most recent call last typeerror test testextcall h argument after must be a mapping not list ha1 h traceback most recent call last typeerror test testextcall h argument after must be a mapping not function ha1 traceback most recent call last typeerror test testextcall h argument after must be a mapping not list h a 1 h traceback most recent call last typeerror test testextcall h argument after must be a mapping not function h a 1 traceback most recent call last typeerror test testextcall h argument after must be a mapping not list dirh traceback most recent call last typeerror dir argument after must be a mapping not function nothingh traceback most recent call last typeerror none argument after must be a mapping not function dirb1 b 1 traceback most recent call last typeerror dir got multiple values for keyword argument b test a kwargs mapping with duplicated keys from collections abc import mapping class multidictmapping def initself items self items items def iterself return k for k v in self items def getitemself key for k v in self items if k key return v raise keyerrorkey def lenself return lenself items def keysself return k for k v in self items def valuesself return v for k v in self items def itemsself return k v for k v in self items gmultidict x 1 y 2 1 y 2 gmultidict x 1 x 2 traceback most recent call last typeerror test testextcall g got multiple values for keyword argument x ga3 multidict x 1 x 2 traceback most recent call last typeerror test testextcall g got multiple values for keyword argument x gmultidict a 3 multidict x 1 x 2 traceback most recent call last typeerror test testextcall g got multiple values for keyword argument x call with dict subtype class mydictdict pass def s1kwargs return kwargs def s2args kwargs return args kwargs def s3 n kwargs return n kwargs md mydict a 1 b 2 assert s1md a 1 b 2 assert s21 2 md 1 2 a 1 b 2 assert s3mydict n 1 b 2 1 b 2 s3md traceback most recent call last typeerror s3 missing 1 required keywordonly argument n another helper function def f2a b return a b d for i in range512 key kd i dkey i a b f21 2 3 d lena lenb b d 3 512 true class foo def methodself arg1 arg2 return arg1arg2 x foo foo methodx 1 2 3 foo methodx 1 2 3 foo method1 2 3 5 foo method1 2 3 5 a pycfunction that takes only positional parameters should allow an empty keyword dictionary to pass without a complaint but raise a typeerror if te dictionary is not empty try silence id1 true except false true id1 foo 1 traceback most recent call last typeerror id takes no keyword arguments a corner case of keyword dictionary items being deleted during the function call setup see http bugs python orgissue2016 class namestr def eqself other try del xself except keyerror pass return str eqself other def hashself return str hashself x namea 1 nameb 2 def fa b printa b fx 1 2 too many arguments def f pass f1 traceback most recent call last typeerror f takes 0 positional arguments but 1 was given def fa pass f1 2 traceback most recent call last typeerror f takes 1 positional argument but 2 were given def fa b1 pass f1 2 3 traceback most recent call last typeerror f takes from 1 to 2 positional arguments but 3 were given def f kw pass f1 kw3 traceback most recent call last typeerror f takes 0 positional arguments but 1 positional argument and 1 keywordonly argument were given def f kw b pass f1 2 3 b3 kw3 traceback most recent call last typeerror f takes 0 positional arguments but 3 positional arguments and 2 keywordonly arguments were given def fa b2 kw pass f2 3 4 kw4 traceback most recent call last typeerror f takes from 1 to 2 positional arguments but 3 positional arguments and 1 keywordonly argument were given too few and missing arguments def fa pass f traceback most recent call last typeerror f missing 1 required positional argument a def fa b pass f traceback most recent call last typeerror f missing 2 required positional arguments a and b def fa b c pass f traceback most recent call last typeerror f missing 3 required positional arguments a b and c def fa b c d e pass f traceback most recent call last typeerror f missing 5 required positional arguments a b c d and e def fa b4 c5 d5 pass fc12 b9 traceback most recent call last typeerror f missing 1 required positional argument a same with keyword only args def f w pass f traceback most recent call last typeerror f missing 1 required keywordonly argument w def f a b c d e pass f traceback most recent call last typeerror f missing 5 required keywordonly arguments a b c d and e
import doctest
import unittest

def load_tests(loader, tests, pattern):
    tests.addTest(doctest.DocTestSuite())
    return tests


if __name__ == '__main__':
    unittest.main()