Skip to content
Back to skills

Fallback Compatibility

ASecurity

For cross-version support: try/except imports, optional dependencies, graceful degradation across Python versions.

  • 4 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 7, 2026
ai-agentspythonapi

Works with

  • api

Security analysis

A100/100

Scanned November 7, 2026

npx -y skills add jimmc414/claude-code-plugin-marketplace --skill fallback-compatibility --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Fallback Compatibility?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Fallback Compatibility
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jimmc414-fallback-compatibility/badge)](https://www.skillsdirectory.com/skills/jimmc414-fallback-compatibility)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: fallback-compatibility
description: "For cross-version support: try/except imports, optional dependencies, graceful degradation across Python versions."
---

# fallback-compatibility

## When to Use
- Supporting multiple Python versions
- Optional dependency handling
- Feature detection
- Graceful degradation

## When NOT to Use
- Single Python version target
- Required dependency (should fail if missing)
- Over-engineering simple code

## The Pattern

Try preferred import/feature, fall back to alternative.

```python
# Python version compatibility
try:
    from functools import cache
except ImportError:
    from functools import lru_cache
    cache = lru_cache(None)

# Optional dependency
try:
    import numpy as np
    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False

def process(data):
    if HAS_NUMPY:
        return np.array(data).mean()
    else:
        return sum(data) / len(data)
```

## Example (from pytudes)

```python
# beal.py - math.gcd location changed between versions
try:
    from math import gcd      # Python 3.5+
except ImportError:
    from fractions import gcd # Python 2.7 and early 3.x

# ngrams.py - use available modules
try:
    from functools import cache
except ImportError:
    from functools import lru_cache
    def cache(func):
        return lru_cache(None)(func)

# Pattern for conditional features
try:
    # Python 3.10+ pattern matching
    def dispatch(x):
        match x:
            case int(): return handle_int(x)
            case str(): return handle_str(x)
            case _: return handle_other(x)
except SyntaxError:
    # Fallback for older Python
    def dispatch(x):
        if isinstance(x, int):
            return handle_int(x)
        elif isinstance(x, str):
            return handle_str(x)
        else:
            return handle_other(x)

# Graceful feature degradation
try:
    import matplotlib.pyplot as plt
    def plot(data):
        plt.plot(data)
        plt.show()
except ImportError:
    def plot(data):
        print("Plotting requires matplotlib")
        print(f"Data: {data[:10]}...")
```

## Key Principles
1. **Try modern first**: Prefer newer, better APIs
2. **ImportError for missing**: Standard exception for imports
3. **Create compatible API**: Wrapper that works either way
4. **Flag for optional**: `HAS_FEATURE` pattern
5. **Fail gracefully**: Warn, don't crash

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.