or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/pypi-types-backports

Type stubs for Python backports modules, providing type annotations for backported functionality from newer Python versions

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/types-backports@0.1.x

To install, run

npx @tessl/cli install tessl/pypi-types-backports@0.1.0

index.mddocs/

types-backports

Type stubs for Python backports modules, providing type annotations for backported functionality from newer Python versions. This package was historically part of the typeshed project and provided typing support for libraries that backported features to older Python versions.

Key Features

  • SSL certificate hostname verification for older Python versions
  • Type-safe certificate validation with proper error handling
  • Namespace support for incomplete typing scenarios
  • Seamless integration with Python's ssl module

Package Information

  • Package Name: types-backports
  • Package Type: pypi
  • Language: Python
  • Installation: pip install types-backports

Core Imports

from backports.ssl_match_hostname import match_hostname, CertificateError

Alternative import style:

import backports.ssl_match_hostname

Basic Usage

from backports.ssl_match_hostname import match_hostname, CertificateError
import ssl
import socket

# Get peer certificate from SSL connection
sock = ssl.wrap_socket(socket.socket())
sock.connect(('example.com', 443))
cert = sock.getpeercert()

# Verify hostname matches certificate
try:
    match_hostname(cert, 'example.com')
    print("Hostname matches certificate")
except CertificateError as e:
    print(f"Hostname verification failed: {e}")
finally:
    sock.close()

# Advanced usage with certificate inspection
def verify_with_details(hostname, port=443):
    """Verify hostname with detailed certificate information."""
    context = ssl.create_default_context()
    with socket.create_connection((hostname, port)) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
            cert = ssock.getpeercert()
            
            # Extract certificate details
            subject = dict(x[0] for x in cert['subject'])
            common_name = subject.get('commonName', '')
            
            try:
                match_hostname(cert, hostname)
                return True, f"Verified: {common_name}"
            except CertificateError as e:
                return False, f"Failed for {common_name}: {e}"

# Example usage
success, message = verify_with_details('example.com')
print(message)

Capabilities

SSL Hostname Verification

Provides SSL certificate hostname verification functionality that was backported from newer Python versions to work with older Python installations.

def match_hostname(cert: _PeerCertRetDictType, hostname: str) -> None:
    """
    Verify that cert (in decoded format as returned by SSLSocket.getpeercert()) 
    matches the given hostname.
    
    Parameters:
    - cert: SSL certificate data structure from SSLSocket.getpeercert()
    - hostname: str, hostname to verify against certificate
    
    Returns:
    None: Function doesn't return a value, raises exception on failure
    
    Raises:
    CertificateError: If hostname doesn't match certificate
    """

Certificate Validation Errors

Exception class for SSL certificate validation failures.

class CertificateError(ValueError):
    """
    Exception raised when SSL certificate validation fails during hostname matching.
    
    Inherits from ValueError and is raised by match_hostname when the provided
    certificate does not match the given hostname.
    """

Namespace Support

Root backports namespace provides dynamic attribute access for incomplete typing.

def __getattr__(name: str) -> Incomplete:
    """
    Provides dynamic attribute access for incomplete typing in the backports namespace.
    
    Parameters:
    - name: str, attribute name being accessed
    
    Returns:
    Incomplete: Placeholder type for incomplete typing
    """

Types

The package uses types from the standard library:

from typing import Any, Dict, List, Union

# SSL certificate dictionary structure as returned by SSLSocket.getpeercert()
_PeerCertRetDictType = Dict[str, Union[
    str,                    # Simple string fields like 'serialNumber', 'version'
    List[List[str]],       # Subject/issuer DN components
    List[str]              # subjectAltName entries  
]]

# Placeholder type for incomplete typing from _typeshed
Incomplete = Any

Notes

  • Deprecation Status: This package is no longer maintained and was removed from typeshed in March 2023
  • Reason for Removal: Modern Python versions (3.2+) include SSL hostname verification in the standard library
  • Migration Path: Use Python's built-in ssl.match_hostname() function instead
  • Historical Context: This package provided typing for the backports.ssl_match_hostname PyPI package, which backported SSL hostname matching functionality to older Python versions
  • Version Coverage: The type stubs were compatible with Python 3.7 and later