Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into main
  • Loading branch information
NowanIlfideme committed Jul 5, 2021
2 parents 864d448 + d72739c commit 7afc697
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/python-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ jobs:
- name: Test with pytest
run: |
pytest
- name: Test with mypy
run: |
mypy -m pydantic_yaml
6 changes: 4 additions & 2 deletions pydantic_yaml/_yaml.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# flake8: noqa

try:
import ruamel.yaml as yaml
import ruamel.yaml as yaml # type: ignore

# ruamel.yaml doesn't have type annotations

if yaml.__version__ < "0.15.0":
__yaml_lib__ = "ruamel-old"
else:
__yaml_lib__ = "ruamel-new"
except ImportError:
try:
import yaml
import yaml # type: ignore

__yaml_lib__ = "pyyaml"
except ImportError:
Expand Down
19 changes: 9 additions & 10 deletions pydantic_yaml/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
from pathlib import Path
from typing import Any, Callable, Optional, Type, TypeVar, Union

from pydantic.main import BaseModel
from pydantic.error_wrappers import ErrorWrapper, ValidationError
from pydantic.types import StrBytes
from pydantic.main import BaseModel
from pydantic.parse import Protocol
from pydantic.types import StrBytes
from pydantic.utils import ROOT_KEY

from ._yaml import yaml

__all__ = ["YamlModel"]

try:
from typing import Literal

ExtendedProto = Union[Protocol, Literal["yaml"]]
from typing import Literal # type: ignore
except ImportError:
# I think this would happen with Python < 3.8
ExtendedProto = Union[Protocol, str]
from typing_extensions import Literal

ExtendedProto = Union[Protocol, Literal["yaml"]]


def is_yaml_requested(content_type: str = None, proto: ExtendedProto = None):
Expand All @@ -31,7 +30,7 @@ def is_yaml_requested(content_type: str = None, proto: ExtendedProto = None):
return is_yaml


T = TypeVar('T', bound='YamlModel')
T = TypeVar("T", bound="YamlModel")


class YamlModel(BaseModel):
Expand Down Expand Up @@ -64,7 +63,7 @@ def yaml(
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)
res = yaml.safe_dump(data)
res = str(yaml.safe_dump(data))
return res

@classmethod
Expand All @@ -73,7 +72,7 @@ def parse_raw(
b: StrBytes,
*,
content_type: str = None,
encoding: str = None,
encoding: str = "utf8",
proto: ExtendedProto = None,
allow_pickle: bool = False,
) -> T:
Expand Down
3 changes: 2 additions & 1 deletion pydantic_yaml/versioned_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from .models import YamlModel

try:
from pydantic import SemVer
# If Pydantic implements a SemVer string (which I want to make a PR for eventually)
from pydantic import SemVer # type: ignore
except ImportError:
from ._semver import SemVer

Expand Down
14 changes: 12 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ install_requires =
semver # 2 or 3 should both work
test_require =
pytest
mypy

[options.package_data]
* = *.yaml, *.yml
* = *.yaml, *.yml, py.typed

[options.extras_require]
pyyaml = pyyaml
pyyaml =
pyyaml
types-PyYAML
ruamel = ruamel.yaml>=0.15,<0.18 # new API starting from 0.15, but old is still available
dev =
black
flake8
bump2version
pytest
mypy

[mypy]
warn_unused_configs = True

[mypy-ruamel.*]
ignore_missing_imports = True

0 comments on commit 7afc697

Please sign in to comment.