Skip to content

fix(refusal_classifier): quote self-referential annotation to fix import NameError - #345

Merged
msoedov merged 1 commit into
msoedov:mainfrom
Anai-Guo:fix/hybrid-classifier-self-annotation
Sep 11, 2026
Merged

msoedov merged 1 commit into
msoedov:mainfrom
Anai-Guo:fix/hybrid-classifier-self-annotation

Conversation

@Anai-Guo

Copy link
Copy Markdown
Contributor

Problem

agentic_security/refusal_classifier/hybrid_classifier.py raises NameError at import time, which breaks the entire HybridRefusalClassifier module (and any test or code that imports it).

The add_detector method is defined inside class HybridRefusalClassifier with a return annotation that refers to the class itself:

class HybridRefusalClassifier:
    def add_detector(
        self,
        detector: RefusalDetector,
        weight: float = 1.0,
        name: str | None = None,
    ) -> HybridRefusalClassifier:   # <-- evaluated eagerly during class-body execution

The module does not use from __future__ import annotations, so this return annotation is evaluated eagerly while the class body is still executing — at which point the name HybridRefusalClassifier is not yet bound. Importing the module fails:

$ python -c "import agentic_security.refusal_classifier.hybrid_classifier"
NameError: name 'HybridRefusalClassifier' is not defined

Because tests/unit/refusal_classifier/test_hybrid_classifier.py imports the module at the top level, that whole test file currently fails to collect.

The module-level factory create_hybrid_classifier(...) -> HybridRefusalClassifier is fine — it runs after the class is defined; only the in-class self-reference is affected.

Fix

Quote the self-referential annotation so it becomes a forward reference (standard PEP 484 pattern), matching how self-references are normally written without from __future__ import annotations:

    ) -> "HybridRefusalClassifier":

One-line change, no behavior change.

Verification

Before: import agentic_security.refusal_classifier.hybrid_classifierNameError.
After: the module imports cleanly and HybridRefusalClassifier() instantiates, so tests/unit/refusal_classifier/test_hybrid_classifier.py can be collected again.

🤖 Generated with Claude Code

@msoedov

msoedov commented Sep 11, 2026

Copy link
Copy Markdown
Owner

@Anai-Guo thx for the patch!

@msoedov
msoedov merged commit 2340bc2 into msoedov:main Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants