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

feat(parser): use str for SES EmailStr when email_validator package is omitted #1259

Closed
Closed
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
13 changes: 11 additions & 2 deletions aws_lambda_powertools/utilities/parser/models/ses.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import logging
import sys
from datetime import datetime
from typing import List, Optional
from typing import List, NewType, Optional

from pydantic import BaseModel, Field
from pydantic.networks import EmailStr
from pydantic.types import PositiveInt

from ..types import Literal

has_email_validator = "email_validator" in sys.modules
Copy link
Contributor

@heitorlessa heitorlessa Jun 27, 2022

Choose a reason for hiding this comment

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

I suspect this will always be False because Pydantic is loading it dynamically, and this will only be true afterwards. You'll need to check if the module is actually installed instead of loaded - I can't recall by heart a 3.6+ compatible way in importlib/pkg_resources to do that. I can help look for that after I finish another doc PR this week


if has_email_validator:
Copy link
Contributor

Choose a reason for hiding this comment

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

we're gonna need #pragma: nocover too because this will decrease our tests despite being intended behaviour ;-)

from pydantic.networks import EmailStr
else:
logging.warning("email_validator package is not installed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Initialize a logger and change it to debug, so we'll know where it comes from.

logger = logging.getLogger(__name__)

...
logger.debug("email_validator package is not installed; using str instead")

EmailStr = NewType("EmailStr", str) # type: ignore[no-redef, misc]
Copy link
Contributor

Choose a reason for hiding this comment

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

NewType has recently been converted to a class which means it'll have a runtime impact. Since we're looking at a string only IF someone didn't follow our recommended install, use a Type Alias instead.

You can also bring this up to save a else branch, for example:

from pydantic import BaseModel 


EmailStr = str

if has_email_validator:
    from pydantic import EmailStr


class Email(BaseModel):
        address: EmailStr


feedback_channel = Email("aws-lambda-powertools-feedback@amazon.com")
print(feedback_channel.address)



class SesReceiptVerdict(BaseModel):
status: Literal["PASS", "FAIL", "GRAY", "PROCESSING_FAILED"]
Expand Down