Reputation: 1407
I have an issue in Python3 where a str
is not a str
. I'm working through a 2to3 project, and I'm getting a failure due to a type check in a third party class. For what it's worth this didn't happen under Python 2. The following example does not error but is similar to my issue:
class TheirClass():
# I don't love that they're using `id` here, but they are
def __init__(self, name, id=None):
if isinstance(name, str):
self.name = name
else:
raise TypeError('name is not a str')
my_object = TheirClass('a name')
print('Yep that worked')
I haven't yet been able to reduce this to a MCVE
, but I do know the following things:
str
into the constructor for TheirClass.name
TheirClass.__init__
isinstance(name, str) == True
TheirClass.__init__
isinstance(name, str) == False
TheirClass.__init__
type(name)
returns <class str>
TheirClass.__init__
dir(name) == dir(str)
inspect.getmro
reports that my pseudo-str doesn't have an __mro__
id
in their code I was able to see that id(str) != id(type(name))
id
of the name
object does not change between the two method callsAs far as I can tell nobody is redefining isinstance
or str
. But the id
of str
does change from outside constructor of TheirClass
to inside of it.
Upvotes: 2
Views: 234
Reputation: 1407
The final clue was that the id
of str
changed. While there was no assignment of str
or def str
there was an import. Specifically:
from past.builtins import str
Upvotes: 1