python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.22k stars 2.78k forks source link

x is None or y is None VS not all((x, y)) #17800

Closed virajkanwade closed 5 hours ago

virajkanwade commented 5 hours ago

Bug Report

(A clear and concise description of what the bug is.)

utils/emailsender.py:22: error: Argument "MAIL_USERNAME" to "ConnectionConfig" has incompatible type "str | None"; expected "str" [arg-type]

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)

if not all((
    self.settings.MAIL_USERNAME,
    self.settings.MAIL_PASSWORD,
    self.settings.EMAIL_SUPPORT,
    self.settings.MAIL_SERVER
)):
    raise ValueError("Mail settings must be configured and cannot be None.")

conf = ConnectionConfig(
            MAIL_USERNAME=self.settings.EMAIL_USERNAME,
            MAIL_PASSWORD=self.settings.EMAIL_PASSWORD,
            MAIL_PORT=self.settings.EMAIL_PORT,
            MAIL_FROM=self.settings.EMAIL_SUPPORT_ADDRESS,
            MAIL_SERVER=self.settings.EMAIL_SERVER,
            MAIL_SSL_TLS=self.settings.EMAIL_SSL_TLS,
            MAIL_STARTTLS=self.settings.EMAIL_STARTTLS,
        )

This raises the warning during check utils/emailsender.py:22: error: Argument "MAIL_USERNAME" to "ConnectionConfig" has incompatible type "str | None"; expected "str" [arg-type]

If i change it to

if (self.settings.EMAIL_USERNAME is None
            or self.settings.EMAIL_PASSWORD is None
            or self.settings.EMAIL_SUPPORT_ADDRESS is None
            or self.settings.EMAIL_SERVER is None
        ):
            raise ValueError("Mail settings must be configured and cannot be None.")

it reports no issues

Expected Behavior

In both cases it should not raise the warning.

Actual Behavior

Your Environment

erictraut commented 5 hours ago

I think this is a duplicate of #17149.

virajkanwade commented 5 hours ago

I tried searching but for some reason didn't see it. Thanks for pointing it out.