CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-six

Python 2 and 3 compatibility utilities

Pending
Overview
Eval results
Files

version-detection.mddocs/

Version Detection and Type Constants

Core constants and type definitions for Python version detection and cross-version type checking. These utilities enable writing code that works consistently across Python 2 and 3 by providing unified type constants and version detection flags.

Capabilities

Version Detection Constants

Boolean constants that identify the Python version at runtime, enabling version-specific code paths.

PY2: bool  # True if running on Python 2, False otherwise
PY3: bool  # True if running on Python 3, False otherwise  
PY34: bool  # True if running on Python 3.4 or higher, False otherwise

Usage Examples:

import six

if six.PY2:
    # Python 2 specific code
    import cPickle as pickle
elif six.PY3:
    # Python 3 specific code
    import pickle

# Feature detection for Python 3.4+ features
if six.PY34:
    from importlib.util import spec_from_loader
else:
    spec_from_loader = None

Type Constants for isinstance() Checks

Tuple constants containing the appropriate types for cross-version isinstance() checks.

string_types: tuple  # String types tuple for isinstance() checks
integer_types: tuple  # Integer types tuple for isinstance() checks
class_types: tuple  # Class types tuple for isinstance() checks

Type Details:

  • string_types: (str,) in Python 3, (basestring,) in Python 2
  • integer_types: (int,) in Python 3, (int, long) in Python 2
  • class_types: (type,) in Python 3, (type, types.ClassType) in Python 2

Usage Examples:

import six

def process_string(value):
    if isinstance(value, six.string_types):
        return value.upper()
    raise TypeError("Expected string type")

def process_number(value):
    if isinstance(value, six.integer_types):
        return value * 2
    raise TypeError("Expected integer type")

Individual Type Constants

Direct references to the appropriate type for each Python version.

text_type: type  # Text string type (str in PY3, unicode in PY2)
binary_type: type  # Binary string type (bytes in PY3, str in PY2)

Usage Examples:

import six

def ensure_unicode(value):
    if isinstance(value, six.text_type):
        return value
    return value.decode('utf-8')

def ensure_bytes(value):
    if isinstance(value, six.binary_type):
        return value
    return value.encode('utf-8')

Maximum Integer Size

Cross-version constant for the maximum value an integer can hold.

MAXSIZE: int  # Maximum value a variable of integer type can take

This constant provides sys.maxsize equivalent across Python versions, handling platform-specific differences including Jython compatibility.

Usage Example:

import six

def check_large_number(value):
    if value > six.MAXSIZE:
        raise OverflowError("Number too large")
    return value

Import Specification Utility

Import specification utility available in Python 3.4+.

spec_from_loader: Callable | None  # importlib.util.spec_from_loader in PY34+, None otherwise

This provides access to importlib.util.spec_from_loader when available, or None for older Python versions.

Usage Example:

import six

if six.spec_from_loader is not None:
    # Use modern import specification
    spec = six.spec_from_loader("mymodule", loader)
else:
    # Fallback for older Python versions
    spec = None

Module Metadata

__author__: str  # "Benjamin Peterson <benjamin@python.org>"
__version__: str  # "1.17.0"

These constants provide access to the six library's metadata for version checking and attribution purposes.

Install with Tessl CLI

npx tessl i tessl/pypi-six

docs

execution.md

index.md

iterator-dict.md

metaclass.md

moves.md

string-bytes.md

testing.md

version-detection.md

tile.json