error masking / misleading in certain cases with TypeError

Issue #1236 resolved
Former user created an issue

Today I got a little too zealous in remodeling my schema and I without noticing the problem, I renamed a table to have the same identifier as its mapped class. The next time I tried to initialize my model, I got the following traceback: (I'll post just the tail of it, where control moves from Pylons/paste into sqlalchemy).

File "H:\project\rentals\rentals\config\environment.py", line 41, in load_environment init_model(engine) File "H:\project\rentals\rentals\model_init__.py", line 291, in init_model 'feature':relation(Feature, uselist=False)}) File "h:\python25\lib\site-packages\sqlalchemy-0.5.0rc4-py2.5.egg\sqlalchemy\orm__init__.py", line 670, in mapper return Mapper(class, local_table, args, *params) File "h:\python25\lib\site-packages\sqlalchemy-0.5.0rc4-py2.5.egg\sqlalchemy\orm\mapper.py", line 137, in init if not issubclass(class_, object): TypeError: issubclass() arg 1 must be a class

Here is the mapper involved: mapper(SFV, tables'SFV', properties={ 'sku':relation(Sku, uselist=False), 'feature':relation(Feature, uselist=False)})

Very very simple in this case. The classes SFV, Sku, and Feature were all defined properly at the time. But what I was entirely forgetting, since I hadn't needed to think about it for months, is that I used one of those evil globals() tricks to save a lot of typing when reflecting my tables:

for t in meta.metadata.tables.items():
    globals()[t[0](t[0)] = t[1](1)

...which meant that all of my table names are entered in as global symbols, right? I might want to change that now that it bit me in the ass.

This failure to initialize my model gave me fits for a while because the error suggested that I had made a mistake somewhere that was causing the class 'Feature' to not respond to type-sniffing or something else in that arcane direction. Worse yet, every about 8th or 10th run of the interpreter (even without changing my model file) it crashed threading.py instead! That was a new one on me.

My fault entirely, but I just wanted to suggest the following minor enhancement to save future cycles for others. I see the signature for Mapper is: Mapper(class_, local_table, args, *params).

You could just type-check the class_ argument to make sure that it is in fact a class and not something else. That's assuming my theory is right, that what crashed in my case, this morning, is that the same identifier referred to the table instead of the class (SFV).

I don't know if this falls into the category of "we can't check for every stupid basic-python-101 mistake a developer might try to make". You be the judge.

Comments (3)

  1. Log in to comment