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

Feature/refactor dto #16

Merged
merged 14 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ repos:
"--disable=C",
"--disable=C0415",
"--disable=W",
"--disable=R0903",
"--disable=E1101",
"--msg-template={path}||{msg_id}||{symbol}||{category}||{line}||{column}||{msg}",
]
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ analyse-fallback-blocks=no
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-allow-list=
extension-pkg-allow-list=ErrorWrapper,pydantic.dataclasses,DataclassT

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code. (This is an alternative name to extension-pkg-allow-list
# for backward compatibility.)
extension-pkg-whitelist=
extension-pkg-whitelist=ErrorWrapper

# Return non-zero exit code if any of these messages/categories are detected,
# even if score is above --fail-under value. Syntax same as enable. Messages
Expand Down
35 changes: 17 additions & 18 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ pytest = "7.1.2"
black = {extras = ["d", "click==8.0.2"], version = ">=22.1.0"}
pre-commit = "^2.17.0"
pylint = "^2.14.4"
isort = "^5.10.1"
anyio = "~3.6.1"
pytest-asyncio = "0.18.3"
pytest-asyncio = "0.19.0"
pysqlite3 = "^0.4.6"
types-pytest-lazy-fixture = ">=0.6.3"

[tool.pytest.ini_options]
markers = [
Expand All @@ -52,8 +54,32 @@ build-backend = "poetry.core.masonry.api"
[tool.mypy]
python_version = "3.9"
plugins = ["pydantic.mypy"]
# See: https://blog.wolt.com/engineering/2021/09/30/professional-grade-mypy-configuration/
disallow_untyped_defs = true
check_untyped_defs = true
disallow_any_unimported = true
ignore_missing_imports = false
no_implicit_optional = true
warn_return_any = true
show_error_codes = true
warn_unused_ignores = true

# See: https://pydantic-docs.helpmanual.io/mypy_plugin/
# mypy per-module options:
[tool.pydantic-mypy]
init_typed = true

# See: https://hadialqattan.github.io/pycln/#/?id=usage
[tool.pycln]
path = "registrations/"
expand_stars=true
verbose=true
diff=true
all=true
no_gitignore=false

[tool.isort]
line_length = 79
include_trailing_comma = true
use_parentheses = true
ensure_newline_before_comments = true
93 changes: 91 additions & 2 deletions registrations/domain/dto.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""Data transfer object for domain registration."""
"""Data transfer object for hospital domain registration."""
from __future__ import annotations

from typing import Optional

import pydantic

from registrations.domain.hospital import registration
from registrations.domain.location.location import Address
from registrations.utils import enum_utils
from registrations.utils.errors import InvalidRegistrationEntryError


class RegisterKeyContact(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected keyword: Unexpected keyword argument extra to call object.__init_subclass__.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonatype-lift ignore

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recorded this as ignored for this pull request. If you change your mind, just comment @sonatype-lift unignore.

pydantic.BaseModel,
extra=pydantic.Extra.forbid,
allow_mutation=False,
validate_assignment=True,
arbitrary_types_allowed=False,
Expand All @@ -21,7 +25,7 @@ class RegisterKeyContact(
email: Optional[pydantic.EmailStr]


class HospitalRegistrationEntry(
class ToHospitalRegistrationEntry(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized attribute: Attribute hospital_contact_number is declared in class ToHospitalRegistrationEntry to have type str but is never initialized.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonatype-lift ignore

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recorded this as ignored for this pull request. If you change your mind, just comment @sonatype-lift unignore.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized attribute: Attribute name is declared in class ToHospitalRegistrationEntry to have type str but is never initialized.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonatype-lift ignore

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recorded this as ignored for this pull request. If you change your mind, just comment @sonatype-lift unignore.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized attribute: Attribute verified_status is declared in class ToHospitalRegistrationEntry to have type Optional[str] but is never initialized.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonatype-lift ignore

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recorded this as ignored for this pull request. If you change your mind, just comment @sonatype-lift unignore.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized attribute: Attribute address is declared in class ToHospitalRegistrationEntry to have type Address but is never initialized.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonatype-lift ignore

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recorded this as ignored for this pull request. If you change your mind, just comment @sonatype-lift unignore.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized attribute: Attribute key_contact is declared in class ToHospitalRegistrationEntry to have type Optional[RegisterKeyContact] but is never initialized.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uninitialized attribute: Attribute ownership_type is declared in class ToHospitalRegistrationEntry to have type str but is never initialized.

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

pydantic.BaseModel,
allow_mutation=False,
validate_assignment=True,
Expand All @@ -35,3 +39,88 @@ class HospitalRegistrationEntry(
key_contact: Optional[RegisterKeyContact]
verified_status: Optional[str]
address: Address

@pydantic.validator("ownership_type", pre=True)
@classmethod
def validate_ownership_type(cls, ownership_type: str) -> str:
"""Validate ownership is limited to OwnershipType enum."""
if ownership_type not in registration.OwnershipType.values():
raise ValueError(f"Invalid ownership type: {ownership_type}")
return ownership_type

@pydantic.validator("name", pre=True)
@classmethod
def validate_name(cls, name: str) -> str:
"""Validate name has atleast one word with 3 characters."""
split_names = name.strip().split()
if valid_conditions := split_names and (
any(len(word) >= 2 for word in split_names)
or (len(split_names) == 1 and len(name.strip().split()[0]) >= 3)
):
return name
else:
raise ValueError(f"Invalid name: {name}. Not enough characters.")

@pydantic.root_validator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid decoration: While applying decorator pydantic.root_validator

Reply with "@sonatype-lift help" for info about LiftBot commands.
Reply with "@sonatype-lift ignore" to tell LiftBot to leave out the above finding from this PR.
Reply with "@sonatype-lift ignoreall" to tell LiftBot to leave out all the findings from this PR and from the status bar in Github.

When talking to LiftBot, you need to refresh the page to see its response. Click here to get to know more about LiftBot commands.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonatype-lift ignore

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recorded this as ignored for this pull request. If you change your mind, just comment @sonatype-lift unignore.

@classmethod
def validate_hospital_entity(cls, values: dict) -> dict:
"""Validate hospital entity.

We either get a verification status or an unverified
manual registration entry. We cannot get both.
Either attribute should be accessible via registration_entry
as verified_status or key_contact.
"""
verified_status = values.get("verified_status")
key_contact: Optional[RegisterKeyContact] = values.get("key_contact")
wrong_invariant = not key_contact and not verified_status
wrong_invariant = wrong_invariant or bool(
verified_status
and (
(
verified_status
== enum_utils.enum_value_of(
registration.VerificationStatus.Unverified
)
and not key_contact
)
or (
verified_status
!= enum_utils.enum_value_of(
registration.VerificationStatus.Unverified
)
and key_contact
)
)
)
if wrong_invariant:
error_msg = (
f"Cannot have {verified_status} verification status with "
f"{(key_contact and key_contact.name) or key_contact} key contact."
)
raise InvalidRegistrationEntryError(error_msg)
return values

def build_hospital_entity_dict(self) -> dict:
"""Build hospital entity.

:return: dict, the hospital entity dict to register.
"""
builder_dict = self.dict()
if verified_status := builder_dict.get("verified_status"):
builder_dict["verified_status"] = registration.VerificationStatus(
verified_status
)
if key_contact := builder_dict.pop("key_contact", None):
builder_dict["key_contact_registrar"] = registration.ContactPerson(
name=key_contact.get("name"),
mobile_number=registration.PhoneNumber(
number=key_contact.get("mobile")
),
email=key_contact.get("email"),
)
builder_dict["phone_number"] = registration.PhoneNumber(
number=builder_dict.pop("hospital_contact_number")
)
builder_dict["hospital_name"] = builder_dict.pop("name")
return builder_dict
Loading