pydantic-basesettings-class-attributes

Prevent non-annotated attribute errors in Pydantic BaseSettings subclasses.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/dragonkid/dotfiles --skill pydantic-basesettings-class-attributes
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pydantic-basesettings-class-attributes
Source: https://github.com/dragonkid/dotfiles/tree/main/claude/skills/learned/pydantic-basesettings-class-attributes
Command: npx skills add https://github.com/dragonkid/dotfiles --skill pydantic-basesettings-class-attributes

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

In Pydantic BaseSettings and BaseModel subclasses, class attributes without type annotations are treated as model fields, causing non-annotated attribute errors. Resolve this by moving constants outside the class as module-level constants or by using ClassVar annotations.

Core Features & Use Cases

  • Avoid model-field leakage by relocating constants to module scope.
  • Use ClassVar to explicitly mark non-field attributes in Pydantic models.
  • Facilitate safer refactoring of hard-coded mappings and configurations.

Quick Start

Refactor a BaseSettings subclass by extracting constants to module scope (e.g., _APOLLO_MAPPING) and, where appropriate, annotate with ClassVar; then run a quick validation to ensure the model loads without errors.

Frequently Asked Questions about pydantic-basesettings-class-attributes

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does Pydantic BaseSettings throw a non-annotated attribute error for class constants?

Pydantic BaseSettings throws a non-annotated attribute error because it treats un-annotated class attributes as model fields. You must explicitly annotate internal constants with ClassVar or move them to module scope to prevent model-field leakage.

How do I fix non-annotated attribute errors in Pydantic BaseModel subclasses?

Fix non-annotated attribute errors in Pydantic BaseModel subclasses by annotating internal mappings or flags with ClassVar. Alternatively, relocate hard-coded constants outside the class as module-level variables to ensure proper model validation.

When do I need to use ClassVar annotations in Pydantic models?

Use ClassVar annotations in Pydantic models when defining internal mappings, flags, or hard-coded configurations as class attributes. This explicitly marks them as non-field attributes, preventing them from being processed as model fields during validation.

Does Pydantic require external packages to handle non-annotated class attributes?

No, resolving non-annotated class attributes in Pydantic requires no external packages. It relies entirely on standard Python tooling and Pydantic type annotations, using built-in ClassVar typing or module-level variable extraction to fix validation errors.

What is the best way to refactor hard-coded mappings in Pydantic BaseSettings?

The best way to refactor hard-coded mappings in Pydantic BaseSettings is to extract them as module-level constants or annotate them with ClassVar. This facilitates safer refactoring and avoids model-field leakage during class initialization.