[Python-bugs-list] [ python-Bugs-563060 ] Module can be used as a base class

noreply@sourceforge.net noreply@sourceforge.net
Fri, 29 Nov 2002 12:22:55 -0800


Bugs item #563060, was opened at 2002-05-31 21:41
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=563060&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Closed
Resolution: Fixed
Priority: 5
Submitted By: Neal Norwitz (nnorwitz)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Module can be used as a base class

Initial Comment:
>From python-dev:

> But this is not what I would expect:
> 
>       >>> import string
>       >>> class newstr(string): pass
>       ... 
>       # i would have expected this to raise a TypeError
>       >>> x = newstr()
>       Traceback (most recent call last):
>         File "<stdin>", line 1, in ?
>       TypeError: 'module' object is not callable
> 
> Perhaps this error should be handled when the class
is constructed
> rather than when instantiating an object?

Guido says "the bug is that you can use any module as a
base class"

----------------------------------------------------------------------

>Comment By: Just van Rossum (jvr)
Date: 2002-11-29 21:22

Message:
Logged In: YES 
user_id=92689

While I'm glad this is fixed, the error message for this
fairly common newbie error is rather obscure:

>>> import UserDict  
>>> class MyDict(UserDict): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: function takes at most 2 arguments (3 given)
>>> 

I have no idea if this is fixable at all.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-06-04 07:56

Message:
Logged In: YES 
user_id=6380

This can't be fixed in 2.2.x without changing the module
constructor signature, which I think is too much of an
incompatibility to risk. (Hey, someone might even have a use
fora module as a base class. :-)


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-06-04 07:55

Message:
Logged In: YES 
user_id=6380

Fixed. The module type's constructor now takes a name and an
optional docstring argument (just like the C constructor
PyModule_New()), and nothing else.

Indeed, new.module can now be changed to point to the module
type.

----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2002-06-01 07:30

Message:
Logged In: YES 
user_id=12800

Note that this bug exists in Python 2.2.1 also.

----------------------------------------------------------------------

Comment By: Phillip J. Eby (pje)
Date: 2002-05-31 22:27

Message:
Logged In: YES 
user_id=56214

More specifically, ModuleType's constructor accepts and
ignores a (name,bases,dict) call signature.  Other objects
will fail at class construction time like this:

>>> class d({}): pass

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    class d({}): pass
TypeError: dict() takes at most 1 argument (3 given)
>>> class n(1): pass

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in ?
    class n(1): pass
TypeError: int() takes at most 2 arguments (3 given)
>>> class s("s"): pass

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in ?
    class s("s"): pass
TypeError: str() takes at most 1 argument (3 given)
>>> 

Since the module constructor doesn't actually need any
arguments, it should complain the same way the above items do.

Interestingly, while type(module) doesn't complain when
called with excess arguments, the function 'new.module'
does.  Ironically, the docstring for new.module seems to
imply it takes an argument.  Perhaps new.module should
simply refer to type(module), once the latter is fixed.

Another, far more radical fix for this bug would be to
change the constructor to accept the (name,bases,dict)
signature, thus making it possible to subclass a module and
have it work correctly, for some value of "correctly".  :)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=563060&group_id=5470