Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

SaisakthiM
Copy link

@SaisakthiM SaisakthiM commented Sep 10, 2025

Description:

Summary

This PR addresses the deprecation warning for Color in FastAPI when using Pydantic v2 while maintaining backward compatibility with Pydantic v1.

Previously, fastapi/encoders.py imported Color directly from pydantic.color, which is deprecated in Pydantic v2, leading to warnings for users upgrading.

This change ensures that the correct Color class is used depending on the installed Pydantic version.


Changes

  • Conditional import of Color:
    try:
        # Pydantic v2
        from pydantic_extra_types.color import Color
    except ImportError:
        # Fallback for Pydantic v1
        from pydantic.color import Color
    

No other functionality is altered.

Fully backward-compatible with both Pydantic v1 and v2.

Checklist

I have verified that the change fixes the deprecation warning.
Code follows existing FastAPI style and conventions.
Tests pass for Pydantic v2.
Tests pass for Pydantic v1.
No breaking changes introduced for either Pydantic version.
Documentation updated if necessary (none needed for this fix).

Testing

Environment 1 – Pydantic v2 (current default)
pytest tests/test_jsonable_encoder.py -v
21 tests collected
18 passed, 3 skipped (v1-specific tests)

Environment 2 – Pydantic v1
pip install "pydantic<2.0"
pytest tests/test_jsonable_encoder.py -v
21 tests collected
17 passed, 4 skipped (v2-specific tests)

Results: ✅ All relevant tests pass in both environments. Only version-specific tests are skipped, as expected.

Motivation

Remove Pydantic v2 deprecation warnings.
Maintain backward compatibility with Pydantic v1.
Ensure smooth user experience for projects upgrading to Pydantic v2.

@SaisakthiM SaisakthiM closed this Sep 10, 2025
@SaisakthiM SaisakthiM changed the title fix: replace deprecated Color with pydantic_extra_types.color.Color fix: make Color import backward-compatible for Pydantic v1 and v2 Sep 10, 2025
@SaisakthiM SaisakthiM reopened this Sep 10, 2025
@YuriiMotov YuriiMotov marked this pull request as draft September 11, 2025 06:57
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SaisakthiM, thanks for the PR!

It looks like the tests are failing for Pydantic v1, could you have a look?

Also, could you add a test that checks the deprecation warning isn't thrown anymore? (basically we'd need a test that fails on master but succeeds on this PR, to showcase the change in functionality)

from pydantic.networks import AnyUrl, NameEmail
from pydantic.types import SecretBytes, SecretStr
from pydantic_extra_types.color import Color # ✅ updated import
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to import this only when you need it for Pydantic v2, as this will crash on Pydantic v1.

@svlandeg svlandeg added the feature New feature or request label Sep 12, 2025
@svlandeg svlandeg changed the title fix: make Color import backward-compatible for Pydantic v1 and v2 👽️ Update Color import depending on the version of Pydantic Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request waiting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants