From 54366901b68b4f348bf0511eb0f7558776f52c32 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 15 Jan 2025 09:33:57 -0600 Subject: [PATCH] INTPYTHON-380 Add doctests (#177) --- .github/workflows/test-python.yml | 3 +- CONTRIBUTING.md | 2 - docs/Makefile | 153 ------------------------------ docs/conf.py | 2 +- docs/index.rst | 7 +- docs/requirements.txt | 2 - examples/wiki/wiki.py | 3 - flask_pymongo/__init__.py | 2 +- justfile | 10 +- 9 files changed, 16 insertions(+), 168 deletions(-) delete mode 100644 docs/Makefile delete mode 100644 docs/requirements.txt diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 65f11b0..f2d70d2 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -32,8 +32,9 @@ jobs: - uses: extractions/setup-just@v2 - run: just install - run: just lint - - run: just docs - run: uv run pre-commit run --hook-stage manual --all-files + - run: just docs + - run: just doctest build: runs-on: ${{ matrix.os }} strategy: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fe6d2e9..7531912 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,8 +29,6 @@ actual bugs and improvement requests. - All new features must include a test. Flask-PyMongo is tested against a matrix of all supported versions of Flask, PyMongo, and MongoDB, so tests ensure that your change works for all users. -- There is also a `style` build. Please ensure your code conforms to - Flask-PyMongo's style rules with `tox -e style` - Use [Sphinx](http://www.sphinx-doc.org/en/master/)-style docstrings diff --git a/docs/Makefile b/docs/Makefile deleted file mode 100644 index 894ee58..0000000 --- a/docs/Makefile +++ /dev/null @@ -1,153 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - -clean: - -rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Flask-PyMongo.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Flask-PyMongo.qhc" - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/Flask-PyMongo" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Flask-PyMongo" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." diff --git a/docs/conf.py b/docs/conf.py index 4fe8992..9ab2aa8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -30,7 +30,7 @@ # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named "sphinx.ext.*") or your custom ones. -extensions = ["sphinx.ext.intersphinx", "sphinx.ext.autodoc"] +extensions = ["sphinx.ext.intersphinx", "sphinx.ext.autodoc", "sphinx.ext.doctest"] # Add any paths that contain templates here, relative to this directory. templates_path = ["_templates"] diff --git a/docs/index.rst b/docs/index.rst index 9f986e1..5357915 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -65,9 +65,8 @@ versions. Flask-PyMongo is tested against `supported versions `_ of MongoDB, and Python -and 3.8+. For the exact list of version combinations that are tested and -known to be compatible, see the `envlist` in `tox.ini -`_. +and 3.9+. For the exact list of version combinations that are tested and +known to be compatible. Helpers @@ -110,7 +109,7 @@ constructor. These are passed directly through to the underlying By default, Flask-PyMongo sets the ``connect`` keyword argument to ``False``, to prevent PyMongo from connecting immediately. PyMongo itself `is not fork-safe - `_, + `_, and delaying connection until the app is actually used is necessary to avoid issues. If you wish to change this default behavior, pass ``connect=True`` as a keyword argument to ``PyMongo``. diff --git a/docs/requirements.txt b/docs/requirements.txt deleted file mode 100644 index 0e5b165..0000000 --- a/docs/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -Flask >= 0.8 -pymongo >= 2.1 diff --git a/examples/wiki/wiki.py b/examples/wiki/wiki.py index ed29fda..afb6068 100644 --- a/examples/wiki/wiki.py +++ b/examples/wiki/wiki.py @@ -89,7 +89,4 @@ def save_upload(filename: str) -> str | Response: if __name__ == "__main__": - import doctest - - doctest.testmod() app.run(debug=True) diff --git a/flask_pymongo/__init__.py b/flask_pymongo/__init__.py index 03ebb81..a27abb5 100644 --- a/flask_pymongo/__init__.py +++ b/flask_pymongo/__init__.py @@ -109,7 +109,7 @@ def init_app(self, app: Flask, uri: str | None = None, *args: Any, **kwargs: Any database_name = parsed_uri["database"] # Try to delay connecting, in case the app is loaded before forking, per - # http://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe + # https://www.mongodb.com/docs/languages/python/pymongo-driver/current/faq/#is-pymongo-fork-safe- kwargs.setdefault("connect", False) if DriverInfo is not None: kwargs.setdefault("driver", DriverInfo("Flask-PyMongo", __version__)) diff --git a/justfile b/justfile index e5fc302..ca10047 100644 --- a/justfile +++ b/justfile @@ -1,3 +1,6 @@ +docs_build := "docs/_build" +sphinx_opts:= "-d " + docs_build + "/doctrees docs" + # Default target executed when no arguments are given. [private] default: @@ -15,7 +18,12 @@ lint: uv run pre-commit run ruff-format --files docs: - uv run sphinx-build -T -b html docs docs/_build + uv run sphinx-build -T -b html {{sphinx_opts}} {{docs_build}} + +doctest: + uv run python -m doctest -v examples/wiki/wiki.py + uv run sphinx-build -E -b doctest {{sphinx_opts}} {{docs_build}}/doctest + uv run sphinx-build -b linkcheck {{sphinx_opts}} {{docs_build}}/linkcheck typing: uv run mypy --install-types --non-interactive .