Old question, but maybe others have seen this and wondered the same thing. Fairly simple answer.
In your factory method, you do not call out the namespace:
obj = MyClass(5);
Needs to be:
obj = mypackage.MyClass(5);
The import is in your test script, not your class. Import statements are extremely localized, so the factory method does not know about a MyClass, only a mypackage.MyClass.