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

Updates find_examples to support alternate docstring style. #7

Merged
merged 2 commits into from
Apr 5, 2023
Merged

Updates find_examples to support alternate docstring style. #7

merged 2 commits into from
Apr 5, 2023

Conversation

donovandicks
Copy link
Contributor

Adds support for docstrings without a line break directly after the triple quotes.

I found this plugin and wanted to use it for my own projects but noticed an issue with example discovery when the docstrings have text on the first line following the initial triple quotes like this:

def func():
    """Does some things.
    ```py
    assert True
    ```
    """

This PR updates the regex in find_examples to capture this style of docstring as well as the original. Included updates to tests to capture the change.

Now supports docstrings without a line break directly after the triple
quotes.
@donovandicks donovandicks changed the title Fixes find examples to support alternate docstring style. Updates find_examples to support alternate docstring style. Apr 3, 2023
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.

Otherwise LGTM.

@@ -140,7 +140,7 @@ def find_examples(*paths: str | Path, skip: bool = False) -> Iterable[CodeExampl
group = uuid4()
if path.suffix == '.py':
code = path.read_text('utf-8')
for m_docstring in re.finditer(r'(^ *)(r?"""\n)(.+?)\1"""', code, flags=re.M | re.S):
for m_docstring in re.finditer(r'(^ *)(r?"""[^\r\n]*\n)(.+?)\1"""', code, flags=re.M | re.S):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for m_docstring in re.finditer(r'(^ *)(r?"""[^\r\n]*\n)(.+?)\1"""', code, flags=re.M | re.S):
for m_docstring in re.finditer(r'(^ *)(r?""".*?\n)(.+?)\1"""', code, flags=re.M | re.S):

Should work the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Updated in 85681d7

Change [\r\n]* to .*?
@samuelcolvin samuelcolvin merged commit a2c6dd1 into pydantic:main Apr 5, 2023
@samuelcolvin
Copy link
Member

great, thanks. I'll try to do a new release soon.

@donovandicks donovandicks deleted the dd/fix-alternate-docstyle branch April 5, 2023 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants