Skip to content Skip to sidebar Skip to footer

Can Not Assert Type Of An Object?

Why this source... ''' [...] ''' # Import the standard date and time system. from datetime import datetime as dt # Ommited the remaining imports section class CuteClass(object)

Solution 1:

Rather than comparing to type (which you certainly shouldn't do as a string!), use isinstance. Also, you shouldn't use assert like that, try something like:

ifnotisinstance(date, dt): # note you have aliased datetime.datetimeraise TypeError(...)

Solution 2:

I asked the duck and then I told the duck I should do

assert (str(type(date)) == "<class 'datetime.datetime'>",
                'assertion failed creating a CuteClass object')

instead of

assert (type(date) == "<class 'datetime.datetime'>",
                'assertion failed creating a CuteClass object')

Post a Comment for "Can Not Assert Type Of An Object?"