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

Make pydantic 1/2 compatible #2

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
matrix:
os: [ ubuntu-latest ]
python-version: [ "3.11" ]
pydantic: [ "pydantic1", "pydantic2" ]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -22,7 +23,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install hatch
pip install tox
- name: Test with pytest
run:
hatch run test
tox -e py-${{ matrix.pydantic }}
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ dependencies = [
"uvicorn",
]

[project.optional-dependencies]
tests = [
"pytest",
"coverage",
]

[project.urls]
Documentation = "https://github.com/biopragmatics/semra#readme"
Issues = "https://github.com/biopragmatics/semra/issues"
Expand Down
6 changes: 3 additions & 3 deletions src/semra/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Input(BaseModel):
class Mutation(BaseModel):
"""Represents a mutation operation on a mapping set."""

source: str = Field()
source: str = Field(..., description="The source type")
confidence: float = 1.0
old: Reference = Field(default=DB_XREF)
new: Reference = Field(default=EXACT_MATCH)
Expand All @@ -69,7 +69,7 @@ class Configuration(BaseModel):

inputs: list[Input]
negative_inputs: list[Input] = Field(default=[Input(source="biomappings", prefix="negative")])
priority: list[str] = Field(description="If no priority is given, is inferred from the order of inputs")
priority: list[str] = Field(..., description="If no priority is given, is inferred from the order of inputs")
mutations: list[Mutation] = Field(default_factory=list)
exclude_pairs: list[tuple[str, str]] = Field(
default_factory=list,
Expand All @@ -91,7 +91,7 @@ class Configuration(BaseModel):

sssom_add_labels: bool = False

@root_validator
@root_validator(skip_on_failure=True)
def infer_priority(cls, values): # noqa:N805
"""Infer the priority from the input list of not given."""
priority = values["priority"]
Expand Down
16 changes: 9 additions & 7 deletions src/semra/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from curies import Reference
from more_itertools import triplewise
from pydantic import Field
from pydantic.types import UUID, UUID4

__all__ = [
"Reference",
Expand Down Expand Up @@ -63,10 +64,10 @@ def curie(self) -> str:


class MappingSet(pydantic.BaseModel):
name: str
version: str | None = Field(description="The version of the dataset from which the mapping comes")
license: str | None = Field(description="License name or URL for mapping set")
confidence: float | None = Field(description="Mapping set level confidence")
name: str = Field(..., description="Name of the mapping set")
version: str | None = Field(default=None, description="The version of the dataset from which the mapping comes")
license: str | None = Field(default=None,description="License name or URL for mapping set")
confidence: float | None = Field(default=None,description="Mapping set level confidence")

def key(self):
return self.name, self.version or "", self.license or "", 1.0 if self.confidence is None else self.confidence
Expand Down Expand Up @@ -98,12 +99,13 @@ class Config:
default=Reference(prefix="semapv", identifier="UnspecifiedMapping"),
description="A SSSOM-compliant justification",
)
mapping_set: MappingSet = Field(description="The name of the dataset from which the mapping comes")
mapping_set: MappingSet = Field(..., description="The name of the dataset from which the mapping comes")
author: Reference | None = Field(
default=None,
description="A reference to the author of the mapping (e.g. with ORCID)",
example=Reference(prefix="orcid", identifier="0000-0003-4423-4370"),
)
uuid: uuid.UUID = Field(default_factory=uuid.uuid4)
uuid: UUID4 = Field(default_factory=uuid.uuid4)

def key(self):
"""Get a key suitable for hashing the evidence.
Expand Down Expand Up @@ -136,7 +138,7 @@ class Config:
frozen = True

evidence_type: Literal["reasoned"] = Field(default="reasoned")
justification: Reference = Field(description="A SSSOM-compliant justification")
justification: Reference = Field(..., description="A SSSOM-compliant justification")
mappings: list[Mapping] = Field(
..., description="A list of mappings and their evidences consumed to create this evidence"
)
Expand Down
19 changes: 19 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
isolated_build = true
envlist =
# the actual tests
py-pydantic1
py-pydantic2

[testenv]
commands = python -m coverage run -p -m pytest --durations=20 {posargs:tests}
deps =
pydantic1: pydantic<2.0
pydantic2: pydantic>=2.0
extras =
tests
Loading