Repository: encode/httpx
File: httpx/config.py (Python)

Code before:
    def __eq__(self, other: typing.Any) -> bool:
        return (
            isinstance(other, self.__class__)
            and self.cert == other.cert
            and self.verify == other.verify
        )

    def __repr__(self) -> str:
        class_name = self.__class__.__name__
        return f"{class_name}(cert={self.cert}, verify={self.verify})"

    def with_overrides(
        self, cert: CertTypes = None, verify: VerifyTypes = None
    ) -> "SSLConfig":
        cert = self.cert if cert is None else cert
        verify = self.verify if verify is None else verify
        if (cert == self.cert) and (verify == self.verify):
            return self
        return SSLConfig(cert=cert, verify=verify)

    def load_ssl_context(
        self, http_versions: "HTTPVersionConfig" = None
    ) -> ssl.SSLContext:
        http_versions = HTTPVersionConfig() if http_versions is None else http_versions

        if self.trust_env:
            if self.verify is True or self.verify is None:
                ssl_cert_file = get_ca_bundle_from_env()
                if ssl_cert_file is not None:
                    self.verify = ssl_cert_file  # type: ignore

        if self.ssl_context is None:
            self.ssl_context = (
                self.load_ssl_context_verify(http_versions=http_versions)
                if self.verify
                else self.load_ssl_context_no_verify(http_versions=http_versions)
            )

        assert self.ssl_context is not None
        return self.ssl_context

    def load_ssl_context_no_verify(
        self, http_versions: "HTTPVersionConfig"
    ) -> ssl.SSLContext:
        """
        Return an SSL context for unverified connections.
        """
        context = self._create_default_ssl_context(http_versions=http_versions)
        context.verify_mode = ssl.CERT_NONE
        context.check_hostname = False
        return context

Review comment (refactor):
Can we move this section into the `load_ssl_context_verify()`? Since we're only doing the loading if we've got verify active anyways.

Also is there any harm in having `SSLConfig.verify` default to `True` in the constructor? It defaults to `True` on the client anyways.

Code after:
    def __eq__(self, other: typing.Any) -> bool:
        return (
            isinstance(other, self.__class__)
            and self.cert == other.cert
            and self.verify == other.verify
        )

    def __repr__(self) -> str:
        class_name = self.__class__.__name__
        return f"{class_name}(cert={self.cert}, verify={self.verify})"

    def with_overrides(
        self, cert: CertTypes = None, verify: VerifyTypes = None
    ) -> "SSLConfig":
        cert = self.cert if cert is None else cert
        verify = self.verify if verify is None else verify
        if (cert == self.cert) and (verify == self.verify):
            return self
        return SSLConfig(cert=cert, verify=verify)

    def load_ssl_context(
        self, http_versions: "HTTPVersionConfig" = None
    ) -> ssl.SSLContext:
        http_versions = HTTPVersionConfig() if http_versions is None else http_versions

        if self.ssl_context is None:
            self.ssl_context = (
                self.load_ssl_context_verify(http_versions=http_versions)
                if self.verify
                else self.load_ssl_context_no_verify(http_versions=http_versions)
            )

        assert self.ssl_context is not None
        return self.ssl_context

    def load_ssl_context_no_verify(
        self, http_versions: "HTTPVersionConfig"
    ) -> ssl.SSLContext:
        """
        Return an SSL context for unverified connections.
        """
        context = self._create_default_ssl_context(http_versions=http_versions)
        context.verify_mode = ssl.CERT_NONE
        context.check_hostname = False
        return context

    def load_ssl_context_verify(
        self, http_versions: "HTTPVersionConfig"
    ) -> ssl.SSLContext:
        """
        Return an SSL context for verified connections.