When is a str not a str?

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:

As 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

Answers (1)

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

Related Questions