0

I'm trying to run pydoc on a Python file using my Windows machine. The file contains SQLAlchemy models, but have been struggling with an error.

I appreciate it if I can receive some guidance. Attached below: the command that I execute, the target Python file and the error that I'm getting.

My command is as follows: python -m pydoc -w objects

Here is my Python file, objects.py.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.app_context().push()
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:mycoolpassword@localhost:5432/mydb'
db = SQLAlchemy(app)
class Book(db.Model):
 """
 Docstring for my class
 Col1
 Col2
 """
 __tablename__ = 'mytable'
 
 col1 = db.Column(db.Integer, primary_key = True)
 col2 = db.Column(db.Integer)
db.drop_all()
db.create_all()

Here is the error that I'm receiving.

$ python -m pydoc -w objects
Traceback (most recent call last):
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
 return _run_code(code, main_globals, None,
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
 exec(code, run_globals)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 2744, in <module>
 cli()
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 2704, in cli
 writedoc(arg)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 1697, in writedoc
 page = html.page(describe(object), html.document(object, name))
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 380, in document
 if inspect.ismodule(object): return self.docmodule(*args)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 738, in docmodule
 contents.append(self.document(value, key, name, fdict, cdict))
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 381, in document
 if inspect.isclass(object): return self.docclass(*args)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 843, in docclass
 for name, kind, cls, value in classify_class_attrs(object)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 204, in classify_class_attrs
 for (name, kind, cls, value) in inspect.classify_class_attrs(object):
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\inspect.py", line 443, in classify_class_attrs
 srch_obj = getattr(srch_cls, name, None)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_sqlalchemy\model.py", line 30, in __get__
 return cls.query_class(
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\orm\query.py", line 273, in __init__
 self._set_entities(entities)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\orm\query.py", line 282, in _set_entities
 self._raw_columns = [
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\orm\query.py", line 283, in <listcomp>
 coercions.expect(
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 413, in expect
 resolved = impl._literal_coercion(
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 651, in _literal_coercion
 self._raise_for_expected(element, argname)
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 1139, in _raise_for_expected
 return super()._raise_for_expected(
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 710, in _raise_for_expected
 super()._raise_for_expected(
 File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 535, in _raise_for_expected
 raise exc.ArgumentError(msg, code=code) from err
sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other columns clause element expected, got <class 'sqlalchemy.orm.decl_api.Model'>.

I tried to reinstall flask and SQLAlchemy and ensure that all dependencies such as psycopg2-binary are all installed.

asked Mar 28, 2024 at 13:34
4
  • pydoc imports files to inspect them, so any top-level code in the file will be executed. The traceback is consistent with an invalid SQLAlchemy query being exwcuted. Is the example file really what is being documented? Commented Mar 28, 2024 at 15:30
  • Yes, it's in fact the example. It successfully runs but the issue is when I ran pydoc. Before I posted, I only changed some parameters to the URL such as password and DB name. Commented Mar 28, 2024 at 15:48
  • If I run pydoc on a file containing the above code it prints 'No Python documentation found for <filename>'', some boilerplate about help() then 'shell returned 1'. Does your module impor tother modules by any chance, which could have queries in their top-level code? Commented Mar 28, 2024 at 15:52
  • Thank you @snakecharmerb. To my knowledge, my module does not import other modules other than the ones listed at the top of the file. I'm afraid this is a bug in pydoc. Not sure how to report it though. Commented Mar 29, 2024 at 14:17

1 Answer 1

0

It seems that pydoc has a bug when it comes to Python files with DB models. I ended up using pdoc and it generated the documentation with no issues.

pip install pdoc
pdoc objects.py
answered Mar 29, 2024 at 14:34
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.