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

noreply@sourceforge.net noreply@sourceforge.net
Sun, 17 Nov 2002 09:57:54 -0800


Patches item #639139, was opened at 2002-11-15 17:25
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: Raymond Hettinger (rhettinger)
Date: 2002-11-17 12: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 12: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 09: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 18: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