Mypy 没有检测到类型保护,为什么?

问题描述 投票:0回答:1

我正在尝试自学如何在我的新 Python 项目中结合 pydantic-settings 使用类型保护,但 mypy 似乎并没有接受它们。我在这里做错了什么?

代码:

import logging
from logging.handlers import SMTPHandler
from functools import lru_cache
from typing import Final, Literal, TypeGuard

from pydantic import EmailStr, SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict

SMTP_PORT: Final = 587


class Settings(BaseSettings):
    """
    Please make sure your .env contains the following variables:
    - BOT_TOKEN - an API token for your bot.
    - TOPIC_ID - an ID for your group chat topic.
    - GROUP_CHAT_ID - an ID for your group chat.
    - ENVIRONMENT - if you intend on running this script on a VPS, this improves logging
        information in your production system.

    Required only in production:

    - SMTP_HOST - SMTP server address (e.g., smtp.gmail.com)
    - SMTP_USER - Email username/address for SMTP authentication
    - SMTP_PASSWORD - Email password or app-specific password
    """

    ENVIRONMENT: Literal["production", "development"]

    # Telegram bot configuration
    BOT_TOKEN: SecretStr
    TOPIC_ID: int
    GROUP_CHAT_ID: int

    # Email configuration
    SMTP_HOST: str | None = None
    SMTP_USER: EmailStr | None = None
    # If you're using Gmail, this needs to be an app password
    SMTP_PASSWORD: SecretStr | None = None

    model_config = SettingsConfigDict(env_file="../.env", env_file_encoding="utf-8")


@lru_cache(maxsize=1)
def get_settings() -> Settings:
    """This needs to be lazily evaluated, otherwise pytest gets a circular import."""
    return Settings()


type DotEnvStrings = str | SecretStr | EmailStr


def is_all_email_settings_provided(
    host: DotEnvStrings | None,
    user: DotEnvStrings | None,
    password: DotEnvStrings | None,
) -> TypeGuard[DotEnvStrings]:
    """
    Type guard that checks if all email settings are provided.

    Returns:
        True if all email settings are provided as strings, False otherwise.
    """
    return all(isinstance(x, (str, SecretStr, EmailStr)) for x in (host, user, password))


def get_logger():
    ...
    settings = get_settings()
    if settings.ENVIRONMENT == "development":
        level = logging.INFO
    else:
        # # We only email logging information on failure in production.
        if not is_all_email_settings_provided(
            settings.SMTP_HOST, settings.SMTP_USER, settings.SMTP_PASSWORD
        ):
            raise ValueError("All email environment variables are required in production.")
        level = logging.ERROR
        email_handler = SMTPHandler(
            mailhost=(settings.SMTP_HOST, SMTP_PORT),
            fromaddr=settings.SMTP_USER,
            toaddrs=settings.SMTP_USER,
            subject="Application Error",
            credentials=(settings.SMTP_USER, settings.SMTP_PASSWORD.get_secret_value()),
            # This enables TLS - https://docs.python.org/3/library/logging.handlers.html#smtphandler
            secure=(),
        )

这是 mypy 所说的:

media_only_topic\media_only_topic.py:122: error: Argument "mailhost" to "SMTPHandler" has incompatible type "tuple[str | SecretStr, int]"; expected "str | tuple[str, int]"  [arg-type]
media_only_topic\media_only_topic.py:123: error: Argument "fromaddr" to "SMTPHandler" has incompatible type "str | None"; expected "str"  [arg-type]
media_only_topic\media_only_topic.py:124: error: Argument "toaddrs" to "SMTPHandler" has incompatible type "str | None"; expected "str | list[str]"  [arg-type]
media_only_topic\media_only_topic.py:126: error: Argument "credentials" to "SMTPHandler" has incompatible type "tuple[str | None, str | Any]"; expected "tuple[str, str] | None"  [arg-type]
media_only_topic\media_only_topic.py:126: error: Item "None" of "SecretStr | None" has no attribute "get_secret_value"  [union-attr]
Found 5 errors in 1 file (checked 1 source file)

我希望 mypy 这里能够正确读取我的变量甚至在理论上都不能是

None
,但是类型保护似乎在这里没有改变任何东西,无论我在这里更改代码多少次。更改为 Pyright 并没有什么区别。这里正确的方法是什么?

python python-typing mypy pydantic
1个回答
0
投票

Settings
更改为
DevSettings
并且仅允许
ENVIRONMENT
development
:

class DevSettings(BaseSettings):
    """
    Please make sure your .env contains the following variables:
    - BOT_TOKEN - an API token for your bot.
    - TOPIC_ID - an ID for your group chat topic.
    - GROUP_CHAT_ID - an ID for your group chat.
    - ENVIRONMENT - if you intend on running this script on a VPS, this improves logging
        information in your production system.

    Required only in production:

    - SMTP_HOST - SMTP server address (e.g., smtp.gmail.com)
    - SMTP_USER - Email username/address for SMTP authentication
    - SMTP_PASSWORD - Email password or app-specific password
    """

    ENVIRONMENT: Literal["development"]

    # Telegram bot configuration
    BOT_TOKEN: SecretStr
    TOPIC_ID: int
    GROUP_CHAT_ID: int

    # Email configuration
    SMTP_HOST: str | None = None
    SMTP_USER: EmailStr | None = None
    # If you're using Gmail, this needs to be an app password
    SMTP_PASSWORD: SecretStr | None = None

    model_config = SettingsConfigDict(env_file="../.env", env_file_encoding="utf-8")

添加一个具有差异的单独

ProdSettings
类。如果缺少任何这些值,
ProdSettings
现在将引发错误。

class ProdSettings(DevSettings):
    ENVIRONMENT: Literal["production"]

    # Email configuration
    SMTP_HOST: str
    SMTP_USER: EmailStr
    # If you're using Gmail, this needs to be an app password
    SMTP_PASSWORD: SecretStr

使用 Discriminator 来定义

Settings
,同时断言是什么让它们不同。

Settings = Annotated[DevSettings | ProdSettings, Field(discriminator="ENVIRONMENT")]

当你运行

get_settings
时,先尝试 dev,然后再尝试 prod。将返回类型设置为
Settings

@lru_cache(maxsize=1)
def get_settings() -> Settings:
    """This needs to be lazily evaluated, otherwise pytest gets a circular import."""
    try:
        return DevSettings()
    except Exception as e:
        return ProdSettings()

现在,当你有了

if settings.ENVIRONMENT == "development":
时,它就充当了 TypeGuard。您会看到 mypy 将
true
识别为
settings
DevSettings
的实例,否则是
ProdSettings
的实例。

def get_logger():
    settings = get_settings()
    if settings.ENVIRONMENT == "development":
        level = logging.INFO
    else:
        level = logging.ERROR
        email_handler = SMTPHandler(
            mailhost=(settings.SMTP_HOST, SMTP_PORT),
            fromaddr=settings.SMTP_USER,
            toaddrs=settings.SMTP_USER,
            subject="Application Error",
            credentials=(settings.SMTP_USER, settings.SMTP_PASSWORD.get_secret_value()),
            # This enables TLS - https://docs.python.org/3/library/logging.handlers.html#smtphandler
            secure=(),
        )
© www.soinside.com 2019 - 2024. All rights reserved.