[Patches] [ python-Patches-639139 ] Remove type-check from urllib2

SourceForge.net noreply@sourceforge.net
Thu, 08 May 2003 18:32:34 -0700


Patches item #639139, was opened at 2002-11-15 14:25
Message generated for change (Comment added) made by bcannon
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=639139&group_id=5470

Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: John J Lee (jjlee)
Assigned to: Nobody/Anonymous (nobody)
Summary: Remove type-check from urllib2

Initial Comment:
Remove undesirable type-checking assertion from 
urllib2.Request.


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

>Comment By: Brett Cannon (bcannon)
Date: 2003-05-08 18:32

Message:
Logged In: YES 
user_id=357491

It's not so much that the dummy attribute would break anything so much as 
it is just unneeded.  If you want to make sure an object is not going to meet 
your required API you either just follow EAFP (Easier to Ask Forgiveness than 
Permission) or you explicitly test for the required interface.  There is no good 
argument to have to flag an object to say that it meets an API spec.

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

Comment By: John J Lee (jjlee)
Date: 2002-11-17 13:21

Message:
Logged In: YES 
user_id=261020

Why not a new attribute?  What would it break?

Checking for the interface by checking all the methods
(there are maybe ten of them) is not really practical,
and really it's the intent that's the important bit.


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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-17 09:57

Message:
Logged In: YES 
user_id=80475

I see no problem with weakening the assertion, but 
hasattr should check for a required part of the interface 
instead of a new, undocumented, dummy attribute.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-17 09:55

Message:
Logged In: YES 
user_id=80475

I see no problem with weakening the assertion, but 
hasattr should check for a required part of the interface 
instead of a new, undocumented, dummy attribute.

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

Comment By: John J Lee (jjlee)
Date: 2002-11-17 06:46

Message:
Logged In: YES 
user_id=261020

It's widely regarded as a bug if Python code checks for type 
with
isinstance (or type(foo) == type(bar)) without some good 
reason.
It's plausible that you may want to make an object that
implements the Request interface without deriving from 
Request
(say, I don't know, to implement the frobozz URI scheme, 
which
requires ordered headers, and never has any data associated 
with
it).  If so, you don't want to have to follow 'bug fixes' in 
the
Python std. library that may break your code simply because 
you
had to derive from Request to satisfy the assertion.  I 
might
have done this when I wrote a couple of modules that build 
on
urllib2, actually.  I'm not sure whether that would have 
been the
best way, because I didn't think about it since I didn't 
have any
choice in the matter, thanks to this assertion!

OTOH, it's true that removing type-checks can break 
backwards
compatibility.  However, this is an assertion, not a real 
runtime
type-check, so it won't break backwards compatibility: if 
people
are relying on catching AssertionError to do type-checking 
in
their own code, that's their problem!

The docs say:

urlopen(url[, data])
 Open the URL url, which can be either a string or a Request
 object (currently the code checks that it really is a 
Request
 instance, or an instance of a subclass of Request).


Note the 'currently' (and the source code comment indicating 
that
what we really want to check is the interface), and that 
fact
that the code *doesn't* actually check it, but only asserts.

Request interface is already documented, so there's no 
problem
there.


John


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

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-15 15:14

Message:
Logged In: YES 
user_id=33168

John, could you explain why you need it and what is the benefit?

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

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