As it informs you of the error, you cannot secure the attributes of many types of extensions implemented in C. (Other Python implementations may have different rules than CPython, but they often have similar limitations.)
A way to create a subclass is monkeypatch.
For example (untested, because I don't have pytest handy ... but it works with manually switching to monkey):
class patched_datetime(datetime.datetime): pass monkeypatch.setattr(patched_datetime, "now", nowfunc) datetime.datetime = patched_datetime
abarnert
source share