Skip to content

Suppress pydantic's shadow warning for names listed in shadowed_fields= #150

Open

Description

@cdwilson

shadowed_fields= is the explicit opt-in for a field that shadows a model method: by the time it appears in a class definition, the user has met ShadowedFieldError (or the docs page explaining it) and accepted the cost for that name. pydantic's class-creation shadow warning for the same name still fires, on every import.

Why it matters

The warning reports exactly the condition the class kwarg just acknowledged, with a weaker signal than the hard error the user already resolved. It reads as "something is still wrong" when nothing is. Users who want it gone reach for warnings.filterwarnings(...), which is process-global and easy to over-scope — and sanctioned configuration that emits warning noise on import trains people to ignore warnings.

What to do

TinydanticModelMetaclass.__new__ sees the class kwargs before super().__new__() runs pydantic's field collection (where the warning is emitted), so it can wrap that call in warnings.catch_warnings() and filter pydantic's specific message — Field name "<name>" in "<class>" shadows an attribute — for exactly the opted-out names. Two details to settle at implementation time:

  • The filter must use the resolved opt-out set: a base class's shadowed_fields= covers its subclasses (resolution goes through get_config_value), so the literal kwarg on the class being built is not the whole story.
  • warnings.catch_warnings() swaps process-global state and is not thread-safe. Class creation is normally import-time and single-threaded, but the trade-off belongs in a comment next to the code.

Reproduction

import warnings

from tinydb import TinyDB
from tinydb.storages import MemoryStorage

from tinydantic import TinydanticModel

db = TinyDB(storage=MemoryStorage)

with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")

    class Report(TinydanticModel, database=db, shadowed_fields=("count",)):
        count: int

for warning in caught:
    print(f"{warning.category.__name__}: {warning.message}")

Run with python repro.py (project venv).

Actual output:

UserWarning: Field name "count" in "Report" shadows an attribute in parent "TinydanticModel"

Expected: no output — shadowed_fields=("count",) already acknowledged precisely this shadowing. (A shadowing name that is not opted out never gets this far: ShadowedFieldError refuses the class.)

Environment: tinydantic 0.4.0 at 389c6d9, Python 3.10.20, TinyDB 4.9.0, pydantic 2.13.4, macOS. Run 2026-08-14.


The reproduction was run as shown. No fix was built or tested. Surfaced while working through the conflict-handling story for #121.

Assisted-by: Claude:claude-fable-5

Activity

  1. added
    enhancementNew feature or request
    polishSmall cleanup, no behavior change
    ai-assistedCreated or investigated with help from an AI agent
    on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-assistedCreated or investigated with help from an AI agentenhancementNew feature or requestpolishSmall cleanup, no behavior change

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions