# TypeError: can't set attributes of built-in/extension type 'datetime.datetime'

- **ID:** `python/pytest-freezegun-datetime-conflict`
- **Domain:** python
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

freezegun tried to patch datetime.datetime but a C-extension module (e.g. pandas, psycopg2) imported datetime earlier and holds a C-level reference that cannot be monkeypatched.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.5.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   from unittest.mock import patch
import mypkg.service

@patch('mypkg.service.datetime')
def test_now(mock_dt):
    mock_dt.utcnow.return_value = datetime(2024, 1, 1)
    ...
   ```
2. **** (95% success)
   ```
   # service takes a clock=datetime.utcnow parameter
def test_now():
    svc = Service(clock=lambda: datetime(2024, 1, 1))
    assert svc.today() == date(2024, 1, 1)
   ```

## Dead Ends

- **** — Import order in tests does not control the order inside application code imported at collection time. (70% fail)
- **** — Fails for the same reason: the C extension holds a direct reference to the built-in type. (80% fail)
