-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Semver as a dependency * Add SemanticVersion type including tests * Increase test coverage --------- Co-authored-by: Yasser Tahiri <[email protected]>
- Loading branch information
1 parent
55c2fcc
commit 1cddee1
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
SemanticVersion definition that is based on the Semantiv Versioning Specification [semver](https://semver.org/). | ||
""" | ||
|
||
from typing import Any, Callable | ||
|
||
from pydantic import GetJsonSchemaHandler | ||
from pydantic.json_schema import JsonSchemaValue | ||
from pydantic_core import core_schema | ||
|
||
try: | ||
import semver | ||
except ModuleNotFoundError as e: # pragma: no cover | ||
raise RuntimeError( | ||
'The `semantic_version` module requires "semver" to be installed. You can install it with "pip install semver".' | ||
) from e | ||
|
||
|
||
class SemanticVersion: | ||
""" | ||
Semantic version based on the official [semver thread](https://python-semver.readthedocs.io/en/latest/advanced/combine-pydantic-and-semver.html). | ||
""" | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__( | ||
cls, | ||
_source_type: Any, | ||
_handler: Callable[[Any], core_schema.CoreSchema], | ||
) -> core_schema.CoreSchema: | ||
def validate_from_str(value: str) -> semver.Version: | ||
return semver.Version.parse(value) | ||
|
||
from_str_schema = core_schema.chain_schema( | ||
[ | ||
core_schema.str_schema(), | ||
core_schema.no_info_plain_validator_function(validate_from_str), | ||
] | ||
) | ||
|
||
return core_schema.json_or_python_schema( | ||
json_schema=from_str_schema, | ||
python_schema=core_schema.union_schema( | ||
[ | ||
core_schema.is_instance_schema(semver.Version), | ||
from_str_schema, | ||
] | ||
), | ||
serialization=core_schema.to_string_ser_schema(), | ||
) | ||
|
||
@classmethod | ||
def __get_pydantic_json_schema__( | ||
cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler | ||
) -> JsonSchemaValue: | ||
return handler(core_schema.str_schema()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from pydantic_extra_types.semantic_version import SemanticVersion | ||
|
||
|
||
@pytest.fixture(scope='module', name='SemanticVersionObject') | ||
def application_object_fixture(): | ||
class Application(BaseModel): | ||
version: SemanticVersion | ||
|
||
return Application | ||
|
||
|
||
def test_valid_semantic_version(SemanticVersionObject): | ||
application = SemanticVersionObject(version='1.0.0') | ||
assert application.version | ||
assert application.model_dump() == {'version': '1.0.0'} | ||
|
||
|
||
def test_invalid_semantic_version(SemanticVersionObject): | ||
with pytest.raises(ValidationError): | ||
SemanticVersionObject(version='Peter Maffay') |