Skip to content

Commit

Permalink
Make explicit cross-version support for Pydantic (#921)
Browse files Browse the repository at this point in the history
As a follow-up to biopragmatics/curies#64 (and related
to #920 and #899),
this PR makes Bioregistry support both Pydantic v1 and v2
  • Loading branch information
cthoyt committed Aug 7, 2023
1 parent 344c795 commit 119c641
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
matrix:
os: [ ubuntu-latest, windows-latest ]
python-version: [ "3.11", "3.8" ]
pydantic: [ "pydantic1", "pydantic2" ]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -72,7 +73,7 @@ jobs:
pip install tox
- name: Test with pytest
run:
tox -e py
tox -e py-${{ matrix.pydantic }}
- name: Doctests
run:
tox -e doctests
Expand Down
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"sphinx.ext.todo",
# 'sphinx.ext.mathjax',
"sphinx.ext.viewcode",
"sphinx_autodoc_typehints",
"sphinx_click.ext",
"sphinx_automodapi.automodapi",
"sphinx_automodapi.smart_resolver",
Expand Down
5 changes: 2 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ install_requires =
pystow>=0.1.13
click
more_click>=0.1.2
pydantic<2.0
curies>=0.5.6
pydantic
curies>=0.6.0

zip_safe = false
include_package_data = True
Expand All @@ -75,7 +75,6 @@ docs =
sphinx
sphinx-rtd-theme
sphinx-click
sphinx-autodoc-typehints==1.21.1
sphinx_automodapi
autodoc_pydantic
gha =
Expand Down
2 changes: 2 additions & 0 deletions src/bioregistry/app/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .api import api_router
from .constants import BIOSCHEMAS
from .ui import ui_blueprint
from ..constants import PYDANTIC_1

if TYPE_CHECKING:
import bioregistry
Expand Down Expand Up @@ -192,6 +193,7 @@ def get_app(
curie_to_str=curie_to_str,
fastapi_url_for=fast_api.url_path_for,
markdown=markdown,
is_pydantic_1=PYDANTIC_1,
)

if return_flask:
Expand Down
12 changes: 9 additions & 3 deletions src/bioregistry/app/templates/meta/related.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
{% endmacro %}

{% macro registry_field_dl(cls, key) -%}
{% set field = cls.__fields__[key].field_info %}
<dt>{{ field.title or key.replace("_", " ").title() }}</dt>
<dd>{{ field.description }}</dd>
{% if is_pydantic_1 %}
{% set field = cls.__fields__[key].field_info %}
<dt>{{ field.title or key.replace("_", " ").title() }}</dt>
<dd>{{ field.description }}</dd>
{% else %}
{% set field = cls.__fields__[key] %}
<dt>{{ field.title or key.replace("_", " ").title() }}</dt>
<dd>{{ field.description }}</dd>
{% endif %}
{% endmacro %}

{% block styles %}
Expand Down
3 changes: 3 additions & 0 deletions src/bioregistry/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""Constants and utilities for registries."""

import importlib.metadata
import os
import pathlib
import re
Expand All @@ -19,6 +20,8 @@
"BIOREGISTRY_MODULE",
]

PYDANTIC_1 = importlib.metadata.version("pydantic").startswith("1.")

HERE = pathlib.Path(os.path.abspath(os.path.dirname(__file__)))
DATA_DIRECTORY = HERE / "data"
EXTERNAL = DATA_DIRECTORY / "external"
Expand Down
1 change: 1 addition & 0 deletions src/bioregistry/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@
},
"comments": {
"title": "Comments",
"description": "Optional additional comments about the registry's governance model",
"type": "string"
},
"accepts_external_contributions": {
Expand Down
Loading

0 comments on commit 119c641

Please sign in to comment.