Skip to content

⚡️ Speed up function _infer_docstring_style by 31% #32

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

Open
wants to merge 1 commit into
base: try-refinement
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pydantic_ai_slim/pydantic_ai/_griffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ def doc_descriptions(
def _infer_docstring_style(doc: str) -> DocstringStyle:
"""Simplistic docstring style inference."""
for pattern, replacements, style in _docstring_style_patterns:
matches = (
re.search(pattern.format(replacement), doc, re.IGNORECASE | re.MULTILINE) for replacement in replacements
)
if any(matches):
return style
for replacement in replacements:
key = (pattern, replacement)
regex = _regex_cache.get(key)
if regex is None:
regex = re.compile(pattern.format(replacement), re.IGNORECASE | re.MULTILINE)
_regex_cache[key] = regex
if regex.search(doc):
return style
# fallback to google style
return 'google'

Expand Down Expand Up @@ -171,3 +174,5 @@ def _disable_griffe_logging():
logging.root.setLevel(logging.ERROR)
yield
logging.root.setLevel(old_level)

_regex_cache = {}
Loading