Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclassing from Modules #38780

Closed
malemburg opened this issue Jul 3, 2003 · 4 comments
Closed

Subclassing from Modules #38780

malemburg opened this issue Jul 3, 2003 · 4 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@malemburg
Copy link
Member

BPO 765228
Nosy @malemburg, @gvanrossum, @birkenfeld, @tiran

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/gvanrossum'
closed_at = <Date 2007-11-26.04:16:17.312>
created_at = <Date 2003-07-03.10:32:45.000>
labels = ['interpreter-core']
title = 'Subclassing from Modules'
updated_at = <Date 2007-11-26.04:16:17.311>
user = 'https://github.com/malemburg'

bugs.python.org fields:

activity = <Date 2007-11-26.04:16:17.311>
actor = 'gvanrossum'
assignee = 'gvanrossum'
closed = True
closed_date = <Date 2007-11-26.04:16:17.312>
closer = 'gvanrossum'
components = ['Interpreter Core']
creation = <Date 2003-07-03.10:32:45.000>
creator = 'lemburg'
dependencies = []
files = []
hgrepos = []
issue_num = 765228
keywords = []
message_count = 4.0
messages = ['16833', '55196', '57776', '57837']
nosy_count = 4.0
nosy_names = ['lemburg', 'gvanrossum', 'georg.brandl', 'christian.heimes']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue765228'
versions = ['Python 2.4']

@malemburg
Copy link
Member Author

In Python 2.2.3 there's a problem with accidental
subclassing from a Python module, e.g.

import MyStuff

class A(MyStuff):
     pass

this gives no error until you try to instantiate the
class:

o = A()

TypeError: 'module' object is not callable

In Python 2.3 the error is generated at module
startup time:

class A(MyStuff):
     pass
TypeError: function takes at most 2 arguments (3 given)

Since it is rather common that you create modules which
have the same name as their most important class, I
would find it more appropriate to raise a TypeError
with a message "can't subclass a module instance"
in both versions.

@malemburg malemburg added interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jul 3, 2003
@birkenfeld
Copy link
Member

In 2.5, the message says "module.__init__() takes at most 2 arguments (3
given)", which is at least a bit more specific.

You get similar errors when "deriving" from other arbitrary objects, so
I don't know if a special case makes sense here.

@tiran
Copy link
Member

tiran commented Nov 23, 2007

I agree. Python can't stop the developer from doing stupid things. We
could remove Py_TPFLAGS_BASETYPE from the module type but that could
cause incompatibilities with existing code.

I'm assigning the bug to our beloved dictator to ask for his opinion.

@gvanrossum
Copy link
Member

I don't see an issue to be fixed here; adding special tests in order to
provide more detailed error messages is rarely a good idea.

Also, PEP-8 has said for years now that modules should *not* be named
the same as classes. Yes, there are a few such modules in the standard
library. They're historical mistakes that will be fixed in 3.0.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

4 participants