Can't inspect code from an object in Python

Viewed 187

Here is the beginning of my code:

import os
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

bootstrap = Bootstrap(app)
moment = Moment(app)
db = SQLAlchemy(app)

a=db.Model

class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role')

    def __repr__(self):
        return '<Role %r>' % self.name

As i do not see an init method in Role, i suppose it is in parent class db.Model. So i wanted to see how this class is defined. I created the object named 'a' in the upper script and in a python terminal run:

In [11]: inspect.getmodule(a)
Out[11]: <module 'sqlalchemy.ext.declarative.api' from 'c:\\users\\laurent\\google drive\\python\\venv\\lib\\site-packages\\sqlalchemy\\ext\\declarative\\api.py'>

In [12]: inspect.getsource(a)
...
C:\Python35\lib\inspect.py in findsource(object)
    785             return lines, candidates[0][1]
    786         else:
--> 787             raise OSError('could not find class definition')
    788
    789     if ismethod(object):

OSError: could not find class definition

Why do i get this error ? The class seemed to come from a python script (not C code). Moreover in bonus if someone could provide me the __init__method of the parent class it would be wonderful!

Thanks (i am autodicact in code so any advices to inspect/debug code would be interesting, perhaps an other dedicated question...).

1 Answers

So I'm not entirely sure what's going on - the exacts of the inspect module are pretty complex.

However I think one thing that might be causing issues is your querying an instance of a class: a and not the actual class.

If you do query the class you get:

>>> t = type(a)
>>> t
flask_sqlalchemy.model.DefaultMeta

>>> inspect.getsourcefile(t)
'...\\site-packages\\flask_sqlalchemy\\model.py'

>>>  inspect.getsource(t)
'class DefaultMeta(NameMetaMixin, BindMetaMixin, DeclarativeMeta):\n    pass\n'

So it seems to make sense that inspecting the class returns the appropriate source file. What I don't understand is why does inspecting an instance of the class return `'...\site-packages\sqlalchemy\ext\declarative\api.py'.

If it doesn't return the file containing the class definition, I would expect it to return the file where it's instantiated, but that would be the script that you run not this api.py file.

Related