mod-011-use-proper-optional-dependency-handling

mod-011-use-proper-optional-dependency-handling is a cursor rule for Cursor from NVIDIA/physicsnemo. It costs 36 tokens per session (787 once invoked), scanned A, original, Apache-2.0.

A project rule for handling optional Python packages—dependencies that are needed only for certain features—without loading them unnecessarily.

In plain words
What is it for?
Use it when adding dependency checks, version-specific features, or optional packages to PhysicsNeMo model code.
Why use it?
It prevents optional packages from breaking unrelated code and keeps dependency declarations in one place. It also protects features that require specific package versions.

Cursor rule for Cursor

About the project

PhysicsNeMo is an open-source PyTorch framework for creating, training, and fine-tuning machine-learning models for physics, scientific computing, and engineering. Researchers and engineers use its reusable components and training recipes for applications such as aerodynamics, weather forecasting, structural mechanics, geophysics, and thermal design. The catalogue entries provide rules and skills for working with PhysicsNeMo projects.

NVIDIA/physicsnemo · 3,222 stars · on GitHub

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nvidia/physicsnemo/mod-011-use-proper-optional-dependency-handling
Clone the repo
git clone --depth 1 https://github.com/NVIDIA/physicsnemo

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for mod-011-use-proper-optional-dependency-handling

README.md
[![agentmods](https://agentmods.dev/badge/rules/nvidia/physicsnemo/mod-011-use-proper-optional-dependency-handling.svg)](https://agentmods.dev/rules/nvidia/physicsnemo/mod-011-use-proper-optional-dependency-handling)
Your own site
<a href="https://agentmods.dev/rules/nvidia/physicsnemo/mod-011-use-proper-optional-dependency-handling"><img src="https://agentmods.dev/badge/rules/nvidia/physicsnemo/mod-011-use-proper-optional-dependency-handling.svg" alt="Measured on agentmods" height="20"></a>
Per session 36 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 787 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00036 $0.00787
Opus 5 $0.00018 $0.00394
Sonnet 5 $0.00007 $0.00157
Haiku 4.5 $0.00004 $0.00079

Measured 5d ago against content hash abc7ff27d7cc, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

mod-011-use-proper-optional-dependency-handling scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 5d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/mod-011-use-proper-optional-dependency-handling.mdc · 101 lines

What it actually says

When handling optional dependencies in model code, rule MOD-011 must be followed. Explicitly reference "Following rule MOD-011, which requires using check_min_version() for optional dependencies..." when implementing dependency checks.

MOD-011: Use proper optional dependency handling

Description:

When a model requires optional dependencies (packages not installed by default), use the PhysicsNeMo APIs for dependency handling:

  1. check_min_version(package, version, hard_fail=False): Use this function to check if a package is installed and available without actually importing it. Set hard_fail=True for hard requirements, hard_fail=False for soft requirements. This is the primary method for handling optional dependencies.

  2. @require_version(package, version): Use this decorator when core code must always be available but certain features need to be protected against older versions. This is rare and should only be used when you need to protect specific methods or classes.

  3. pyproject.toml: This file is the one, only, and universal source of truth for all dependencies in PhysicsNeMo. All optional dependencies must be declared there.

Rationale:

Centralized dependency handling ensures consistent error messages and version checking across the codebase. Checking availability without importing prevents import errors and allows graceful degradation. Using pyproject.toml as the single source of truth prevents dependency specification from becoming scattered and inconsistent.

Example:

import torch
from physicsnemo.core import Module
from physicsnemo.core.version_check import check_min_version, require_version

# Check optional dependency availability without importing
APEX_AVAILABLE = check_min_version("apex", "0.1.0", hard_fail=False)

class MyModel(Module):
    def __init__(
        self,
        input_dim: int,
        use_apex: bool = False
    ):
        super().__init__()
        self.use_apex = use_apex

        if use_apex and not APEX_AVAILABLE:
            raise RuntimeError(
                "apex is required for use_apex=True but is not installed. "
                "Install with: pip install apex>=0.1.0"
            )

        if use_apex:
            import apex  # Only import when actually needed
            self.fused_layer = apex.FusedLayer()
        else:
            self.fused_layer = None

# Using @require_version for protecting version-specific features
class AdvancedModel(Module):
    @require_version("torch", "2.4.0")
    def use_device_mesh(self):
        """This feature requires torch>=2.4.0."""
        from torch.distributed.device_mesh import DeviceMesh
        # Protected code

Anti-pattern:

# WRONG: Direct import without checking availability
import apex  # Will fail if apex not installed!

class MyModel(Module):
    def __init__(self, use_apex: bool = False):
        if use_apex:
            self.layer = apex.FusedLayer()  # Already failed at import!

# WRONG: Try/except for dependency checking
try:
    import apex
    APEX_AVAILABLE = True
except ImportError:
    APEX_AVAILABLE = False
# Use check_min_version instead!

# WRONG: Hardcoded version strings in multiple places
if version.parse(apex.__version__) < version.parse("0.1.0"):
    raise ImportError("apex>=0.1.0 required")
# Should use check_min_version or require_version!
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 5d ago First seen · 101 lines · 36 tokens per session scan A abc7ff27d7cc

Subscribe to this mod's changes

mod-011-use-proper-optional-dependency-handling is a cursor rule published in the GitHub repository NVIDIA/physicsnemo (3,222 stars, last pushed today), licensed Apache-2.0. It adds 36 tokens to every session and 787 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.