Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate docs exampels for Python 3.10 and above #4339

Merged
merged 16 commits into from Aug 14, 2022

Conversation

Bobronium
Copy link
Contributor

@Bobronium Bobronium commented Aug 6, 2022

Change Summary

image

Embedding examples workflow is simplified:

Current New
```py
{!.tmp_examples/schema_main.py!}
```
_(This script is complete, it should run "as is")_

Outputs:

```json
{!.tmp_examples/schema_main.json!}
```
{!.tmp_examples/schema_main.md!}

docs/build/exec_examples.py will now:

  1. check if example uses all features of minimal supported version via pyupgrade
  2. create and add versions of the file with upgraded syntax that correspond to major python releases, if applicable
  3. add This script is complete, it should run "as is" if applicable
  4. add json output if applicable

Screenshots

Tabs
image image
Json output, single tab, incomplete script image

Related issue number

Closes #4334

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@Bobronium
Copy link
Contributor Author

Please review. I mostly need feedback on the approach and end result, rather than detailed review of code/style/etc.
Once I get confirmation that this path is applicable, I'll iron out the implementation and will rerequest full review.

@samuelcolvin
Copy link
Member

Without reviewing the code at all, I think the idea is great and the approach is great.

One note, I think I'll change how examples work a lot in V2, see dirty-equals for an example of what I want to do.

That shouldn't affect this, I'll just copy your code or reimplement the same approach when I'm doing the docs for V2.

@Bobronium
Copy link
Contributor Author

Ok, this should be ready. Plesase review.

I've cleaned up the implementation and allowed multiple future versions to be generated, e.g. Python 3.7, 3.9, 3.10, where it's applicable.

@Bobronium Bobronium marked this pull request as ready for review August 7, 2022 14:01
Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think this looks great 🎉, thanks so much.

A few small things, but my only significant suggestion is:

Could we change the default tab is the newest version? It seems this isn't possible directly in mkdocs, so options are:

  • reverse options so the newest version comes first - I think this is kind of fine???
  • use JS to change tab on page load, a bit 🤮 but might work
  • ask mkdocs to add this feature - could be slow

@@ -1,10 +1,10 @@
from __future__ import annotations
from typing import List # <-- List is defined in the module's global scope
from typing import Any, List
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is import for context, please revert.

Also better to use int to keep the meaning of the example as clear as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, but waiting for response in #4339 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced List and Any with HttpUrl and added a comment

docs/usage/models.md Outdated Show resolved Hide resolved
@@ -3,11 +3,12 @@


def this_is_broken():
# List is defined inside the function so is not in the module's
# Annotations are defined inside the function so is not in the module's
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to revert this, it was slightly clear to the layman.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it was changed is that List is removed by pyupgrade in 3.9+, so I added Any that shouldn't be removed in any case. Other option is to revert changes and add # dont-upgrade to the top of the file, but it might be unclear why there's no future versions in this example.

And the third way — rethink the whole example so it would fit both versions without confusion on any of them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think better to import one of the pydantic types like HttpUrl inside the function?

Copy link
Contributor Author

@Bobronium Bobronium Aug 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

I also called the function so prints get executed to showcase the error and absence of it.

docs/build/exec_examples.py Show resolved Hide resolved
@samuelcolvin
Copy link
Member

please update.

docs/requirements.txt Outdated Show resolved Hide resolved
@Bobronium Bobronium force-pushed the docs-3-10-examples branch 2 times, most recently from 7b17339 to 052ac76 Compare August 9, 2022 22:02
@Bobronium
Copy link
Contributor Author

Bobronium commented Aug 9, 2022

Could we change the default tab is the newest version? It seems this isn't possible directly in mkdocs, so options are:

Don't know, but the good thing is that mkdocs will remember preffered tab throught the docs, so maybe it's ok for now?

Please review. CI failed with mypy complaining since it can't find libraries used in docs.

My guess is that since mypy previously was used only on pydantic, which didn't have any hard dependencies, it didn't complain much nor required install anything.

I think mypy should really run in matrix with tests or in its own matrix (perhaps just for diffrenent python versions), since results may vary from installation to installation, but that's for another issue.

So options for now:

  1. install docs dependencies for linting
  2. add devtools and ansi2htm to linting dependencies
  3. don't run mypy on docs
  4. # type ignore the thing — this will cause unused type ignore comment errors locally

Suggestions?

@samuelcolvin
Copy link
Member

You just need to add new sections to setup.cfg like:

pydantic/setup.cfg

Lines 83 to 84 in 4fb872e

[mypy-toml]
ignore_missing_imports = true

Please update.

@Bobronium
Copy link
Contributor Author

Please review.

@samuelcolvin
Copy link
Member

this is really exciting 🎉, thanks so much for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs: have two examples versions: Python 3.6+ and 3.10+
4 participants