mod-009-avoid-string-based-class-selection

mod-009-avoid-string-based-class-selection is a cursor rule for Cursor from NVIDIA/physicsnemo. It costs 29 tokens per session (542 once invoked), scanned A, original, Apache-2.0.

A code-design rule for constructors that choose among several implementation classes. It recommends passing an already-created object when there are more than two or three possible choices instead of passing a class name as text.

In plain words
What is it for?
Use it when designing model or component constructors with multiple interchangeable implementations, deciding whether to inject an instance or select one by string.
Why use it?
Text-based selection hides dependencies and makes type checking, debugging, and testing harder. Passing an object makes the API clearer and allows test doubles to be supplied directly.

Cursor rule for Cursor ✓ vendor

Written for Cursor: installed under .cursor/.

Good fit Use it when designing model or component constructors with multiple interchangeable implementations, deciding whether to inject an instance or select one by string.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection
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,418 stars · on GitHub · developer.nvidia.com

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.

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-009-avoid-string-based-class-selection

README.md
[![agentmods](https://agentmods.dev/badge/rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection/github.svg)](https://agentmods.dev/rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection)
Your own site
<a href="https://agentmods.dev/rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection"><img src="https://agentmods.dev/badge/rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for mod-009-avoid-string-based-class-selection

Your own site · 80×15
<a href="https://agentmods.dev/rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection"><img src="https://agentmods.dev/badge/rules/nvidia/physicsnemo/mod-009-avoid-string-based-class-selection.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 29 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 542 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
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.1 $0.00029 $0.00542
Opus 5.5 $0.00012 $0.00217
Sonnet 5.5 $0.00006 $0.00108
Haiku 4.5 $0.00003 $0.00054

Measured 2mo ago against content hash 63caf4ca0322, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

mod-009-avoid-string-based-class-selection 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 2mo 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-009-avoid-string-based-class-selection.mdc · 76 lines

What it actually says

When designing model constructor APIs, rule MOD-009 should be followed. Explicitly reference "Following rule MOD-009, which discourages string-based class selection when there are many choices..." when deciding constructor parameter design.

MOD-009: Avoid string-based class selection in model constructors

Description:

Passing a string that represents a class name, which is then used to instantiate an internal submodule, should be avoided unless there are only a few choices (2 or 3 maximum) for the class name.

When there are more than 2-3 choices, the recommended practice is to pass an already instantiated instance of a submodule instead of a string primitive for dependency injection. This promotes better type safety, clearer APIs, and easier testing.

Rationale:

String-based class selection makes code harder to type-check, debug, and test. It obscures dependencies and makes it difficult for static analysis tools to understand the code. Direct instance injection provides better IDE support, type safety, and makes testing easier by allowing mock object injection.

Example:

# Good: Limited choices (2-3 max) - string selection acceptable
class MyModel(Module):
    def __init__(
        self,
        activation: Literal["relu", "gelu"] = "relu"
    ):
        if activation == "relu":
            self.act = nn.ReLU()
        elif activation == "gelu":
            self.act = nn.GELU()

# Good: Many choices - use instance injection
class MyModel(Module):
    def __init__(
        self,
        encoder: Module,  # Pass instance, not string
        decoder: Module   # Pass instance, not string
    ):
        self.encoder = encoder
        self.decoder = decoder

# Usage:
model = MyModel(
    encoder=MyCustomEncoder(dim=128),
    decoder=MyCustomDecoder(dim=128)
)

Anti-pattern:

# WRONG: String selection with many choices
class MyModel(Module):
    def __init__(
        self,
        encoder_type: str = "transformer"  # Many possible values
    ):
        # String-based factory pattern with 10+ choices
        if encoder_type == "transformer":
            self.encoder = TransformerEncoder()
        elif encoder_type == "cnn":
            self.encoder = CNNEncoder()
        # ... many more options
        # WRONG: Should accept encoder instance instead
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. 2mo ago First seen · 76 lines · 29 tokens per session scan A 63caf4ca0322

Subscribe to this mod's changes

mod-009-avoid-string-based-class-selection is a cursor rule published in the GitHub repository NVIDIA/physicsnemo (3,418 stars, last pushed yesterday), licensed Apache-2.0. It adds 29 tokens to every session and 542 once invoked, about $0.0001 per session on Opus 5.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.

Related

mod-009-avoid-string-based-class-selection compared with cursor rules from other repositories

mod-009-avoid-string-based-class-selection next to 6 cursor rules from other repositories
ModTypeStarsLast pushLicenceScanTokensWorks with
mod-009-avoid-string-based-class-selectionNVIDIA/physicsnemoCursor rule3.4kyesterdayApache-2.0A29Cursor
python_libstreamlit/streamlitCursor rule46kyesterdayApache-2.0A0Cursor
typescriptstreamlit/streamlitCursor rule46kyesterdayApache-2.0A5,351Cursor
specsstreamlit/streamlitCursor rule46kyesterdayApache-2.0A0Cursor
workflowsstreamlit/streamlitCursor rule46kyesterdayApache-2.0A0Cursor
skillsstreamlit/streamlitCursor rule46kyesterdayApache-2.0A0Cursor
python_testsstreamlit/streamlitCursor rule46kyesterdayApache-2.0A0Cursor