Skip to content

Type parameters & datetime.replace #299

@kellerza

Description

@kellerza

PEP 695, Python 3.12+

This function gives 2 errors

def strip_utc[T](obj: T) -> T:
    """Remove UTC from datetime objects inside lists or dicts."""
    if isinstance(obj, datetime):
        return obj.replace(tzinfo=None)  # <<<<<<<<<<<<<<<<
    if isinstance(obj, list):
        return cast(
            T,
            [strip_utc(i) if isinstance(i, list | dict | datetime) else i for i in obj],
        )
    if isinstance(obj, dict):
        res = dict.copy(obj)
        for k, v in res.items():
            if isinstance(v, list | dict | datetime):
                res[k] = strip_utc(v)
        return res  # <<<<<<<<<<<<<<<<<<<<<<<<<<
    return obj
error: Incompatible return value type (got "datetime", expected "T")  [return-value]
error: Incompatible return value type (got "dict[Any, list | dict | datetime]", expected "T")  [return-value]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions