From a7b77cac7331636c0e464805020b18b84fa77c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Voron?= Date: Wed, 12 Jul 2023 10:54:51 +0200 Subject: [PATCH] Create a dedicated test environment and fix coverage/typing issues to support Pydantic V2 --- fastapi_users/schemas.py | 24 ++++++++++++------------ pyproject.toml | 26 +++++++++++++++----------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/fastapi_users/schemas.py b/fastapi_users/schemas.py index 86696241..1a618410 100644 --- a/fastapi_users/schemas.py +++ b/fastapi_users/schemas.py @@ -9,21 +9,21 @@ PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.") SCHEMA = TypeVar("SCHEMA", bound=BaseModel) -if PYDANTIC_V2: +if PYDANTIC_V2: # pragma: no cover def model_dump(model: BaseModel, *args, **kwargs) -> Dict[str, Any]: - return model.model_dump(*args, **kwargs) + return model.model_dump(*args, **kwargs) # type: ignore def model_validate(schema: Type[SCHEMA], obj: Any, *args, **kwargs) -> SCHEMA: - return schema.model_validate(obj, *args, **kwargs) + return schema.model_validate(obj, *args, **kwargs) # type: ignore -else: +else: # pragma: no cover # type: ignore def model_dump(model: BaseModel, *args, **kwargs) -> Dict[str, Any]: - return model.dict(*args, **kwargs) + return model.dict(*args, **kwargs) # type: ignore def model_validate(schema: Type[SCHEMA], obj: Any, *args, **kwargs) -> SCHEMA: - return schema.from_orm(obj) + return schema.from_orm(obj) # type: ignore class CreateUpdateDictModel(BaseModel): @@ -53,9 +53,9 @@ class BaseUser(CreateUpdateDictModel, Generic[models.ID]): is_superuser: bool = False is_verified: bool = False - if PYDANTIC_V2: - model_config = ConfigDict(from_attributes=True) - else: + if PYDANTIC_V2: # pragma: no cover + model_config = ConfigDict(from_attributes=True) # type: ignore + else: # pragma: no cover class Config: orm_mode = True @@ -93,9 +93,9 @@ class BaseOAuthAccount(BaseModel, Generic[models.ID]): account_id: str account_email: str - if PYDANTIC_V2: - model_config = ConfigDict(from_attributes=True) - else: + if PYDANTIC_V2: # pragma: no cover + model_config = ConfigDict(from_attributes=True) # type: ignore + else: # pragma: no cover class Config: orm_mode = True diff --git a/pyproject.toml b/pyproject.toml index 101323e2..cb21e80b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,18 +73,7 @@ dependencies = [ "ruff", ] -[[tool.hatch.envs.default.matrix]] -pydantic = ["v1", "v2"] - -[tool.hatch.envs.default.overrides] -matrix.pydantic.extra-dependencies = [ - {value = "pydantic<2.0", if = ["v1"]}, - {value = "pydantic>=2.0", if = ["v2"]}, -] - [tool.hatch.envs.default.scripts] -test = "pytest --cov=fastapi_users/ --cov-report=term-missing --cov-fail-under=100" -test-cov-xml = "pytest --cov=fastapi_users/ --cov-report=xml --cov-fail-under=100" lint = [ "isort ./fastapi_users ./tests", "isort ./docs/src -o fastapi_users", @@ -103,6 +92,21 @@ lint-check = [ ] docs = "mkdocs serve" +[tool.hatch.envs.test] + +[tool.hatch.envs.test.scripts] +test = "pytest --cov=fastapi_users/ --cov-report=term-missing --cov-fail-under=100" +test-cov-xml = "pytest --cov=fastapi_users/ --cov-report=xml --cov-fail-under=100" + +[[tool.hatch.envs.test.matrix]] +pydantic = ["v1", "v2"] + +[tool.hatch.envs.test.overrides] +matrix.pydantic.extra-dependencies = [ + {value = "pydantic<2.0", if = ["v1"]}, + {value = "pydantic>=2.0", if = ["v2"]}, +] + [tool.hatch.build.targets.sdist] support-legacy = true # Create setup.py